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

普通阈值

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的视频上传后,模糊了不少,可能是免费的缘故吧,那就凑合用凑合看。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 11:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 11:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 11:16:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 11:16:03       20 阅读

热门阅读

  1. AtCoder Beginner Contest 297(A-E)

    2024-01-02 11:16:03       44 阅读
  2. oracle 独立事务的含义和用法

    2024-01-02 11:16:03       35 阅读
  3. 基于SpringBoot的家具商城设计与实现

    2024-01-02 11:16:03       37 阅读
  4. 排序算法——快速排序

    2024-01-02 11:16:03       41 阅读