-3

我有下面列出的这 3 张验证码图像,我想使用 opencv 使文本尽可能干净。有没有可以实现我想要的管道?

这是我想要的一个例子:

这里最大的问题是字母的色差。任何帮助,将不胜感激!

这是我的代码中包含所有图像预处理的一些部分

raw_img = cv2.imread(image_file) #load image
img = cv2.cvtColor(raw_img, cv2.COLOR_RGB2GRAY) #grayscale

_,thresh = cv2.threshold(img,240,255, cv2.THRESH_BINARY_INV + cv2.THRESH_TRUNC) #thresholding

thresh = cv2.bitwise_not(thresh) #invert black and white

#resizing image
resized = cv2.resize(img, (140, 60), interpolation=cv2.INTER_AREA)

img_gray = cv2.copyMakeBorder(resized, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255, 255, 255])

#blur
blur = cv2.GaussianBlur(img_gray, (3, 3), 0)

# threshold again
_, img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY or cv2.THRESH_OTSU)

cv2.imshow("Output", img)
cv2.waitKey()

但是,如果我将相同的代码应用于第二个 iamge,例如,这就是我得到的:

我对 OpenCV 比较陌生。我不确定我是否在这里使用了正确的方法,如果有人可以帮助我展示其他方法来清理这些图像,我会非常感激!

4

1 回答 1

2

由于图像中的背景似乎相同,您可以减去背景以找到前景对象。要获取背景图像,您可以运行此代码或使用下面提供的图像。

import numpy as np
import cv2
img = cv2.imread('./test_img3.png')  # use the 3rd example
bg = np.repeat(img[:,0, np.newaxis], 180, axis=1)  # take the first column and repeat
cv2.imwrite('cap_bg.png', bg)

这给出了以下图像,然后可以将其用于减法:

验证码背景

为了找到线,可以使用霍夫变换。它可以在灰度图像中找到线条,并且在 openCV 文档中有很好的解释。

bin_img = ((img - bg) > 0).astype(np.uint8) * 255  # subtract background and obtain binary image
bin_img = cv2.cvtColor(bin_img, cv2.COLOR_RGB2GRAY)  # convert to greyscale image
lines = cv2.HoughLines(bin_img, 1, np.pi/360, 100)  # find lines

这将输出具有霍夫变换和给定参数的图像中找到的所有线的列表。现在你只需要通过在它们上面画一条背景颜色的线来“擦除”每条线。您需要使用参数,但我希望这可以帮助您开始。

于 2021-08-04T21:28:48.030 回答