macOS python3 opencv 验证码干扰线 直线 和字符宽度不相等

版权声明:本文为博主原创文章,可以自由转载。 https://blog.csdn.net/u010953692/article/details/83996200

验证码 去除干扰线 直线和字符宽度不相等

1,8邻域算法

#! /usr/local/bin/python3
# coding:utf-8

from PIL import Image
import pytesseract

"""
p1 = Image.open("/root/8069.jpg")
text = pytesseract.image_to_string(p1)
print (text)
"""

img = Image.open("/root/8069line.jpg")
#img.show()

img = img.convert("L")
img.show()
pixdata = img.load()
w ,h = img.size
for y in range(h):
    for x in range(w):
        if pixdata[x,y] < 180:
            pixdata[x,y] = 0
        else:
            pixdata[x,y] = 255
img.show()

# 对二值化图片降噪
pixdata = img.load()
w,h = img.size
# 8邻域算法
for y in range(1,h-1):
    for x in range(1,w-1):
        count = 0
        if pixdata[x,y-1] > 245:#上
            count = count + 1
        if pixdata[x,y+1] > 245:#下
            count = count + 1
        if pixdata[x-1,y] > 245:#左
            count = count + 1
        if pixdata[x+1,y] > 245:#右
            count = count + 1
        if pixdata[x-1,y-1] > 245:#左上
            count = count + 1
        if pixdata[x-1,y+1] > 245:#左下
            count = count + 1
        if pixdata[x+1,y-1] > 245:#右上
            count = count + 1
        if pixdata[x+1,y+1] > 245:#右下
            count = count + 1
        if count > 4:
            pixdata[x,y] = 255

img.show()


  • 原图
    在这里插入图片描述
  • 二值化图
    在这里插入图片描述
  • 8邻域算法图
    在这里插入图片描述

参考:

  1. python验证码识别1:灰度处理、二值化、降噪、tesserocr识别
  2. PIL验证码图片预处理 4邻域像素算法

猜你喜欢

转载自blog.csdn.net/u010953692/article/details/83996200