python 图片验证码去噪 识别

二值化处理(图片默认在C:\Python27\Scripts文件夹下,可根据自己需求更改)

原图:


from PIL import Image  
  
# 二值化处理  
def two_value():    
        # 打开文件夹中的图片  
        image=Image.open('3.jpg')  
        # 灰度图  
        lim=image.convert('L')  
        # 灰度阈值设为165,低于这个值的点全部填白色  
        threshold=165
        table=[]  
          
        for j in range(256):  
            if j<threshold:  
                table.append(0)  
            else:  
                table.append(1)  
  
        bim=lim.point(table,'1')  
        bim.save('4.jpg')  
        
two_value()       

二值化:

对于有干扰线的图片先二值化再进行去干扰线操作

from PIL import Image  
  
# 去除干扰线  
im = Image.open('4.jpg')  
# 图像二值化  
data = im.getdata()  
w,h = im.size  
black_point = 0  
  
for x in range(1,w-1):  
    for y in range(1,h-1):  
        mid_pixel = data[w*y+x] # 中央像素点像素值  
        if mid_pixel <50: # 找出上下左右四个方向像素点像素值  
            top_pixel = data[w*(y-1)+x]  
            left_pixel = data[w*y+(x-1)]  
            down_pixel = data[w*(y+1)+x]  
            right_pixel = data[w*y+(x+1)]  
  
            # 判断上下左右的黑色像素点总个数  
            if top_pixel <10:  
                black_point += 1  
            if left_pixel <10:  
                black_point += 1  
            if down_pixel <10:  
                black_point += 1  
            if right_pixel <10:  
                black_point += 1  
            if black_point <1:  
                im.putpixel((x,y),255)  
            # print(black_point)  
            black_point = 0  
            
im.save('4.jpg')  

去噪算法(8邻域降噪)

#coding:utf-8  
import sys,os  
from PIL import Image,ImageDraw  
  
#二值判断,如果确认是噪声,用改点的上面一个点的灰度进行替换  
#该函数也可以改成RGB判断的,具体看需求如何  
def getPixel(image,x,y,G,N):  
    L = image.getpixel((x,y))  
    if L > G:  
        L = True  
    else:  
        L = False  
  
    nearDots = 0  
    if L == (image.getpixel((x - 1,y - 1)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x - 1,y)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x - 1,y + 1)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x,y - 1)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x,y + 1)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x + 1,y - 1)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x + 1,y)) > G):  
        nearDots += 1  
    if L == (image.getpixel((x + 1,y + 1)) > G):  
        nearDots += 1  
  
    if nearDots < N:  
        return image.getpixel((x,y-1))  
    else:  
        return None  
  
# 降噪   
# 根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0 <N <8),当A的RGB值与周围8个点的RGB相等数小于N时,此点为噪点   
# G: Integer 图像二值化阀值   
# N: Integer 降噪率 0 <N <8   
# Z: Integer 降噪次数   
# 输出   
#  0:降噪成功   
#  1:降噪失败   
def clearNoise(image,G,N,Z):  
    draw = ImageDraw.Draw(image)  
  
    for i in xrange(0,Z):  
        for x in xrange(1,image.size[0] - 1):  
            for y in xrange(1,image.size[1] - 1):  
                color = getPixel(image,x,y,G,N)  
                if color != None:  
                    draw.point((x,y),color)  
    
    #打开图片 
os.chdir("C:\Python27\Scripts") 
image = Image.open('5.jpg')  
  
    #将图片转换成灰度图片    
clearNoise(image,50,4,4)
src = '6.jpg'
image.save(src)
对于有干扰线的效果依旧不行,识别正确率较低。
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值