DrGraph原理示教 - OpenCV 4 功能 - 阈值

news/2025/2/14 2:53:20/

普通阈值

OpenCV中的阈值用于相对于提供的阈值分配像素值。在阈值处理中,将每个像素值与阈值进行比较,如果像素值小于阈值则设置为0,否则设置为最大值(一般为255)。
在OpenCV中,有多种阈值类型可供选择,以下是一些常见的阈值类型:
二进制阈值化:选择一个特定的阈值量,大于该阈值的像素点的灰度值设定为最大值(如8位灰度值最大为255),小于该阈值的像素点的灰度值设定为0。
反二进制阈值化:与二进制阈值化相似,只是最后的设定值相反。在8位灰度图中,例如大于阈值的设定为0,而小于该阈值的设定为255。
截断阈值化:同样需要选择一个阈值,图像中大于该阈值的像素点被设定为该阈值,小于该阈值的保持不变。
阈值化为0:选择一个阈值,然后对图像做如下处理:1. 像素点的灰度值大于该阈值的不进行任何改变;2. 像素点的灰度值小于该阈值的,其灰度值全部变为0。
反阈值化为0:与阈值化为0类似,只是像素点的灰度值大于该阈值的不进行任何改变,而小于该阈值的,其灰度值全部变为0。
其实就是调用threshold()函数进行阈值处理,它有4个参数:
参数1:需要处理的原图,一般是灰度图。
参数2:设定的阈值。
参数3:最大阈值,一般为255。
参数4:阈值的方式,主要有5种,分别为cv.THRESH_BINARY、cv.THRESH_BINARY_INV、cv.THRESH_TRUNC、cv.THRESH_TOZERO、cv.THRESH_TOZERO_INV。

/** @brief Applies a fixed-level threshold to each array element.The function applies fixed-level thresholding to a multiple-channel array. The function is typically
used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for
this purpose) or for removing a noise, that is, filtering out pixels with too small or too large
values. There are several types of thresholding supported by the function. They are determined by
type parameter.Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the
above values. In these cases, the function determines the optimal threshold value using the Otsu's
or Triangle algorithm and uses it instead of the specified thresh.@note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images.@param src input array (multiple-channel, 8-bit or 32-bit floating point).
@param dst output array of the same size  and type and the same number of channels as src.
@param thresh threshold value.
@param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
types.
@param type thresholding type (see #ThresholdTypes).
@return the computed threshold value if Otsu's or Triangle methods used.@sa  adaptiveThreshold, findContours, compare, min, max*/
CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,double thresh, double maxval, int type );

5种阈值方式cv.THRESH_BINARY、cv.THRESH_BINARY_INV、cv.THRESH_TRUNC、cv.THRESH_TOZERO、cv.THRESH_TOZERO_INV的效果分别为
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
从别的地方看,threshold是处理灰度图,不过从实际运行情况【OpenCV 4.6】来看,彩色图也支持threshold,目测是针对各通道进行threshold后再合成结果图像。有时间看下源码就可以不用猜测了。
从头文件注释中可以看到

Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the
above values. In these cases, the function determines the optimal threshold value using the Otsu's
or Triangle algorithm and uses it instead of the specified thresh.
@note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images.

即THRESH_OTSU与THRESH_TRIANGLE可组合。这个的结果是8位单通道图,应该是在处理前就将其余类型图像转化为灰度图了
在这里插入图片描述
在这里插入图片描述

智能阈值

运行发现,THRESH_OTSU与THRESH_TRIANGLE的运算结果与阈值大小无关。
这有点象智能阈值,即阈值是智能或自动确定的。
而真正的智能阈值,也叫自适应阈值,是由adaptiveThreshold函数负责处理

/** @brief Applies an adaptive threshold to an array.The function transforms a grayscale image to a binary image according to the formulae:
-   **THRESH_BINARY**\f[dst(x,y) =  \fork{\texttt{maxValue}}{if \(src(x,y) > T(x,y)\)}{0}{otherwise}\f]
-   **THRESH_BINARY_INV**\f[dst(x,y) =  \fork{0}{if \(src(x,y) > T(x,y)\)}{\texttt{maxValue}}{otherwise}\f]
where \f$T(x,y)\f$ is a threshold calculated individually for each pixel (see adaptiveMethod parameter).The function can process the image in-place.@param src Source 8-bit single-channel image.
@param dst Destination image of the same size and the same type as src.
@param maxValue Non-zero value assigned to the pixels for which the condition is satisfied
@param adaptiveMethod Adaptive thresholding algorithm to use, see #AdaptiveThresholdTypes.
The #BORDER_REPLICATE | #BORDER_ISOLATED is used to process boundaries.
@param thresholdType Thresholding type that must be either #THRESH_BINARY or #THRESH_BINARY_INV,
see #ThresholdTypes.
@param blockSize Size of a pixel neighborhood that is used to calculate a threshold value for the
pixel: 3, 5, 7, and so on.
@param C Constant subtracted from the mean or weighted mean (see the details below). Normally, it
is positive but may be zero or negative as well.@sa  threshold, blur, GaussianBlur*/
CV_EXPORTS_W void adaptiveThreshold( InputArray src, OutputArray dst,double maxValue, int adaptiveMethod,int thresholdType, int blockSize, double C );

参数说明如下:
src:输入图像。
dst:输出图像。
maxValue:使用CV_THRESH_BINARY和CV_THRESH_BINARY_INV的最大值。
adaptive_method:自适应阈值算法使用,可以选择CV_ADAPTIVE_THRESH_MEAN_C或CV_ADAPTIVE_THRESH_GAUSSIAN_C。
thresholdType:阈值类型,可以选择CV_THRESH_BINARY或CV_THRESH_BINARY_INV。
blockSize:邻域大小,默认值为3。
自适应阈值方法的思想是根据图像不同区域的亮度分布,计算其局部阈值,从而能够自适应地处理图像的不同区域。这种方法可以更好地处理明暗差异较大的图像,并且可以在一定程度上减轻光照不均对图像分割的影响。
通过调参方式查看的代码:

// [基本 - 智能阈值」类 - 滤镜处理dstMat = CvHelper::ChangeMatDim(dstMat, 1);int paramIndex = 0;int adaptiveMethod = GetParamValue_Int(paramIndex++); // 0 - 算法int thresholdType = GetParamValue_Int(paramIndex++);   // 1 - 类型int maxValue = GetParamValue_Int(paramIndex++);   // 2 - 最大值int blockSize = GetParamValue_Int(paramIndex++);       // 3 - 块大小if(blockSize % 2 == 0)blockSize++;int C = GetParamValue_Int(paramIndex++);     // 4 - 常数adaptiveThreshold(dstMat, dstMat, maxValue, adaptiveMethod, thresholdType, blockSize, C);

在这里插入图片描述
其实,阈值的原理不多,有个直观的感受即可。
运行效果:

OpenCV 4 功能 - 阈值

CSDN的视频上传后,模糊了不少,可能是免费的缘故吧,那就凑合用凑合看。


http://www.ppmy.cn/news/1288886.html

相关文章

【小沐学NLP】Python实现K-Means聚类算法(nltk、sklearn)

文章目录 1、简介1.1 机器学习1.2 K 均值聚类1.2.1 聚类定义1.2.2 K-Means定义1.2.3 K-Means优缺点1.2.4 K-Means算法步骤 2、测试2.1 K-Means(Python)2.2 K-Means(Sklearn)2.2.1 例子1:数组分类2.2.2 例子2&#xff1…

视频人脸识别马赛克处理

文章目录 前言一、实现思路?二、Coding三、实现效果 前言 前面几篇文章我们尝试了使用opencv完成图像人脸识别以及识别后贴图或者打马赛克的方法。 偶尔我们也会有需求在视频中将人脸马赛克化,opencv也提供了相应的方法来实现这个功能。 一、实现思路&a…

数据库-期末考前复习-第4章-数据库安全性

1、掌握实现数据库安全性控制的常用方法和技术有。 序号常用方法和技术概括1访问控制通过授权和权限管理来限制用户对数据库的访问和操作。可以使用角色和用户管理来定义不同用户的权限级别,并使用访问控制列表(ACL)来控制用户对数据库对象的…

磁盘管理-------RAID卡

目录 一、RAID概述 二、常见类型 (一)RAID 0 (二)RAID 1 (三)RAID 5 (四)RAID 6 (五)RAID 10 (六)总结 三、创建RAID &…

科普:敏捷估算为什么用斐波那契数列

被一个同学问:敏捷估算为什么用斐波那契数列?有什么意义? 简单说说我自己的简介: 敏捷开发中使用斐波那契数列来估算的原因是,斐波那契数列可以用于估算任务的难度级别,并帮助团队预测完成任务所需的时间…

Spring-IOC-xml方式

简介 **控制反转**(Inversion of Control,缩写为**IoC**),是[面向对象编程]中的一种设计原则,可以用来减低计算机[代码]之间的[耦合度]。其中最常见的方式叫做[依赖注入]Dependency Injection,简称DI&#…

Hadoop之Yarn 详细教程

1、yarn 的基本介绍和产生背景 YARN 是 Hadoop2 引入的通用的资源管理和任务调度的平台,可以在 YARN 上运行 MapReduce、Tez、Spark 等多种计算框架,只要计算框架实现了 YARN 所定义的 接口,都可以运行在这套通用的 Hadoop 资源管理和任务调…

kafka处理大量消息积压tips —— 筑梦之路

一、consumer导致kafka积压了大量消息 场景: 1. 如果是Kafka消费能力不足,则可以考虑增加 topic 的 partition 的个数, 同时提升消费者组的消费者数量,消费数 分区数 (二者缺一不可) 2. 若是下游数据处理…