Python图像处理二值化方法实例汇总


Posted in Python onJuly 24, 2020

在用python进行图像处理时,二值化是非常重要的一步,现总结了自己遇到过的6种 图像二值化的方法(当然这个绝对不是全部的二值化方法,若发现新的方法会继续新增)。

1. opencv 简单阈值 cv2.threshold

2. opencv 自适应阈值 cv2.adaptiveThreshold (自适应阈值中计算阈值的方法有两种:mean_c 和 guassian_c ,可以尝试用下哪种效果好)

3. Otsu's 二值化

例子:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('scratch.png', 0)
# global thresholding
ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Otsu's thresholding
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# Otsu's thresholding
# 阈值一定要设为 0 !
ret3, th3 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1, img, 0, th2, img, 0, th3]
titles = [
  'Original Noisy Image', 'Histogram', 'Global Thresholding (v=127)',
  'Original Noisy Image', 'Histogram', "Adaptive Thresholding",
  'Original Noisy Image', 'Histogram', "Otsu's Thresholding"
]
# 这里使用了 pyplot 中画直方图的方法, plt.hist, 要注意的是它的参数是一维数组
# 所以这里使用了( numpy ) ravel 方法,将多维数组转换成一维,也可以使用 flatten 方法
# ndarray.flat 1-D iterator over an array.
# ndarray.flatten 1-D array copy of the elements of an array in row-major order.
for i in range(3):
  plt.subplot(3, 3, i * 3 + 1), plt.imshow(images[i * 3], 'gray')
  plt.title(titles[i * 3]), plt.xticks([]), plt.yticks([])
  plt.subplot(3, 3, i * 3 + 2), plt.hist(images[i * 3].ravel(), 256)
  plt.title(titles[i * 3 + 1]), plt.xticks([]), plt.yticks([])
  plt.subplot(3, 3, i * 3 + 3), plt.imshow(images[i * 3 + 2], 'gray')
  plt.title(titles[i * 3 + 2]), plt.xticks([]), plt.yticks([])
plt.show()

结果图:

Python图像处理二值化方法实例汇总

4. skimage niblack阈值

5. skimage sauvola阈值 (主要用于文本检测)

例子:

https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_niblack_sauvola.html

import matplotlib
import matplotlib.pyplot as plt

from skimage.data import page
from skimage.filters import (threshold_otsu, threshold_niblack,
               threshold_sauvola)


matplotlib.rcParams['font.size'] = 9


image = page()
binary_global = image > threshold_otsu(image)

window_size = 25
thresh_niblack = threshold_niblack(image, window_size=window_size, k=0.8)
thresh_sauvola = threshold_sauvola(image, window_size=window_size)

binary_niblack = image > thresh_niblack
binary_sauvola = image > thresh_sauvola

plt.figure(figsize=(8, 7))
plt.subplot(2, 2, 1)
plt.imshow(image, cmap=plt.cm.gray)
plt.title('Original')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.title('Global Threshold')
plt.imshow(binary_global, cmap=plt.cm.gray)
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(binary_niblack, cmap=plt.cm.gray)
plt.title('Niblack Threshold')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(binary_sauvola, cmap=plt.cm.gray)
plt.title('Sauvola Threshold')
plt.axis('off')

plt.show()

结果图:

Python图像处理二值化方法实例汇总

6.IntegralThreshold(主要用于文本检测)

使用方法: 运行下面网址的util.py文件

https://github.com/Liang-yc/IntegralThreshold

结果图:

Python图像处理二值化方法实例汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用语句导入模块或包的机制研究
Mar 30 Python
几个提升Python运行效率的方法之间的对比
Apr 03 Python
python实现下载指定网址所有图片的方法
Aug 08 Python
pandas DataFrame 根据多列的值做判断,生成新的列值实例
May 18 Python
python实现单链表中删除倒数第K个节点的方法
Sep 28 Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 Python
Python OpenCV之图片缩放的实现(cv2.resize)
Jun 28 Python
Python何时应该使用Lambda函数
Jul 02 Python
python中class的定义及使用教程
Sep 18 Python
Django框架下静态模板的继承操作示例
Nov 08 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
May 16 Python
python实现b站直播自动发送弹幕功能
Feb 20 Python
Python如何合并多个字典或映射
Jul 24 #Python
Matplotlib 绘制饼图解决文字重叠的方法
Jul 24 #Python
Python3.7安装pyaudio教程解析
Jul 24 #Python
python调用私有属性的方法总结
Jul 24 #Python
python中取绝对值简单方法总结
Jul 24 #Python
python代码能做成软件吗
Jul 24 #Python
深入了解Python 变量作用域
Jul 24 #Python
You might like
超级简单的发送邮件程序
2006/10/09 PHP
使用php判断网页是否gzip压缩
2013/06/25 PHP
PHP借助phpmailer发送邮件
2015/05/11 PHP
PHP判断字符串长度的两种方法很实用
2015/09/22 PHP
微信接口生成带参数的二维码
2017/07/31 PHP
PHP getName()函数讲解
2019/02/03 PHP
php实现断点续传大文件示例代码
2020/06/19 PHP
javascript实现仿银行密码输入框效果的代码
2007/12/13 Javascript
jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
2010/04/01 Javascript
一个可拖拽列宽表格实例演示
2012/11/26 Javascript
js遍历td tr等html元素
2012/12/13 Javascript
jquery定时滑出可最小化的底部提示层特效代码
2013/10/02 Javascript
给事件响应函数传参数的四种方式小结
2013/12/05 Javascript
[原创]Javascript 实现广告后加载 可加载百度谷歌联盟广告
2016/05/11 Javascript
Javascript发送AJAX请求实例代码
2016/08/21 Javascript
JS中使用正则表达式g模式和非g模式的区别
2017/04/01 Javascript
JS获取短信验证码倒计时的实现代码
2017/05/22 Javascript
JavaScript伪数组用法实例分析
2017/12/22 Javascript
vue+elementUI动态生成面包屑导航教程
2019/11/04 Javascript
js实现动态时钟
2020/03/12 Javascript
python清理子进程机制剖析
2017/11/23 Python
python 集合 并集、交集 Series list set 转换的实例
2018/05/29 Python
python with (as)语句实例详解
2020/02/04 Python
Django REST framwork的权限验证实例
2020/04/02 Python
在python中对于bool布尔值的取反操作
2020/12/11 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
2021/01/27 Python
应用化学专业本科生求职信
2013/09/29 职场文书
英文简历中的自我评价用语
2013/12/09 职场文书
高中毕业自我鉴定
2013/12/19 职场文书
校园学雷锋活动月总结
2014/03/09 职场文书
推广普通话共筑中国梦演讲稿
2014/09/21 职场文书
社区好人好事材料
2014/12/26 职场文书
2015年银行客户经理工作总结
2015/04/01 职场文书
2015年公司国庆放假通知
2015/07/30 职场文书
python如何获取网络数据
2021/04/11 Python
MySQL 主从复制数据不一致的解决方法
2022/03/18 MySQL