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. Log4d:Error:Could not instantiate class[com.mapgis.util.tools.JDBCExtAppender]

    https://blog.csdn.net/gikieng/article/details/47150567  https://blog.alswl.com/2018/03/sql-server-mi ...

  2. shell脚本一一项目3

    主题:批量创建100个用户并设置密码 脚本内容 user_list=$@user_file=./user.infofor USER in ${user_list};do if ! id $USER & ...

  3. MySQL- 查询总结

    查询总结 语法: select 查询字段 from 表 别名 连接类型inner|left|right join on 连接条件 where 筛选 group by 分组列表 having 筛选(二次 ...

  4. Java thread(2)

    这一块主要是从Thread类源码的角度来分析两种线程的实现方式,这里分析的也仅仅是最基本的部分. 就从线程的启动函数 start方法开始分析 只是分析最主要的部分 在start()方法中,除了grou ...

  5. JQ获取当前根目录

    function getRootPath_web() {            //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp  ...

  6. ichunqiu在线挑战--我很简单,请不要欺负我 writeup

    挑战链接: http://www.ichunqiu.com/tiaozhan/114 知识点: 后台目录扫描,SQL Injection,一句话木马, 提权,登陆密码破解 这个挑战是为像我这种从来都没 ...

  7. UI自动化处理文件上传

    UI自动化处理文件上传 import win32guiimport win32con def set_uploader(self, file_path): sleep(2) self.file_pat ...

  8. mybatis开发注意事项:字段名称以及表名

    在使用mybatis开发中,数据库设计的时候字段名称最好不要带下划线,推荐使用驼峰命名法 数据表的名称第一个字母大写

  9. JS事件循环,MACRO TASK,MICRO TASK

    事件循环的基本概念 JS执行的过程中,由JS引擎控制的函数调用栈来控制时间循环 定时器线程,事件触发线程,异步http请求线程控制异步的任务队列 任务分为macro task,micro task 对 ...

  10. 构建自己的AngularJS,第一部分:作用域和digest 转摘:http://www.ituring.com.cn/article/39865

    构建自己的AngularJS,第一部分:Scope和Digest 原文链接:http://teropa.info/blog/2013/11/03/make-your-own-angular-part- ...