python图像处理二值化方法

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

1. opencv 简单阈值 cv2.threshold

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

3. Otsu's 二值化

例子:

来自 : OpenCV-Python 中文教程

 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()

结果图:

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()

结果图:

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

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

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

结果图:

7.

python 图像处理中二值化方法归纳总结的更多相关文章

  1. Python实现熵值法确定权重

    本文从以下四个方面,介绍用Python实现熵值法确定权重: 一. 熵值法介绍 二. 熵值法实现 三. Python实现熵值法示例1 四. Python实现熵值法示例2 一. 熵值法介绍 熵值法是计算指 ...

  2. 深度学习最全优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)(转)

    转自: https://zhuanlan.zhihu.com/p/22252270    ycszen 另可参考: https://blog.csdn.net/llx1990rl/article/de ...

  3. 《零压力学Python》 之 第二章知识点归纳

    第二章(数字)知识点归纳 要生成非常大的数字,最简单的办法是使用幂运算符,它由两个星号( ** )组成. 如: 在Python中,整数是绝对精确的,这意味着不管它多大,加上1后都将得到一个新的值.你将 ...

  4. python排序之二冒泡排序法

    python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...

  5. Python图像处理库:Pillow 初级教程

    Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...

  6. 使用Python,字标注及最大熵法进行中文分词

    使用Python,字标注及最大熵法进行中文分词 在前面的博文中使用python实现了基于词典及匹配的中文分词,这里介绍另外一种方法, 这种方法基于字标注法,并且基于最大熵法,使用机器学习方法进行训练, ...

  7. Python图像处理之验证码识别

      在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract ...

  8. Python的生成器进阶玩法

    Python的生成器进阶玩法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.yield的表达式形式 #!/usr/bin/env python #_*_coding:utf-8 ...

  9. Python字典按值排序的方法

    Python字典按值排序的方法: 法1: (默认升序排序,加  reverse = True 指定为降序排序) # sorted的结果是一个list dic1SortList = sorted( di ...

随机推荐

  1. 树TreeView控件与DataTable交互添加节点(最高效的方法)

    #region "读取树结点从Datatable" /// <summary> /// 读取树结点从Datatable" /// </summary&g ...

  2. 透明的LISTVIEW

    .NET就是封装的太密了,有时很多时候让我们反而更麻烦,特别是COPY不到的时候,又不懂自已想的话,说土一点就是死路一条, 记得以前经常用一句话,C++支持,可C#他不支持啊!就这样安慰自已 其实做多 ...

  3. [CSP-S模拟测试]:凤凰院凶真(LCIS)

    题目描述 $\alpha$世界线.凤凰院凶真创立了反抗$SERN$统治的组织“瓦尔基里”.为了脱离$\alpha$线,他需要制作一个世界线变动率测量仪.测量一个世界线相对于另一个世界线的变动率,实质上 ...

  4. OpenCV笔记:pyrDown()函数和pryUp()函数的使用

    OpenCV实现了用于创建图像金字塔的两个函数pyrDown()和pryUp(). 图像金字塔是一种经典的图像多尺寸描述方法,它将降采样和平滑滤波结合在一起,对图像进行多尺度表示.图像金字塔由不同尺寸 ...

  5. C#如何获取系统downloads和documents路径

    https://stackoverflow.com/questions/7672774/how-do-i-determine-the-windows-download-folder-path 如果你通 ...

  6. JSON字符串格式化为JSON对象

    根据项目需要,需要对json格式的字符串格式化为json对象,以下是解决方法: 参考文章:https://www.cnblogs.com/cailijuan/p/10150918.html

  7. Flask 重定向到动态url

    url_for() 函数是动态构建一个网址给特定的功能是非常有用的.该函数接受函数的名称作为第一个参数,并接受一个或多个关键字参数,每个参数对应于URL的变量部分. 以下脚本演示了使用 url_for ...

  8. canvas绘制加载特效

    css样式: body{ text-align: center; } canvas{ background: #ddd; } canvas标签: <canvas id="canvas& ...

  9. 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")

    页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...

  10. Ajax局部刷新(使用JS操作)

    对于在不使用Ajax的情况下,使用JS来进行局部刷新,主要有如下的几步: 1. 得到XMLHttpRequest 2. 使用open方法打开连接 3. 设置请求头信息 4. 注册onreadystat ...