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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hashmap的数据结构以及put和get

    hashmap的数据结构以及put和get一,hashmap数据结构。数据结构中有数组和链表来实现对数据的存储,但是这两种方式的优点和缺点都很明显:1,数组存储,它的存储区间是连续的,比较占内存,故空间复杂度高。但是利用二分法进行查找的话,效率高,时间复杂度为O(1)。其特点就是:存储区间连续,查找速度快,但是占内存严重,插入和删除就慢。2,链表查询,它的存储区间离散,占内存比较宽松,故空间复杂度低,但时间复杂度高,为O(n)。其特

    2022年5月19日
    35
  • hadoop Secondary NameNode作用

    hadoop Secondary NameNode作用

    2021年9月9日
    42
  • java treeset subset_Java TreeSet subSet()方法

    java treeset subset_Java TreeSet subSet()方法JavaTreeSetsubSet()方法java.util.TreeSet.subSet(EfromElement,EtoElement)方法用于返回位于给定范围(包括fromElement和不包括toElement)之间的一组元素。1语法publicSortedSetsubSet(EfromElement,EtoElement)2参数fromElement:这是返回集的最…

    2025年6月16日
    0
  • Burp Suite抓包使用步骤「建议收藏」

    Burp Suite抓包使用步骤「建议收藏」原创https://www.cnblogs.com/mihoutao/p/11690432.html金龟子大战猕猴桃BurpSuite抓包工具的操作步骤见安装步骤那篇博客检查是否存在漏洞,就看拦截之后修改过的数据是否写进了数据库举例一、上传文件1、打开Burp.调整Proxy-Intercept-Interceptison为‘Interceptisoff’(拦截器开关)2、在火狐中打开需要拦截的网页,(比如同charlse博客里面写到的上传1.txt文件),点击上传文件按钮3、回

    2022年4月30日
    304
  • mysql binlog日志_事务日志

    mysql binlog日志_事务日志binlog就是binarylog,二进制日志文件,这个文件记录了mysql所有的dml操作。通过binlog日志我们可以做数据恢复,做主住复制和主从复制等等。对于开发者可能对binlog并不怎么关注,但是对于运维或者架构人员来讲是非常重要的。如何开启mysql的binlog日志呢?在my.inf主配置文件中直接添加三行log_bin=ONlog_bin_basenam

    2022年8月31日
    1
  • java什么是重写什么是重载_方法的重写和重载

    java什么是重写什么是重载_方法的重写和重载重写和重载的区别一.重写1.重写概念重写是父类与子类中的多态性,子类可以继承父类中的方法并进行重写,但是要保证几点没有变1)方法返回值类型不变2)参数列表不变3)方法名不变满足了这三点之后,子类中父类的方法就被覆盖了,如果想要调用父类的方法,用super关键字2.重写规则重写也是有一定的规则的,否则会报错重写规则:1)参数列表必须完全与被重写的方法相同;2)返回类型必须一直与被重写的方法相同;3)访问修饰符的权限一定要大于等于父类方法的访问修饰符;4)重写方法一定不能抛出

    2022年9月9日
    0

发表回复

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

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