【问题标题】:Removing background noisy lines from Captcha Image using PYTHON PIL使用 PYTHON PIL 从验证码图像中去除背景噪声线
【发布时间】:2013-02-25 12:06:41
【问题描述】:

我有一张经过处理的验证码图像(放大),看起来像:

如您所见,“TEXT”的字体大小略大于嘈杂线的宽度。
所以我需要一种算法或代码来去除这张图片中的嘈杂线条。

借助 Python PIL 库和下面提到的斩波算法,我无法获得 OCR 可以轻松读取的输出图像。

这是我尝试过的 Python 代码:

import PIL.Image
import sys

# python chop.py [chop-factor] [in-file] [out-file]

chop = int(sys.argv[1])
image = PIL.Image.open(sys.argv[2]).convert('1')
width, height = image.size
data = image.load()

# Iterate through the rows.
for y in range(height):
    for x in range(width):

        # Make sure we're on a dark pixel.
        if data[x, y] > 128:
            continue

        # Keep a total of non-white contiguous pixels.
        total = 0

        # Check a sequence ranging from x to image.width.
        for c in range(x, width):

            # If the pixel is dark, add it to the total.
            if data[c, y] < 128:
                total += 1

            # If the pixel is light, stop the sequence.
            else:
                break

        # If the total is less than the chop, replace everything with white.
        if total <= chop:
            for c in range(total):
                data[x + c, y] = 255

        # Skip this sequence we just altered.
        x += total


# Iterate through the columns.
for x in range(width):
    for y in range(height):

        # Make sure we're on a dark pixel.
        if data[x, y] > 128:
            continue

        # Keep a total of non-white contiguous pixels.
        total = 0

        # Check a sequence ranging from y to image.height.
        for c in range(y, height):
            # If the pixel is dark, add it to the total.
            if data[x, c] < 128:
                total += 1

            # If the pixel is light, stop the sequence.
            else:
                break

        # If the total is less than the chop, replace everything with white.
        if total <= chop:
            for c in range(total):
                data[x, y + c] = 255

        # Skip this sequence we just altered.
        y += total

image.save(sys.argv[3])

所以,基本上我想知道一种更好的算法/代码来消除噪音,从而使图像能够被 OCR(Tesseract 或 pytesser)读取。

【问题讨论】:

  • 你有没有找到去除噪音的算法?这正是我现在所需要的。

标签: python algorithm image-processing python-imaging-library captcha


【解决方案1】:

要快速消除大部分线条,您可以将具有两个或更少相邻黑色像素的所有黑色像素变为白色。那应该可以修复杂散线。然后,当您有很多“块”时,您可以移除较小的。

这是假设样本图像已被放大,线条只有一个像素宽。

【讨论】:

    【解决方案2】:

    您可以使用自己的扩张和侵蚀功能,这将删除最小的线条。可以在here 找到一个不错的实现。

    【讨论】:

      【解决方案3】:

      我个人使用如上所述的扩张和腐蚀,但将其与宽度和高度的一些基本统计数据结合起来,尝试找出异常值并根据需要消除这些线条。之后,在将临时图像用作原始图像之前,采用内核的最小值并在临时图像中转换该颜色的中心像素(向下迭代旧图像)的过滤器应该可以工作。在枕头/PIL 中,基于最小值的任务是通过 img.filter(ImageFilter.MINFILTER) 完成的。

      如果这还不够,它应该生成一个可识别的集合,OpenCV 的轮廓和最小边界旋转框可用于旋转一个字母以进行比较(此时我推荐 Tesseract 或商业 OCR,因为它们有大量字体和额外的功能,如集群和清理)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 2012-04-27
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多