Ahm*_*şli 8 python ocr captcha tesseract image-processing
我对图像处理很陌生,我想做的是清除验证码中的噪音;
对于验证码,我有不同类型的验证码:
对于第一个我所做的是:
首先,我将每个不是黑色的像素转换为黑色。然后,我从图像中找到了一种噪声模式并将其删除。对于第一个验证码,很容易清除它,我找到了带有 tesseract 的文本。
但我正在为第二个和第三个寻找解决方案。
这一定是怎样的?我的意思是清除它的可能方法是什么?
这是我删除模式的方式:
def delete(searcher,h2,w2):
h = h2
w = w2
search = searcher
search = search.convert("RGBA")
herear = np.asarray(search)
bigar = np.asarray(imgCropped)
hereary, herearx = herear.shape[:2]
bigary, bigarx = bigar.shape[:2]
stopx = bigarx - herearx + 1
stopy = bigary - hereary + 1
pix = imgCropped.load()
for x in range(0, stopx):
for y in range(0, stopy):
x2 = x + herearx
y2 = y + hereary
pic = bigar[y:y2, x:x2]
test = (pic == herear)
if test.all():
for q in range(h):
for k in range(w):
pix[x+k,y+q] = (255,255,255,255)
Run Code Online (Sandbox Code Playgroud)
对不起变量名,我只是在测试函数。
谢谢..
这是我所能得到的:
您可能知道medianBlur
在每个内核中找到中值并将该值替换为内核中心的函数。我们可以做类似的事情,但不要使用中位数,使用最大值然后使用最小值。也有中值模糊,我得到了一些结果。我知道它们并不完美,但我希望它能给你一些想法(你可以使用输入图像和内核的大小,它可能会使结果更好一点)。
我现在没有安装 python,所以我分享了我使用过的确切 C++ 代码:
Mat im1 = imread("E:/1/3.jpg", 0);
Mat im2, im3;
im2 = Mat::zeros(im1.size(), CV_8U);
for (size_t i = 1; i < im1.rows-1; i++)
{
for (size_t j = 1; j < im1.cols-1; j++)
{
double minVal, maxVal = 0;
minMaxIdx(im1(Rect(j - 1, i - 1, 3, 3)), &minVal, &maxVal);
im2.at<uchar>(i, j) = maxVal;
}
}
imshow("(1) max bluring", im2);
medianBlur(im2, im2, 3);
imshow("(2) median bluring", im2);
im2.copyTo(im1);
im2 = Mat::zeros(im1.size(), CV_8U);
for (size_t i = 1; i < im1.rows - 1; i++)
{
for (size_t j = 1; j < im1.cols - 1; j++)
{
double minVal, maxVal = 0;
minMaxIdx(im1(Rect(j - 1, i - 1, 3, 3)), &minVal, &maxVal);
im2.at<uchar>(i, j) = minVal;
}
}
imshow("(3) min bluring", im2);
Mat tmp;
double st = threshold(im2, tmp, 10, 255, THRESH_OTSU);
threshold(im2, im2, st + 14, 255, THRESH_BINARY_INV);
//dilate(im2, im2, Mat::ones(3, 3, CV_8U));
imshow("(4) final", im2);
waitKey(0);
Run Code Online (Sandbox Code Playgroud)
顺便说一下,在这种情况下,像 YOLO 和 RCNN 这样的深度学习方法是最好的方法。也试试吧。
这是我的解决方案,
首先我得到了背景图案(手工编辑的)。从:
之后,我创建了一个空白图像以填充图案和图像之间的差异。
img = Image.open("x.png").convert("RGBA")
pattern = Image.open("y.png").convert("RGBA")
pixels = img.load()
pixelsPattern = pattern.load()
new = Image.new("RGBA", (150, 50))
pixelNew = new.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if(pixels[i,j] != pixelsPattern[i,j]):
pixelNew[i,j] = pixels[i,j]
new.save("differences.png")
Run Code Online (Sandbox Code Playgroud)
这是差异..
结果 :
pytesseract 结果为 2041,该图像是错误的,但总体比率约为 %60。