10

我有一个看起来像这样的验证码图像:

使用 McAfee 的名为TesserCap的实用程序,我可以对图像应用“斩波”过滤器。(在运行它之前,我确保图像中只有两种颜色,白色和黑色。)在文本框中使用值为 2 的过滤器的结果给我留下了深刻的印象。它准确地去除了大部分噪音,但保留了正文,结果如下:

我想在我自己的一个脚本上实现这样的东西,所以我试图找出 TesserCap 使用的图像处理库。我什么也找不到;事实证明它使用自己的代码来处理图像。然后我阅读了这份白皮书,该白皮书准确地解释了该程序的工作原理。它给了我关于这个斩波滤波器的以下描述:

如果给定灰度值的连续像素数小于数字框中提供的数量,则斩波滤波器会根据用户选择将这些序列替换为 0(黑色)或 255(白色)。CAPTCHA 在水平和垂直方向上进行分析,并进行相应的更改。

我不确定我是否理解它在做什么。我的脚本是用 Python 编写的,所以我尝试使用 PIL 来操作类似于引用所描述的像素。这听起来很简单,但我失败了,可能是因为我真的不知道过滤器到底在做什么:

(这是由使用圆形图案的稍微不同的验证码制成的。)

我还尝试查看是否可以使用 ImageMagick 的 convert.exe 轻松完成。他们的 -chop 选项完全不同。使用 -median 和一些 -morphology 命令有助于减少一些噪音,但出现了令人讨厌的点并且字母变得非常扭曲。这并不像使用 TesserCap 进行斩波滤波器那么简单。

所以,我的问题如下:如何在 Python 中实现 TesserCap 的斩波过滤器,无论是使用 PIL 还是 ImageMagick?该斩波过滤器比我尝试过的任何替代品都好得多,但我似乎无法复制它。我已经为此工作了几个小时,但还没有弄清楚任何事情。

4

2 回答 2

10

该算法本质上检查一行中是否有多个目标像素(在本例中为非白色像素),如果像素数小于或等于斩波因子,则更改这些像素。

例如,在像素的样本行中,其中#是黑色和-白色,应用斩波因子2将转换--#--###-##---#####---#-#------###-------#####-------。这是因为存在小于或等于2个像素的黑色像素序列,并且这些序列被替换为白色。大于 2 个像素的连续序列仍然存在。

这是我的 Python 代码(如下)在您帖子的原始图像上实现的斩波算法的结果:

“切碎”图像

为了将其应用于整个图像,您只需在每一行和每一列上执行此算法。这是完成此任务的 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])
于 2012-06-29T01:07:45.750 回答
3

尝试这样的事情(伪代码):

for each row of pixels:
    if there is a group of about 3 or more pixels in a row, leave them
    else remove the pixels

然后只需对列重复相同的操作。似乎它至少可以工作一点。像这样水平和垂直移动也将删除水平/垂直线。

于 2012-06-28T23:20:44.333 回答