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的Template使用指南
Sep 11 Python
python实现的文件夹清理程序分享
Nov 22 Python
python通过函数属性实现全局变量的方法
May 16 Python
Django实现图片文字同时提交的方法
May 26 Python
python 多线程实现检测服务器在线情况
Nov 25 Python
Python3实现Web网页图片下载
Jan 28 Python
Python文件操作之合并文本文件内容示例代码
Sep 19 Python
windows下python安装pip图文教程
May 25 Python
Django ModelForm组件使用方法详解
Jul 23 Python
django之自定义软删除Model的方法
Aug 14 Python
flask框架自定义url转换器操作详解
Jan 25 Python
基于nexus3配置Python仓库过程详解
Jun 15 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
说明的比较细的php 正则学习实例
2008/07/30 PHP
php入门学习知识点五 关于php数组的几个基本操作
2011/07/14 PHP
php fsockopen中多线程问题的解决办法[翻译]
2011/11/09 PHP
解析php框架codeigniter中如何使用框架的session
2013/06/24 PHP
php禁止某ip或ip地址段访问的方法
2015/02/25 PHP
php文件上传及下载附带显示文件及目录功能
2017/04/27 PHP
Uglifyjs(JS代码优化工具)入门 安装使用
2020/04/13 Javascript
javaScript使用EL表达式的几种方式
2014/05/27 Javascript
9款2014最热门jQuery实用特效推荐
2014/12/07 Javascript
javascript中innerText和innerHTML属性用法实例分析
2015/05/13 Javascript
实例剖析AngularJS框架中数据的双向绑定运用
2016/03/04 Javascript
表格展示利器 Bootstrap Table实例代码
2017/09/06 Javascript
AngularJS实现表单验证功能详解
2017/10/12 Javascript
微信小程序实现单选选项卡切换效果
2020/06/19 Javascript
[00:12]2018DOTA2亚洲邀请赛 sylar表现SOLO技艺
2018/04/06 DOTA
Python单链表的简单实现方法
2014/09/23 Python
Python命令行参数解析模块optparse使用实例
2015/04/13 Python
python中for语句简单遍历数据的方法
2015/05/07 Python
python实现按行切分文本文件的方法
2016/04/18 Python
python对json的相关操作实例详解
2017/01/04 Python
selenium+python实现自动登录脚本
2018/04/22 Python
Python Cookie 读取和保存方法
2018/12/28 Python
Python数据可视化之画图
2019/01/15 Python
在django中使用post方法时,需要增加csrftoken的例子
2020/03/13 Python
python 对xml解析的示例
2021/02/27 Python
详解CSS3中使用gradient实现渐变效果的方法
2015/08/18 HTML / CSS
中国旅游网站:同程旅游
2016/09/11 全球购物
美国球鞋寄卖网站:Stadium Goods
2018/05/09 全球购物
介绍一下grep命令的使用
2015/06/12 面试题
解释下面关于J2EE的名词
2013/11/15 面试题
优秀员工自荐书范文
2013/12/08 职场文书
物理力学求职信
2014/02/18 职场文书
全国优秀辅导员事迹材料
2014/05/14 职场文书
税务干部个人整改措施思想汇报
2014/10/10 职场文书
2016年十一促销广告语
2016/01/28 职场文书
【海涛DOTA解说】EVE女子战队独家录像加ZSMJ神牛两连发
2022/04/01 DOTA