简体   繁体   English

Python opencv消除验证码的噪音

[英]Python opencv remove noise from captcha

I need to resolve captcha automatically to grab the public data from sites. 我需要自动解析验证码才能从站点获取公共数据。

I use python and opencv. 我使用python和opencv。 I'm newbee in solving the images processing. 我是解决图像处理方面的新手。 After search, as a method to resolve captcha I came up with next. 搜索后,我想出了一种解决验证码的方法。 As the text in Captha uses group of related colours I try to use the HSV format and mask, then convert image to Grayscale and use Threshold (Adaptive_THRESH_MEAN_C) to remove noise from the image. 由于Captha中的文本使用了一组相关的颜色,因此我尝试使用HSV格式和蒙版,然后将图像转换为灰度并使用阈值(Adaptive_THRESH_MEAN_C)来消除图像中的噪点。

But this is not enough to remove noise and provide automatic text recognition with OCR (Tesseract). 但这还不足以消除噪音并使用OCR(Tesseract)提供自动文本识别。 See images below. 参见下面的图片。

Is there something I can improve in my solution or there is a better way? 有什么可以改善我的解决方案的方法吗?

Original images: 原始图片:

captcha1captcha2

Processed images: 处理的图像:

captcha1captcha2

Code: 码:

import cv2
import numpy as np

img = cv2.imread("1.jpeg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255)) #green
# mask = cv2.inRange(hsv, (0, 0, 0), (10, 255, 255))
# mask = cv2.inRange(hsv, (125, 0, 0), (135, 255,255))

img = cv2.bitwise_and(img, img, mask=mask)
img[np.where((img == [0,0,0]).all(axis = 2))] = [255,255,255]

img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, 2)

cv2.imwrite("out.png", img)

I think you can reach a good performance by applying some smoothing methods and after that finding image edges. 我认为您可以通过应用一些平滑方法并在找到图像边缘后达到良好的性能。 Here is the code: 这是代码:

import cv2

img = cv2.imread("input.jpg")
# smoothing the image
img = cv2.medianBlur(img, 5)

#edge detection    
edges = cv2.Canny(img, 100, 200)
cv2.imwrite('output.png', edges)

在此处输入图片说明 在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

You can try different approaches to achieve your goal: Your first image can be processed via the application of a median filter (r=2), followed by adaptive thresholding: 您可以尝试不同的方法来实现您的目标:可以通过应用中值滤波器(r = 2)处理第一张图像,然后进行自适应阈值处理: 带有清晰文字的已处理图像

The binary option of Opening would be another option one could try: Opening的二进制选项是另一个可以尝试的选项: 带有清晰文本的已处理图像。文字的质量比图片低 .

Note that the quality is lower than with the first approach (especially the last G is visibily degraded). 请注意,其质量低于第一种方法(尤其是最后一个G明显降解)。

The second image responds different to the treatment than the first one: 第二张图片对处理的反应与第一张图片不同:

For the median approach: 对于中位数方法:

在此处输入图片说明

For opening: 开启:

第二张图片

However, it is possible to extract the text via the application of a median blur (r=1), followed by auto-contrast and then thresholding with 50: 但是,可以通过应用中间模糊(r = 1)提取文本,然后进行自动对比,然后使用50进行阈值处理:

图像二降低了噪点

As you can see, it is possible to improve the quality of your images enough be recognizable. 如您所见,可以提高图像的质量,使其足以被识别。 The first image can be converted to text without problem, but the second one can only be recognized partially. 第一张图像可以毫无问题地转换为文本,但是第二张图像只能被部分识别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM