Python图片验证码降噪和8邻域降噪


Posted in Python onAugust 30, 2021

Python图片验证码降噪 和8邻域降噪

一、简介

图片验证码识别的可以分为几个步骤,一般用 Pillow 库或 OpenCV 来实现:

1.灰度处理&二值化
2.降噪
3.字符分割
4.标准化
5.识别

所谓降噪就是把不需要的信息通通去除,比如背景,干扰线,干扰像素等等,只留下需要识别的字符,让图片变成2进制点阵,方便代入模型训练。

二、8邻域降噪

8邻域降噪 的前提是将图片灰度化,即将彩色图像转化为灰度图像。以RGN色彩空间为例,彩色图像中每个像素的颜色由R 、G、B三个分量决定,每个分量由0到255种取值,这个一个像素点可以有一千多万种颜色变化。而灰度则是将三个分量转化成一个,使每个像素点只有0-255种取值,这样可以使后续的图像计算量变得少一些。

Python图片验证码降噪和8邻域降噪

以上面的灰度图片为例,图片越接近白色的点像素越接近255,越接近黑色的点像素越接近0,而且验证码字符肯定是非白色的。对于其中噪点大部分都是孤立的小点的,而且字符都是串联在一起的。8邻域降噪 的原理就是依次遍历图中所有非白色的点,计算其周围8个点中属于非白色点的个数,如果数量小于一个固定值,那么这个点就是噪点。对于不同类型的验证码这个阈值是不同的,所以可以在程序中配置,不断尝试找到最佳的阈值。

经过测试8邻域降噪 对于小的噪点的去除是很有效的,而且计算量不大,下图是阈值设置为4去噪后的结果:

Python图片验证码降噪和8邻域降噪

三、Pillow实现

下面是使用 Pillow 模块的实现代码:

from PIL import Image


def noise_remove_pil(image_name, k):
    """
    8邻域降噪
    Args:
        image_name: 图片文件命名
        k: 判断阈值

    Returns:

    """

    def calculate_noise_count(img_obj, w, h):
        """
        计算邻域非白色的个数
        Args:
            img_obj: img obj
            w: width
            h: height
        Returns:
            count (int)
        """
        count = 0
        width, height = img_obj.size
        for _w_ in [w - 1, w, w + 1]:
            for _h_ in [h - 1, h, h + 1]:
                if _w_ > width - 1:
                    continue
                if _h_ > height - 1:
                    continue
                if _w_ == w and _h_ == h:
                    continue
                if img_obj.getpixel((_w_, _h_)) < 230:  # 这里因为是灰度图像,设置小于230为非白色
                    count += 1
        return count

    img = Image.open(image_name)
    # 灰度
    gray_img = img.convert('L')

    w, h = gray_img.size
    for _w in range(w):
        for _h in range(h):
            if _w == 0 or _h == 0:
                gray_img.putpixel((_w, _h), 255)
                continue
            # 计算邻域非白色的个数
            pixel = gray_img.getpixel((_w, _h))
            if pixel == 255:
                continue

            if calculate_noise_count(gray_img, _w, _h) < k:
                gray_img.putpixel((_w, _h), 255)
    return gray_img


if __name__ == '__main__':
    image = noise_remove_pil("test.jpg", 4)
    image.show()

四、OpenCV实现

使用OpenCV可以提高计算效率:

import cv2


def noise_remove_cv2(image_name, k):
    """
    8邻域降噪
    Args:
        image_name: 图片文件命名
        k: 判断阈值

    Returns:

    """

    def calculate_noise_count(img_obj, w, h):
        """
        计算邻域非白色的个数
        Args:
            img_obj: img obj
            w: width
            h: height
        Returns:
            count (int)
        """
        count = 0
        width, height = img_obj.shape
        for _w_ in [w - 1, w, w + 1]:
            for _h_ in [h - 1, h, h + 1]:
                if _w_ > width - 1:
                    continue
                if _h_ > height - 1:
                    continue
                if _w_ == w and _h_ == h:
                    continue
                if img_obj[_w_, _h_] < 230:  # 二值化的图片设置为255
                    count += 1
        return count

    img = cv2.imread(image_name, 1)
    # 灰度
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    w, h = gray_img.shape
    for _w in range(w):
        for _h in range(h):
            if _w == 0 or _h == 0:
                gray_img[_w, _h] = 255
                continue
            # 计算邻域pixel值小于255的个数
            pixel = gray_img[_w, _h]
            if pixel == 255:
                continue

            if calculate_noise_count(gray_img, _w, _h) < k:
                gray_img[_w, _h] = 255

    return gray_img


if __name__ == '__main__':
    image = noise_remove_cv2("test.jpg", 4)
    cv2.imshow('img', image)
    cv2.waitKey(10000)

到此这篇关于Python图片验证码降噪和8邻域降噪的文章就介绍到这了,更多相关Python验证码降噪和8邻域降噪内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python open()文件处理使用介绍
Nov 30 Python
python脚本内运行linux命令的方法
Jul 02 Python
numpy添加新的维度:newaxis的方法
Aug 02 Python
python实现机器学习之多元线性回归
Sep 06 Python
pandas筛选某列出现编码错误的解决方法
Nov 07 Python
pandas 缺失值与空值处理的实现方法
Oct 12 Python
OpenCV python sklearn随机超参数搜索的实现
Jan 17 Python
python属于哪种语言
Aug 16 Python
python中的垃圾回收(GC)机制
Sep 21 Python
浅谈anaconda python 版本对应关系
Oct 07 Python
Python3读写ini配置文件的示例
Nov 06 Python
Python字符串常规操作小结
Apr 03 Python
Python音乐爬虫完美绕过反爬
Aug 30 #Python
详解解Django 多对多表关系的三种创建方式
Aug 23 #Python
一些让Python代码简洁的实用技巧总结
Aug 23 #Python
一篇文章搞懂python混乱的切换操作与优雅的推导式
Aug 23 #Python
Python学习开发之图形用户界面详解
Aug 23 #Python
利用Python读取微信朋友圈的多种方法总结
Aug 23 #Python
教你使用一行Python代码玩遍童年的小游戏
You might like
PHP中cookies使用指南
2007/03/16 PHP
php设计模式 Decorator(装饰模式)
2011/06/26 PHP
php遍历目录方法小结
2015/03/10 PHP
PHP输入输出流学习笔记
2015/05/12 PHP
PHP单例模式简单用法示例
2017/06/23 PHP
解决laravel5.4下的group by报错的问题
2019/10/16 PHP
jquery的extend和fn.extend的使用说明
2011/01/09 Javascript
jquery操作select详解(取值,设置选中)
2014/02/07 Javascript
跟我学Node.js(四)---Node.js的模块载入方式与机制
2014/06/04 Javascript
Jquery异步提交表单代码分享
2015/03/26 Javascript
javascript学习小结之prototype
2015/12/03 Javascript
关于Jquery中的bind(),on()绑定事件方式总结
2016/10/26 Javascript
JavaScript评论点赞功能的实现方法
2017/03/13 Javascript
Angularjs中数据绑定的实例详解
2017/08/25 Javascript
用JS编写一个函数,返回数组中重复出现过的元素(实例)
2017/09/14 Javascript
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
React+TypeScript+webpack4多入口配置详解
2019/08/08 Javascript
webpack是如何实现模块化加载的方法
2019/11/06 Javascript
跟老齐学Python之从if开始语句的征程
2014/09/14 Python
简单介绍Python中的filter和lambda函数的使用
2015/04/07 Python
为Python的web框架编写MVC配置来使其运行的教程
2015/04/30 Python
在Python中使用PIL模块对图片进行高斯模糊处理的教程
2015/05/05 Python
python fabric实现远程部署
2017/01/05 Python
使用Python对SQLite数据库操作
2017/04/06 Python
Python数据分析matplotlib设置多个子图的间距方法
2018/08/03 Python
python实时获取外部程序输出结果的方法
2019/01/12 Python
Win10 安装PyCharm2019.1.1(图文教程)
2019/09/29 Python
关于pytorch中全连接神经网络搭建两种模式详解
2020/01/14 Python
pytorch实现保证每次运行使用的随机数都相同
2020/02/20 Python
python新手学习可变和不可变对象
2020/06/11 Python
美国珠宝网上商店:Jeulia
2016/09/01 全球购物
初中三年学生的学习自我评价
2013/11/13 职场文书
小区门卫的岗位职责
2014/09/26 职场文书
离婚协议书应该怎么写
2014/10/12 职场文书
配置nginx 重定向到系统维护页面
2021/06/08 Servers
MySql 8.0及对应驱动包匹配的注意点说明
2021/06/23 MySQL