首页 > 解决方案 > 使用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结果:

修复后的结果

有没有更好的方法来清理图像?生成的图像看起来不太好有没有办法改善结果?

标签: pythonpython-3.xopencvhough-transform

解决方案


推荐阅读