macOS python3 opencv 驗證碼干擾線 直線 和字元寬度不相等
阿新 • • 發佈:2018-11-13
驗證碼 去除干擾線 直線和字元寬度不相等
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鄰域演算法圖
參考: