通过复制 TesserCap 的斩波滤波器去除验证码图像的背景噪声

2023-12-31

我有一个验证码图像,如下所示:

使用名为的实用程序泰瑟帽 http://www.mcafee.com/us/downloads/free-tools/tessercap.aspx通过 McAfee,我可以对图像应用“斩波”滤镜。 (在运行之前,我确保图像中只有两种颜色:白色和黑色。)使用文本框中值为 2 的过滤器的结果给我留下了深刻的印象。它准确地消除了大部分噪音,但保留了主要文本,结果是:

我想在自己的一个脚本上实现这样的东西,所以我试图找出 TesserCap 使用的图像处理库。我什么也没找到;事实证明它使用自己的代码来处理图像。然后我读了本白皮书 http://www.mcafee.com/us/resources/white-papers/foundstone/wp-tessercap-visual-captcha-tool.pdf这准确地解释了该程序是如何工作的。它为我提供了有关此斩波滤波器功能的以下描述:

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

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

(This is made from a slightly different captcha that uses a circular pattern.)

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

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


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

例如,在像素的示例行中,其中#是黑色的并且-是白色的,应用斩波因子2会转变--#--###-##---#####---#-# into ------###-------#####-------。这是因为存在小于或等于2个像素的黑色像素序列,并且这些序列被替换为白色。保留大于 2 个像素的连续序列。

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

为了将其应用于整个图像,您只需在每一行和每一列上执行此算法即可。下面是完成该任务的 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])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过复制 TesserCap 的斩波滤波器去除验证码图像的背景噪声 的相关文章