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 相关文章推荐
tornado捕获和处理404错误的方法
Feb 26 Python
在Python中使用dict和set方法的教程
Apr 27 Python
Python教程之全局变量用法
Jun 27 Python
浅谈Python的垃圾回收机制
Dec 17 Python
批量将ppt转换为pdf的Python代码 只要27行!
Feb 26 Python
在Python中实现替换字符串中的子串的示例
Oct 31 Python
Python中的枚举类型示例介绍
Jan 09 Python
基于python实现KNN分类算法
Apr 23 Python
Python实现的排列组合、破解密码算法示例
Apr 12 Python
一篇文章弄懂Python中所有数组数据类型
Jun 23 Python
Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加签验签
Dec 04 Python
Python的三个重要函数详解
Jan 18 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通用防注入程序 推荐
2011/02/26 PHP
Session保存到数据库的php类分享
2011/10/24 PHP
php实现图片转换成ASCII码的方法
2015/04/03 PHP
php关闭warning问题的解决方法
2016/05/17 PHP
php使用ftp实现文件上传与下载功能
2017/07/21 PHP
总结PHP代码规范、流程规范、git规范
2018/06/18 PHP
jQuery根据纬度经度查看地图处理程序
2013/05/08 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
JS判断页面是否出现滚动条的方法
2015/07/17 Javascript
javascript RegExp 使用说明
2016/05/21 Javascript
jQuery封装的屏幕居中提示信息代码
2016/06/08 Javascript
js实现5秒倒计时重新发送短信功能
2017/02/05 Javascript
ES6下子组件调用父组件的方法(推荐)
2018/02/23 Javascript
jQuery.extend 与 jQuery.fn.extend的用法及区别实例分析
2018/07/25 jQuery
Vue.js单向绑定和双向绑定实例分析
2018/08/14 Javascript
js时间转换毫秒的实例代码
2019/08/21 Javascript
js实现简易计算器功能
2019/10/18 Javascript
[36:17]DOTA2上海特级锦标赛 - VGL音乐会全集
2016/03/06 DOTA
Python网络编程之TCP套接字简单用法示例
2018/04/09 Python
python线程中的同步问题及解决方法
2019/08/29 Python
Python3批量创建Crowd用户并分配组
2020/05/20 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
Python reversed反转序列并生成可迭代对象
2020/10/22 Python
10分钟理解CSS3 Grid布局
2018/12/20 HTML / CSS
马来西亚银饰品牌:JEOEL
2017/12/15 全球购物
古驰英国官网:GUCCI英国
2020/03/07 全球购物
法国低价在线宠物商店:bitiba.fr
2020/07/03 全球购物
表彰大会主持词
2014/03/26 职场文书
春秋淹城导游词
2015/02/11 职场文书
自主招生自荐信范文
2015/03/04 职场文书
月考总结与反思
2015/10/22 职场文书
《植物妈妈有办法》教学反思
2016/02/23 职场文书
新学期小学班主任工作计划
2019/06/21 职场文书
Redis基于Bitmap实现用户签到功能
2021/06/20 Redis
MySQL系列之十 MySQL事务隔离实现并发控制
2021/07/02 MySQL
golang三种设计模式之简单工厂、方法工厂和抽象工厂
2022/04/10 Golang