[英]Cleaning up captcha image
我正在嘗試清理上面的圖像我已經嘗試了幾種使用 open cv 的不同方法,我要么過度侵蝕原始圖像,以至於部分字母丟失,如下所示:
我不太確定如何去掉最后一條對角線並修復 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')
任何幫助將不勝感激,這里有更多由驗證碼生成的圖像類型的示例;
也是包含圖像的文件夾的鏈接
這是一個使用OpenCvSharp的 C# 解決方案(它應該很容易轉換回 python/c++,因為方法名稱完全相同)。
它使用 OpenCV 的修復技術來避免在可能運行 OCR 階段之前破壞太多字母。 我們可以看到線條的顏色與其他線條不同,因此我們將在任何灰度/黑白色之前盡早使用該信息。 步驟如下:
這是面具:
結果如下:
這是樣本集的結果:
這是 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
,灰塵在112
。
一個簡單的灰度過濾在這里會有很大幫助。
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)
現在使用cv2.dilate
( cv2.erode
on a white on black text) 去除剩余的灰塵。
這不是一個非常強大的解決方案,但在大多數情況下可能會有所幫助:
通過查看上面張貼的圖像樣本,我可以觀察到對角線的一個共同特征,它們在圖像邊緣開始或結束,而我們感興趣的文本位於中間,因此我們可以通過這種方式確定通過在圖像矩陣的前幾行和最后幾列中搜索這些對角線並將它們作為噪聲消除。 而且這種方法也可能花費更少的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.