python - 分割验证码图像中的字母

标签 python image-processing captcha image-segmentation scikit-image

我用 Python 编写了这个算法,用于使用 scikit-image 读取验证码:

from skimage.color import rgb2gray
from skimage import io

def process(self, image):
    """
    Processes a CAPTCHA by removing noise

    Args:
        image (str): The file path of the image to process
    """

    input = io.imread(image)
    histogram = {}

    for x in range(input.shape[0]):
        for y in range(input.shape[1]):
            pixel = input[x, y]
            hex = '%02x%02x%02x' % (pixel[0], pixel[1], pixel[2])

            if hex in histogram:
                histogram[hex] += 1
            else:
                histogram[hex] = 1

    histogram = sorted(histogram, key = histogram.get, reverse=True)
    threshold = len(histogram) * 0.015

    for x in range(input.shape[0]):
        for y in range(input.shape[1]):
            pixel = input[x, y]
            hex = '%02x%02x%02x' % (pixel[0], pixel[1], pixel[2])
            index = histogram.index(hex)

            if index < 3 or index > threshold:
                input[x, y] = [255, 255, 255, 255]

    input = rgb2gray(~input)
    io.imsave(image, input)

之前:

Before

之后:

After

它工作得相当好,在通过 Google 的 Tesseract OCR 运行它后我得到了不错的结果,但我想让它变得更好。我认为将字母拉直会产生更好的结果。我的问题是我该怎么做?

我知道我需要以某种方式将字母装箱,如下所示:

Boxed

然后,对于每个字符,根据垂直线或水平线将其旋转一定度数。

我最初的想法是确定一个字符的中心(可能是通过在直方图中找到最常用颜色的簇)然后扩展一个框直到它找到黑色,但同样,我不太确定如何去做这样做。

为了达到这个结果,图像分割中使用了哪些常见做法?

编辑:

最后,进一步改进颜色过滤器并将 Tesseract 限制为仅字符产生了几乎 100% 准确的结果,没有任何偏差校正。

最佳答案

你想做的操作在计算机视觉技术上被称为对象的校正,为此你必须对对象应用几何变换,我有一段代码来对对象应用校正(二进制)。这是代码(使用 opencv 库):

def deskew(image, width):
    (h, w) = image.shape[:2]
    moments = cv2.moments(image)
    skew = moments["mu11"] / moments["mu02"]
    M = np.float32([[1, skew, -0.5 * w * skew],[0, 1, 0]])
    image = cv2.warpAffine(image, M, (w, h), flags = cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) 
    return image

关于python - 分割验证码图像中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294595/

相关文章:

Python-pandas - 日期时间索引 : What is the mosty pythonic strategy to analyse rolling with steps? (例如每天的某些时间)

python - 结合python中的几种结构类型

python - Pandas :比日期小的最大索引

c++ - Halide 编程语言入门?

python - 如何在图像中找到签名?

image-processing - 使图像中的文本更薄以用于 OCR

python - 当数据帧包含混合数据类型时,Pyarrow from_pandas 会使解释器崩溃

captcha - 更难、更好、更快、更强……基于图像的验证码技术?

captcha - reCAPTCHA 与其他验证码系统

python - 使用 Google AppEngine 验证码