摘自https://docs.opencv.org/4.2.0/d7/d4d/tutorial_py_thresholding.html

Simple Thresholding

The function cv.threshold is used to apply the thresholding. The first argument is the source image, which should be a grayscale image. The second argument is the threshold value which is used to classify the pixel values. The third argument is the maximum value which is assigned to pixel values exceeding the threshold. OpenCV provides different types of thresholding which is given by the fourth parameter of the function.

The method returns two outputs. The first is the threshold that was used and the second output is the thresholded image.

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

Adaptive Thresholding

In the previous section, we used one global value as a threshold. But this might not be good in all cases, e.g. if an image has different lighting conditions in different areas. In that case, adaptive thresholding can help. Here, the algorithm determines the threshold for a pixel based on a small region around it.

In addition to the parameters described above, the method cv.adaptiveThreshold takes three input parameters:

The adaptiveMethod decides how the threshold value is calculated:

  • cv.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighbourhood area minus the constant C.
  • cv.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is a gaussian-weighted sum of the neighbourhood values minus the constant C.

The blockSize determines the size of the neighbourhood area and C is a constant that is subtracted from the mean or weighted sum of the neighbourhood pixels.

import cv2 as cv
import numpy as np
from matplotlib import pyplot as pltimg = cv.imread('sudoku.png',0)
img = cv.medianBlur(img,5)ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.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()

Otsu's Binarization

Consider an image with only two distinct image values (bimodal image), where the histogram would only consist of two peaks. A good threshold would be in the middle of those two values. Similarly, Otsu's method determines an optimal global threshold value from the image histogram.

In order to do so, the cv.threshold() function is used, where cv.THRESH_OTSU is passed as an extra flag. The threshold value can be chosen arbitrary. The algorithm then finds the optimal threshold value which is returned as the first output.

Check out the example below. The input image is a noisy image. In the first case, global thresholding with a value of 127 is applied. In the second case, Otsu's thresholding is applied directly. In the third case, the image is first filtered with a 5x5 gaussian kernel to remove the noise, then Otsu thresholding is applied.

img = cv.imread('noisy2.png',0)# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)

Image Thresholding相关推荐

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

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

  2. OpenCV使用inRange的阈值操作Thresholding Operations

    OpenCV使用inRange的阈值操作Thresholding Operations 使用inRange的阈值操作 目标 理论 HSV色彩空间 代码 解释 结果 使用inRange的阈值操作 目标 ...

  3. 数字图像处理100问—03二值化(Thresholding)

    提示:内容整理自:https://github.com/gzr2017/ImageProcessing100Wen CV小白从0开始学数字图像处理 03二值化(Thresholding) 把图像进行二 ...

  4. Image Thresholding图像阙值化和Adaptive Thresholding

    要用到的函数是cv2.threshold() 这个参数的形式是cv.Threshold(src, dst, threshold, maxValue, thresholdType) Parameters ...

  5. Document-Level Relation Extraction with Adaptive Thresholding and Localized Context Pooling

    ​​​​​​http://arxiv.org/abs/2010.11304 用自适应阈值和局部上下文池化技术进行文档级关系抽取任务 与句子级关系抽取相比,文档级关系抽取(RE)提出了新的挑战.一个文档 ...

  6. AAAI-21-DocRE-Document-Level Relation Extraction with Adaptive Thresholding and Localized Context Po

    Document-Level Relation Extraction with Adaptive Thresholding and Localized Context Pooling 目录 Docum ...

  7. Document-Level Relation Extraction with Adaptive Thresholding and Localized Context Pooling 阅读笔记

    Document-Level Relation Extraction with Adaptive Thresholding and Localized Context Pooling Purpose ...

  8. 灰度值阈值设置matlab,基本全局阈值法(basic global thresholding)MATLAB实现

    基本全局阈值分割步骤如下: (1)设定参数 ,并选择一个初始的估计阈值 . (2)用阈值 分割图像.将图像分成两部分: 是由灰度值大于 的像素组成, 是由灰度值小于或等于 的像素组成. (3)计算 和 ...

  9. (转)IST:Iterative Shrinkage/Thresholding和Iterative Soft Thresholding

    原 IST:Iterative Shrinkage/Thresholding和Iterative Soft Thresholding 2016年08月10日 15:26:02 jbb0523 阅读数: ...

  10. IST改进算法之Two-Step Iterative Shrinkage/Thresholding(TwIST)

    题目:IST改进算法之Two-Step Iterative Shrinkage/Thresholding(TwIST) 本篇介绍IST的一种改进算法TwIST,该改进算法由以下文献提出: Biouca ...

最新文章

  1. PHP编实现程动态图像的创建
  2. php自动加载类与路由,PHP实现路由与类自动加载步骤详解
  3. mysql 应用程序优化
  4. 【OO学习】OO第四单元作业总结及OO课程总结
  5. (二)图像处理技术概述
  6. Windows 10 Edge 浏览器续航对比火狐/Chrome
  7. 10.Windows线程切换_FS段寄存器
  8. oracle11g 隐藏参数_oracle隐含参数的查看与修改
  9. vc mysql init 崩溃_故障分析 | 崩溃恢复巨慢原因分析
  10. flask中数据库的基本操作-增删改查【备忘】
  11. 双基因突变患者_双任务干预对携带LRRK2基因突变的帕金森病患者手灵活性的影响...
  12. BZOJ 1786 DP
  13. gauge 运行其他spec_Gem5(SE模式)上运行SPEC2017教程
  14. centos安装DHCP服务器
  15. Linux文本编辑器-vi/vim
  16. [2019徐州网络赛J题]Random Access Iterator
  17. desktop viewer
  18. 华为NP课程笔记27-QINQ概述
  19. ESP分区引导文件修复bcdboot .exe
  20. java分组求和实例_mybatis example group by count 分组求和 - java分组求和

热门文章

  1. 一个“网络混子”做网站近一年来的一点随感
  2. 测试的基础理论与软件缺陷的介绍
  3. labVIEW while循环中的移位寄存器的用法及作用
  4. 数飞OA该推出1800元的OA系统吗
  5. form标签的action属性怎么用?form标签action属性的用法介绍(附实例)
  6. linux服务器 openshift,Openshift 指南
  7. java 显示文本框_java计算器文本框显示
  8. Python 自动控制原理 control
  9. 初学C语言-结构体与联合体
  10. 人生有好多难关 及早把心路放宽——想法简单 生命更宽