要用到的函数是cv2.threshold()

这个参数的形式是cv.Threshold(src, dst, threshold, maxValue, thresholdType)

Parameters:
  • src – input array (single-channel, 8-bit or 32-bit floating point).
  • dst – output array of the same size and type as src.
  • thresh – threshold value.
  • maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
  • type – thresholding type (see the details below).

一开始没明白thresh和maxval的区别,后来问了同学知道了,maxval就是指超过阙值的部分取maxval这个值

五种不同的type出现的效果图不一样,参考如下

tip:在官网tutorial中是for i in xrange(6),在Python3中 range和xrange已经合并成了range 。

matplotlib的一个函数plt.subplot(),可以绘制多个子图,第一个参数是行数,第二个参数是列数,第三个参数表示图形的标号。

贴上代码

import cv2
import numpy as np
from matplotlib import pyplot as pltimg = cv2.imread('gradient.png',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]for i in range(6):plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')plt.title(titles[i])plt.xticks([]),plt.yticks([])plt.show()

Adaptive Thresholding

函数形式:cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])

Parameters:
  • src – Source 8-bit single-channel image.
  • dst – Destination image of the same size and the same type as src .
  • maxValue – Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
  • adaptiveMethod – Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
  • thresholdType – Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
  • blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
  • C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well. 从计算的平均值或者加权平均值减去的一个常数

为什么每次dst这个参数都被省略了。。。

贴上代码

import cv2
import numpy as np
from matplotlib import pyplot as pltimg = cv2.imread('dave.jpg',0)
img = cv2.medianBlur(img,5)ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\cv2.THRESH_BINARY,11,2)titles = ['Original Image', 'Global Thresholding (v = 127)','Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]for i in xrange(4):plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')plt.title(titles[i])plt.xticks([]),plt.yticks([])
plt.show()

效果图

Image Thresholding图像阙值化和Adaptive Thresholding相关推荐

  1. Python使用openCV把原始彩色图像转化为灰度图、使用OpenCV把图像二值化(仅仅包含黑色和白色的简化版本)、基于自适应阈值预处理(adaptive thresholding)方法

    Python使用openCV把原始彩色图像转化为灰度图.使用OpenCV把图像二值化(仅仅包含黑色和白色的简化版本).基于自适应阈值预处理(adaptive thresholding)方法 目录

  2. C#,图像二值化(22)——局部阈值的伯恩森算法(Bernsen Thresholding)及源程序

    1.局部阈值的伯恩森算法(Bernsen Thresholding) Bernsen方法是为图像分割开发的局部自适应二值化方法之一.在这项研究中,实现了Bernsen的局部自适应二值化方法,并对不同灰 ...

  3. C#,图像二值化(21)——局部阈值的韦尔纳算法(Wellner Thresholding)及源代码

    1 韦尔纳算法(Wellner Throsholding) 摘要 针对计算大量缺陷时速度较慢且图像阈值不平滑的Wellner算法,本文提出了两种改进方案,第一种是一维平滑算法(ODSA),第二种是基于 ...

  4. C#,图像二值化(16)——全局阈值的力矩保持算法(Moment-proserving Thresholding)及其源代码

    1.力矩保持法 提出了一种基于矩保持原理的自动阈值选择方法.以这样的方式确定地计算阈值,即在输出画面中保留输入画面的时刻.实验结果表明,该方法可以将给定的图像阈值化为有意义的灰度级.该方法描述了全局阈 ...

  5. C#,图像二值化(05)——全局阈值的联高自适应算法(Legal Self-Adaptive Thresholding)及其源代码

    阈值的选择当然希望智能.简单一些.应该能应付一般的图片. What is Binarization? Binarization is the process of transforming data ...

  6. C#,图像二值化(18)——全局阈值的模糊集理论算法(Huang Thresholding)与源程序

    1 模糊集理论 模糊集理论,也称为模糊集合论,或简单地称为模糊集,1965年美国学者扎德在数学上创立了一种描述模糊现象的方法-模糊集合论.这种方法把待考察的对象及反映它的模糊概念作为一定的模糊集合,建 ...

  7. C#,图像二值化(06)——全局阈值的大津算法(OTSU Thresholding)及其源代码

    1.大津算法OTSU ALGORITHM OTSU算法效果很一般. 最大类间方差法是1979年由日本学者大津(Nobuyuki Otsu)提出的,是一种自适应阈值确定的方法,又叫大津法,简称OTSU, ...

  8. C#,图像二值化(04)——全局阈值的凯勒算法(Kittler Thresholding)及源程序

    1.Kittler算法(最小误差法)概述 最小误差法是 J. Kittler & J. Illingworth 1986年在<MINIMUM ERROR THRESHOLDING> ...

  9. C#,图像二值化(13)——全局阈值的双峰平均值算法(Bimodal Thresholding)与源程序

    1.图像二值化概述 图像二值化是将彩色图像转换为黑白图像.大多数计算机视觉应用程序将图片转换为二进制表示.图像越是未经处理,计算机就越容易解释其基本特征. 二值化过程 在计算机存储器中,所有文件通常以 ...

最新文章

  1. 点云配准求物体的6D姿态(转)
  2. python毕业设计开题报告-基于Python图书管理系统开题报告
  3. C# Enum,Int,String的互相转换 枚举转换
  4. 网络爬虫--之爬起校招信息代码
  5. IIS 6.0支持.SHTML
  6. python线性加权模型_局部加权之线性回归(1) - Python实现
  7. 使用ORM提取数据很容易! 是吗?
  8. MySQL中union和order by一起使用的方法
  9. bootstrap table 的简单Demo
  10. 我们应该改变Linux的二十四件事
  11. Flink on Zeppelin (3) - Streaming 篇
  12. 精美js聊天窗口界面代码
  13. JavaScript学习之初识JS
  14. 世界首例AI同性婚姻惊呆众人 | Siri和Alexa结婚了
  15. API接口调用并处理返回的json数据
  16. python选股软件编写
  17. Tortoise 账号和密码设置
  18. OnTime pro for mac(多功能时钟工具)
  19. 你想成为什么样的人取决于你付出了多少?
  20. 【国内动态】服务器列表

热门文章

  1. electron---windows客户端开发探索
  2. 20201010基础标签用途说明
  3. 使用HSL连接欧姆龙PLC配置
  4. 计算机毕业设计Javaweb实验室课表管理系统(源码+系统+mysql数据库+lw文档)
  5. Maven下载及目录结构
  6. linux服务器视频转换,linux下视频格式转换工具
  7. RealView编译器常用特有功能
  8. Python编写的命令行版微信。(已集成自动聊天机器人(通过网址api形式))
  9. SqlServer 调优的几个关键的步骤--sp_lock,sp_who
  10. 网页短信平台国际通道搭建|后台定制-移讯云短信系统