使用PYTHON PIL从Captcha图像中去除背景噪线

4
我有一张处理过的验证码图片(放大后)如下:
captcha 如您所见,“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)识别。


你有没有找到一种去除噪音的算法?这正是我现在所需要的。 - zindarod
3个回答

1
为了快速消除大部分线条,您可以将所有只有一个或两个相邻黑色像素的黑色像素变成白色。这样应该就能解决偏离的线条问题。然后,当您有许多“块”时,可以删除较小的块。
这是在假设示例图像已经被放大,并且线条只有一个像素宽的情况下。

0

您可以使用自己的膨胀和腐蚀函数,以去除最小的线条。这里可以找到一个不错的实现


0

我个人使用膨胀和腐蚀,结合基本的宽度和高度统计学方法,尝试找到异常值并根据需要消除这些线条。之后,使用一个滤波器,取一个核的最小值,并将中心像素设置为临时图像中的颜色(在旧图像上迭代),然后使用临时图像作为原始图像即可。在pillow/PIL中,基于最小值的任务可以通过img.filter(ImageFilter.MINFILTER)来完成。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接