加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python图像去除干扰线

(2018-07-30 13:06:27)
分类: python开发
# coding = utf-8
f r o m P I L i m p o r t  I m a g e
f r  o m s q l a l c h e m  y . s q   l . e x p r e s s i o n i m p o r t e x c e p t _

# 打开该路径下的图片
def openPicture(url):
    image = Image.open("C:\\Users\\gg\\Desktop\\python_picture\\pic" + str(url) + ".png","wb")
    imgry = image.convert('L')  # 转化为灰度图
    table = get_bin_table()
    out = imgry.point(table, '1')
    out.save("C:\\Users\\gg\\Desktop\\gray_picture\\pic" + str(url) + ".png")
   

   
    # threshold 是关于原始图像的颜色识别相关内容
def get_bin_table(threshold=255):
    """
          获取灰度转二值的映射table
    """
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

# 干扰线特点分析


"""
干扰横线:像素上下位置像素为空白
干扰竖线:像素左右位像素为空白
干扰斜线或干扰点:像素上下左右像素为空白
"""


def plx(data, address):
    w = data.width
    h = data.height

    for x in range(1, w - 1):
        if x > 1 and x != w - 2:
            # 获取目标像素点左右位置
            left = x - 1
            right = x + 1
        for y in range(1, h - 1):
                # 获取目标像素点上下位置
            up = y - 1
            down = y + 1
               
            if x <= 2 or x >= (w - 2):
                data.putpixel((x, y), 255)
            elif y <= 2 or y >= (h - 2):
                data.putpixel((x, y), 255)
            elif data.getpixel((x, y)) == 0:
                if y > 1 and y != h - 1:
                    # 以目标像素点为中心店,获取周围像素点颜色
                     # 0为黑色  255为白色
                    up_color = data.getpixel((x, up))
                    down_color = data.getpixel((x, down))
                    left_color = data.getpixel((left, y))
                    left_down_color = data.getpixel((left, down))
                    right_color = data.getpixel((right, y))
                    right_up_color = data.getpixel((right, up))
                    right_down_color = data.getpixel((right, down))
                       
                    # 去除竖线干扰
                    if down_color == 0:
                        if left_color == 255 and left_down_color == 255 and right_color == 255 and right_down_color == 255:
                            data.putpixel((x, y), 255)
                           
                    # 去除横干扰线
                    elif right_color == 0:
                        if down_color == 255 and right_down_color == 255 and up_color == 255 and right_up_color == 255:
                            data.putpixel((x, y), 255)
                               
                        # 去除斜干扰线
                if left_color == 255 and right_color == 255 \
                    and up_color == 255 and down_color == 255:
                    data.putpixel((x, y), 255)
            else :
                pass
               
            # 保存去除干扰线后的图片
            data.save(address, "png")
   
   
if __name__ == "__main__":
    for i in range(1000):
       # openPicture(i)
       image = Image.open(r"C:\Users\gg\Desktop\gray_picture\pic" + str(i) + ".png")
       try: 
           plx(image,r"C:\Users\gg\Desktop\gray_final_picture\pic" + str(i) + ".png")
       except :
            pass

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有