简体   繁体   English

Python image clean 解决验证码

[英]Python image clean to solve the 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:原始图像:

在此处输入图像描述

Processed images:处理后的图像:

在此处输入图像描述

from PIL import Image
image = Image.open("captcha-img.png").convert("L")
pixel_matrix = image.load()

# thresholding
for column in range(0, image.height):
    for row in range(0, image.width):
        if pixel_matrix[row, column] != 0:
            pixel_matrix[row, column] = 255

# stray line and pixel removal
for column in range(1, image.height - 1):
    for row in range(1, image.width - 1):
        if pixel_matrix[row, column] == 0 \
            and pixel_matrix[row, column - 1] == 255 and pixel_matrix[row, column + 1] == 255:
            pixel_matrix[row, column] = 255
        if pixel_matrix[row, column] == 0 \
            and pixel_matrix[row - 1, column] == 255 and pixel_matrix[row + 1, column] == 255:
            pixel_matrix[row, column] = 255

image.save("output.png")

If you squint your eyes, the noise gets reduced.如果你眯起眼睛,噪音会降低。

Same could be applied to the image by blurring it via a convolution .同样可以通过卷积对图像进行模糊处理。

Using a multi-blur approach, one can get acceptable results.使用多重模糊方法,可以获得可接受的结果。 Wether or not its suitable for your OCR, I do not know.我不知道它是否适合您的 OCR。

在此处输入图像描述

import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from scipy.signal import convolve2d

# get numpy array from img
image = Image.open("captcha-img.png").convert("L")
img = np.array(image)

# blur img via convolution
blur_size = 3
blur_repeat = 6
blur_kernel = np.ones((blur_size, blur_size))
for _ in range(blur_repeat):
    img = convolve2d(img, blur_kernel)

# threshold and crop
remaining_img = img > np.median(img)
border = blur_size*blur_repeat
plt.imshow(remaining_img[border:-border+1, border:-border+1])
plt.show()

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

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