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基础教程之实现石头剪刀布游戏示例
Feb 11 Python
Python多进程分块读取超大文件的方法
Apr 13 Python
python进阶_浅谈面向对象进阶
Aug 17 Python
Python实现PS滤镜功能之波浪特效示例
Jan 26 Python
pandas 快速处理 date_time 日期格式方法
Nov 12 Python
python读取txt文件并取其某一列数据的示例
Feb 19 Python
python Django编写接口并用Jmeter测试的方法
Jul 31 Python
python-序列解包(对可迭代元素的快速取值方法)
Aug 24 Python
python实现数据清洗(缺失值与异常值处理)
Dec 02 Python
jenkins+python自动化测试持续集成教程
May 12 Python
Python-for循环的内部机制
Jun 12 Python
Python 调用C++封装的进一步探索交流
Mar 04 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 作用域解析运算符(::)
2010/07/27 PHP
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
php使用glob函数快速查询指定目录文件的方法
2014/11/15 PHP
PHP微信开发之查询城市天气
2016/06/23 PHP
PHP封装mysqli基于面向对象的mysql数据库操作类与用法示例
2019/02/25 PHP
JavaScript 继承的实现
2009/07/09 Javascript
js 数组的for循环到底应该怎么写?
2010/05/31 Javascript
Js放到HTML文件中的哪个位置有什么区别
2013/08/21 Javascript
JavaScript中使用stopPropagation函数停止事件传播例子
2014/08/27 Javascript
在JavaScript中访问字符串的子串
2015/07/07 Javascript
Bootstrap每天必学之表单
2015/11/23 Javascript
利用AngularJs实现京东首页轮播图效果
2016/09/08 Javascript
jQuery实现的多张图无缝滚动效果【测试可用】
2016/09/12 Javascript
Node.js下自定义错误类型详解
2016/10/17 Javascript
footer定位页面底部(代码分享)
2017/03/07 Javascript
解决vue2.x中数据渲染以及vuex缓存的问题
2017/07/13 Javascript
jQuery EasyUI 选项卡面板tabs的使用实例讲解
2017/12/25 jQuery
vuex如何重置所有state(可定制)
2019/01/17 Javascript
Python中的getopt函数使用详解
2015/07/28 Python
pandas 选择某几列的方法
2018/07/03 Python
在pycharm中设置显示行数的方法
2019/01/16 Python
python使用pandas处理excel文件转为csv文件的方法示例
2019/07/18 Python
Python的缺点和劣势分析
2019/11/19 Python
Python 实现Serial 与STM32J进行串口通讯
2019/12/18 Python
Python列表操作方法详解
2020/02/09 Python
英国和爱尔兰的自炊式豪华度假小屋:Rural Retreats
2018/06/08 全球购物
微软台湾官方网站:Microsoft台湾
2018/08/15 全球购物
伦敦剧院门票:London Theatre Direct
2018/11/21 全球购物
英国手机零售商:Metrofone
2019/03/18 全球购物
Ibatis如何调用存储过程
2015/05/15 面试题
什么是Web Service?
2012/07/25 面试题
新闻记者个人求职的自我评价
2013/11/28 职场文书
工作个人的自我评价
2014/01/14 职场文书
中专毕业生个人职业生涯规划
2014/02/19 职场文书
个人担保书格式范文
2014/05/12 职场文书
2015年卫生监督工作总结
2015/05/21 职场文书