python - 使用openCV从图像中去除直线噪声
问题描述
我正在尝试清理其中包含直线作为噪声的验证码图像。
我对 OpenCV 很陌生,但据我了解,最好的方法是将图像转换为黑白,然后使用霍夫线变换来检测线条,然后对其进行遮罩。
我在这里做到了:
im_gray = cv2.imread('download.png', cv2.IMREAD_GRAYSCALE)
im_bw = cv2.threshold(im_gray, 120, 255, cv2.THRESH_BINARY)[1]
im_bw_inverted = cv2.bitwise_not(im_bw)
im_bw_inverted
:
然后我使用霍夫线检测了这些线:
rho = 1
theta = np.pi / 180
min_line_length = 1
max_line_gap = 20
threshold = 50
line_image = np.copy(im_bw) * 0
lines = cv2.HoughLinesP(im_bw_inverted, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),1)
line_image
:
用来inpaint
掩盖它:
line_image_dilate = cv2.dilate(line_image, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4)))
dst = cv2.inpaint(im_bw_inverted,line_image_dilate, 1, cv2.INPAINT_NS)
dst
结果:
有没有更好的方法来清理图像?生成的图像看起来不太好有没有办法改善结果?
解决方案
推荐阅读
- java - 如何让java编译器忽略错误/警告
- jquery - Combo 上的 Jquery 事件打开
- r - 用 xml2 解析 xml
- ios - 如何设置按钮图像色调颜色?
- node.js - NativeBase 设置/安装错误
- angular - Angular 2/4 自动完成搜索框
- android - 如何让 ImageSpan 与 TextView 中的其余文本具有相同的高度
- r - 如何解决错误“观察太少”。使用 ROSE 平衡 R 中的数据时?
- c# - 构建 VSTS 错误,无法恢复
- python - 为什么在尝试获取 Python Built-in 的源代码时,inspect.getsource 会抛出 TypeError?