gpt4 book ai didi

python - 使用 PYTHON PIL 从验证码图像中去除背景噪声线

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:01 24 4
gpt4 key购买 nike

我有一个经过处理的验证码图像(放大)看起来像:
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)可读图像。

最佳答案

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

这是假设示例图像已被放大,并且线条只有一个像素宽。

关于python - 使用 PYTHON PIL 从验证码图像中去除背景噪声线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com