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

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • android 平板重装系统,平板电脑系统重装方法「建议收藏」

    不少用户想要平板电脑重装,但是不知如何操作,为此有些为难。平板电脑是跟笔记本电脑方便携带出现的物品,不会意味着台式机电脑会退出电脑界,2020年新的开始新的一年,让许多的电脑品牌竞争激烈起来。平板电脑也叫便携式电脑,是一款以触摸屏作为基本的输入设备,无须翻盖、没有键盘、小到放入女士手袋,但是却功能完整的PC。可是满不满足可以重装系统的条件,下面小编整理了平板装系统的方法。1、系统重装支持ARIM构…

  • __setattr__,setattr(),getattr__,getattr()及__getattribute__的区别

    __setattr__,setattr(),getattr__,getattr()及__getattribute__的区别Python2.7IDEPycharm5.0.3首先,给一波定义__setattr__(self,name,value)#当试图给特性name赋值时候自动被调用,其余方法同理#相当于触发机制__getattribute__(self,item)#每次通过实例化访问属性都会通过该函数#如果没有该属性,则访问完该函数后,再会通过__getattr__函数_setattr_和_get

    2022年10月30日
  • 用matlab产生时域离散信号实验报告(有关数字信号处理)

    1.正弦序列离散正弦序列的MATLAB表示与连续信号类似,只不过是用stem函数而不是用plot函数来画出序列的波形。下面就是正弦序列的MATLAB源程序。%正弦序列实现程序k=0:39;fk=sin(pi/6*k);stem(k,fk)2.指数序列离散指数序列的一般形式为,可用MATLAB中的数组幂运算(即点幂运算)c*来实现。下面为用MATLAB编写绘制离散时

  • 精进Quartz—Quartz简单入门Demo(二)

    要学习Quartz框架,首先大概了解了Quartz的基本知识后,在通过简单的例子入门,一步一个脚印的走下去。 下面介绍Quartz入门的示例,由于Quartz的存储方式分为RAM和JDBC,分别对这两种进行简单的说明。并附上代码! 首先需要添加Quartz的Jar包 ,我使用的是quartz.2.2.1版本!

  • 【python】错误SyntaxError: invalid syntax的解决方法总结

    【python】错误SyntaxError: invalid syntax的解决方法总结今天学习了python,然而刚开始就出了一个难题,明明代码没有一点问题,可是每次运行都会显示“SyntaxError:invalidsyntax”。“SyntaxError:invalidsyntax”的意思就是语法错误;经过查询解决了这个问题,所以总结一个这个问题的解决方法:版本问题:因为python2和python3是不兼容的;可以尝试更换版本;粗心问题:忘…

  • 阿里云的ddns_阿里云docker部署

    阿里云的ddns_阿里云docker部署文档:阿里云DDNSjava实现.note链接:http://note.youdao.com/noteshare?id=c2fe233164c3756b38693d78f31347ed如果有小伙伴发现bug或者有建议请及时联系

    2022年10月30日

发表回复

您的电子邮箱地址不会被公开。

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