Image Thresholding

Image Thresholding摘自https://docs.opencv.org/4.2.0/d7/d4d/tutorial_py_thresholding.htmlSimpleThresholdingThefunctioncv.thresholdisusedtoapplythethresholding.Thefirstargumentisthesourceimage,whichsh…

大家好,又见面了,我是你们的朋友全栈君。

摘自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 plt

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

Image Thresholding

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:

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 plt

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

Image Thresholding

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 5×5 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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132295.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab绘画三维图形(三)

    matlab绘画三维图形(三)对散点图拟合三维网格图形 num xlsread data 2011a xls B4 E322 读取出该区域的数据作为表格 A num 1 从 B 矩阵取出第一列的所有行 B num 2 C num 3 xx linspace min A max A 50 产生 min A 到 max A 均摊的 50 个点 目的上拟合离散点数量上的不足 yy linsp

    2025年10月10日
    2
  • cs与bs模式的优缺点_什么是cs什么是bs

    cs与bs模式的优缺点_什么是cs什么是bscs与bs模式关于CS(Client-Server)模式和BS(Browser-Server)模式的水很深,盆地自己也认为对此了解不够透彻,但作为手机客户端设计,如果不对CS、BS做一定程度的了解,是很容易出现一些方向性的错误、走一些弯路抑或在实现性价比上付出过多代价。本文偏向于基础知识,产品人员很多情况下不仅仅要了解表现、交互,还需要一定程度上了解可实现性、实现代价、实现形式、实现限制等…

    2025年10月11日
    5
  • 如何做小册子在a4纸上出四页_一年级政治小册子内容

    如何做小册子在a4纸上出四页_一年级政治小册子内容打印小册子(一张A4纸4页内容)步骤分享

    2025年9月21日
    5
  • 中介者模式和观察者模式的区别_序列中介作用

    中介者模式和观察者模式的区别_序列中介作用中介者模式 Mediator动机模式定义结构要点总结笔记动机在软件构建过程中,经常会出现多个多个对象相互关联交互的情况,对象之间常常会维持一种复杂的引用关系.如果遇到一些需求的更改.这种直接的引用关系将面临不断地变化这种情况下,我们可以使用一个”中介对象”来管理对象间地关联关系,避免相互交互地对象之间地紧耦合引用关系,从而更好地抵御变换模式定义用一个中介对象来封装(封装变化)一系列地对象交互中.中介者使各个对象不需要显式地相互引用(编译时依赖->运行时依赖),从而使其耦合松散(管理变化),而

    2022年8月11日
    3
  • 费曼学习法

    费曼学习法费曼学习法我的理解:费曼学习法就是把学好的东西用简洁易懂的语言,传授给别人举例:你学完微积分,然后自己去培训班,自己做老师,传授给学生们,并且学生们都能听懂费曼学习法的四个步骤:1.确定目标

    2022年7月28日
    7
  • R6034错误,C Runtime Error

    R6034错误,C Runtime Error这是我转的一篇非常全的帖子 nbsp 能查到的解决方法都在里面有提及 nbsp 我是使用 stdafx h 加入这句 code pragmacommen linker manifestdepe type Win32 name Microsoft VC80 CRT version 8 0 50608 0 processorArc X86

    2025年7月23日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号