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安装第三方库及常见问题处理方法汇总
Sep 13 Python
Python Socket使用实例
Dec 18 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
Jan 29 Python
pandas中去除指定字符的实例
May 18 Python
windows下python安装pip图文教程
May 25 Python
Python中时间datetime的处理与转换用法总结
Feb 18 Python
python隐藏终端执行cmd命令的方法
Jun 24 Python
python+numpy按行求一个二维数组的最大值方法
Jul 09 Python
django项目环境搭建及在虚拟机本地创建django项目的教程
Aug 02 Python
通过实例简单了解python yield使用方法
Aug 06 Python
python 递归相关知识总结
Mar 03 Python
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
Apr 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封装CURL扩展类实例
2015/07/28 PHP
Yii中的relations数据关联查询及统计功能用法详解
2016/07/14 PHP
在一个form用一个SUBMIT(或button)分别提交到两个处理表单页面的代码
2007/02/15 Javascript
JS面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型模式)
2014/08/16 Javascript
jQuery表单域属性过滤器用法分析
2015/02/10 Javascript
认识Knockout及如何使用Knockout绑定上下文
2015/12/25 Javascript
jquery限定文本框只能输入数字(整数和小数)
2016/01/08 Javascript
浅析$(function) ready和onload 的区别
2016/09/03 Javascript
无阻塞加载js,防止因js加载不了影响页面显示的问题
2016/12/18 Javascript
详解angularjs 关于ui-router分层使用
2017/06/12 Javascript
微信小程序--组件(swiper)详细介绍
2017/06/13 Javascript
canvas绘制爱心的几种方法总结(推荐)
2017/10/31 Javascript
jQuery 实现左右两侧菜单添加、移除功能
2018/01/02 jQuery
详解iframe跨域的几种常用方法(小结)
2019/04/29 Javascript
jquery validate 实现动态增加/删除验证规则操作示例
2019/10/28 jQuery
js实现双人五子棋小游戏
2020/05/28 Javascript
Python使用bs4获取58同城城市分类的方法
2015/07/08 Python
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2018/05/12 NodeJs
python 图像平移和旋转的实例
2019/01/10 Python
python 字段拆分详解
2019/12/17 Python
Python3列表List入门知识附实例
2020/02/09 Python
python随机模块random的22种函数(小结)
2020/05/15 Python
Django 解决model 反向引用中的related_name问题
2020/05/19 Python
小米俄罗斯授权商店:Xiaomi俄罗斯
2019/12/08 全球购物
海量信息软件测试笔试题
2015/08/08 面试题
一份软件工程师的面试试题
2016/02/01 面试题
普通员工辞职信
2014/01/17 职场文书
函授本科个人自我鉴定
2014/03/25 职场文书
乒乓球兴趣小组活动总结
2014/07/08 职场文书
私营公司诉讼代理委托书范本
2014/09/13 职场文书
会计工作态度自我评价
2015/03/06 职场文书
2015年禁毒工作总结
2015/04/30 职场文书
2015年项目经理工作总结
2015/04/30 职场文书
民主生活会主持词
2015/07/01 职场文书
Oracle表空间与权限的深入讲解
2021/11/17 Oracle
PHP获取学生成绩的方法
2021/11/17 PHP