正在清理captcha imag

2024-05-19 19:18:21 发布

您现在位置:Python中文网/ 问答频道 /正文

captcha image

我试图清理上面的图像我尝试了几种不同的方法使用开放式简历,我要么过度侵蚀原始图像,导致部分字母丢失,如下图所示:

result of erosion via python opencv 3

我不太确定如何删除最后一条对角线并修复S,到目前为止,我的代码是:

import cv2 
import matplotlib.pylab as plt
img = cv2.imread('/captcha_3blHDdS.png')

#make image gray 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#Blur
blur = cv2.GaussianBlur(gray,(5,5),0)
bilateral = cv2.bilateralFilter(gray,5,75,75)

#Thresholding
ret, thresh = cv2.threshold(bilateral,25,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

#Kernal
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

#other things
erosion = cv2.erode(thresh,kernel,iterations = 1)
closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel, iterations = 1)

#Transform image
dist_transform = cv2.distanceTransform(closing,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.02*dist_transform.max(),255,cv2.THRESH_BINARY)#,255,0)

#kernel_1
kernel_1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 2))

dilation_1 = cv2.dilate(sure_fg,kernel_1,iterations = 2)
erosion_1 = cv2.erode(dilation_1,kernel_1,iterations = 3)

plt.imshow(erosion_1, 'gray')

任何帮助将不胜感激,这里有更多的例子,类型的图像,是由验证码产生的; example of captcha images

这里还有指向包含images的文件夹的链接


Tags: 图像imageimportimgdisttransformpltcv2
3条回答

下面是一个使用OpenCvSharp的C解决方案(应该很容易转换回python/C++,因为方法名完全相同)。在

它使用OpenCV的inpainting技术来避免在可能运行OCR阶段之前销毁过多的字母。我们可以看到这些线的颜色与其他线不同,所以我们将在很早的时候使用这些信息,在任何灰度/白化之前。步骤如下:

  • 用线条的颜色制作一个面具(#707070)
  • 将遮罩放大一点,因为线条可能是用抗锯齿绘制的
  • 使用此蒙版重新绘制(“修复”)原始图像,这将删除线条,同时保留线条(字母)下方的大部分内容。注意,我们可以在这一步之前去掉小的点,我认为这样会更好
  • 应用一些放大/模糊/阈值来完成

这是面具:

enter image description here

结果如下:

enter image description here

以下是样本集的结果:

enter image description here

以下是C代码:

static void Decaptcha(string filePath)
{
    // load the file
    using (var src = new Mat(filePath))
    {
        using (var binaryMask = new Mat())
        {
            // lines color is different than text
            var linesColor = Scalar.FromRgb(0x70, 0x70, 0x70);

            // build a mask of lines
            Cv2.InRange(src, linesColor, linesColor, binaryMask);
            using (var masked = new Mat())
            {
                // build the corresponding image
                // dilate lines a bit because aliasing may have filtered borders too much during masking
                src.CopyTo(masked, binaryMask);
                int linesDilate = 3;
                using (var element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(linesDilate, linesDilate)))
                {
                    Cv2.Dilate(masked, masked, element);
                }

                // convert mask to grayscale
                Cv2.CvtColor(masked, masked, ColorConversionCodes.BGR2GRAY);
                using (var dst = src.EmptyClone())
                {
                    // repaint big lines
                    Cv2.Inpaint(src, masked, dst, 3, InpaintMethod.NS);

                    // destroy small lines
                    linesDilate = 2;
                    using (var element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(linesDilate, linesDilate)))
                    {
                        Cv2.Dilate(dst, dst, element);
                    }

                    Cv2.GaussianBlur(dst, dst, new Size(5, 5), 0);
                    using (var dst2 = dst.BilateralFilter(5, 75, 75))
                    {
                        // basically make it B&W
                        Cv2.CvtColor(dst2, dst2, ColorConversionCodes.BGR2GRAY);
                        Cv2.Threshold(dst2, dst2, 255, 255, ThresholdTypes.Otsu);

                        // save the file
                        dst2.SaveImage(Path.Combine(
                            Path.GetDirectoryName(filePath),
                            Path.GetFileNameWithoutExtension(filePath) + "_dst" + Path.GetExtension(filePath)));
                    }
                }
            }
        }
    }
}

这不是一个非常可靠的解决方案,但在大多数情况下可能会有帮助:

通过看到上面发布的图像样本,我可以观察到一个关于对角线的共同特征,它们要么开始于图像边缘,要么结束于图像边缘,而我们感兴趣的文本位于中间,这样我们就可以通过在图像矩阵并将其作为噪声消除。 而且这种方法也可能花费更少的时间。在

仔细看看你的验证码。灰阶值和图像中的灰度值不同。在

文本在140中,尘埃在{}中。在

一个简单的灰度过滤在这里会有很大的帮助。在

from scipy.misc import imread, imsave
import numpy as np

infile = "A1nO4.png"
outfile = "A1nO4_out.png"

im = imread(infile, True)
out_im = np.ones(im.shape) * 255

out_im[im == 140] = 0

imsave(outfile, out_im)

enter image description here

现在使用cv2.dilatecv2.erode在白纸黑字上)来清除残留的灰尘。在

相关问题 更多 >