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


相关推荐

  • JFinal开发web项目出现故障小记

    JFinal开发web项目出现故障小记

    2022年1月28日
    45
  • linux超级用户权限 rwx_Linux的RWX权限管理实现详解及chmod使用

    linux超级用户权限 rwx_Linux的RWX权限管理实现详解及chmod使用前文我们对Linux操作系统的权限管理进行了简要的介绍。今天我们就详细介绍一下关于RWX权限管理的更多细节。很多同学对RWX权限都有一些了解,但是要说出子丑来恐怕就不那么容易了。Linux的RWX权限控制又称为DAC(DiscretionaryAccessControl,自主访问控制)。DAC机制就是指对象的拥有者可以任意修改或授予此对象相应的权限。从主体和客体的角度来说,就是主体对其拥有的…

    2022年5月30日
    38
  • 于Linux-2.6.32内核上编译ipset-6.23的坎坷经历[通俗易懂]

    于Linux-2.6.32内核上编译ipset-6.23的坎坷经历[通俗易懂]新版本的ipset上周在儿童医院给小小看病等待叫号的间隙,收到了Netfilter邮件列表的推送消息,一览了ipset最新的6.23版本的新特性,很多正是我目前所需要的,特别是timeout和skbinfo参数的支持,具体的详情请自行查看manual,如果不想看那么多,我这里简单的贴一下:  timeout      All set types supportstheoptional

    2022年9月1日
    2
  • Redis的持久化-RDB

    Redis的持久化-RDB

    2022年2月12日
    50
  • awk工具详解

    awk工具详解目录awk概述awk工作原理awk命令格式awk概述AWK是一种处理文本文件的语言,是一个强大的文本分析工具。它是专门为文本处理设计的编程语言,也是行处理软件,通常用于扫描、过滤、统计汇总

    2022年7月4日
    21
  • pycharm虚拟环境的解释器设置_pycharm虚拟环境

    pycharm虚拟环境的解释器设置_pycharm虚拟环境什么是Pycharm中的虚拟环境 假如想要在Pycharm中建立两个项目,并且这两个项目需要用到同一个第三方库的不同版本,如果这两个项目共享一个运行环境,那么此时就会发生版本冲突问题。为了解决这个问题,Pycharm提供了Virtualenv(即,虚拟环境)。Virtualenv可以创建一套独立运行的Python环境,从而做到不同项目之间的隔离。当需要安装该环境所需要的包时,在设置—项目—…

    2025年6月28日
    3

发表回复

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

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