opencv3.0 函数学习 6——threshold 二值化

时间:2021-11-10 21:45:36

threshold 全局二值化

一幅图像包括目标物体、背景还有噪声,要想从多值的数字图像中直接提取出目标物体,最常用的方法就是设定一个全局的阈值T,用T将图像的数据分成两部分:大于T的像素群和小于T的像素群。将大于T的像素群的像素值设定为白色(或者黑色),小于T的像素群的像素值设定为黑色(或者白色)。


函数参数

double threshold( InputArray src, OutputArray dst,
                               double thresh, double maxval, int type );  (  项目中自己设置 maxval默认的值为255,thresh由用户调节)

double cv::threshold ( InputArray  src,
    OutputArray  dst,
    double  thresh,      (需调节)
    double  maxval,    (定义为255)
    int  type            (需要调节)
  )    

Applies a fixed-level threshold to each array element.

The function applies fixed-level thresholding to a single-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image (cv::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 cv::THRESH_OTSU or cv::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 . The function returns the computed threshold value. Currently, the Otsu's and Triangle methods are implemented only for 8-bit images.

Parameters
src input array (single-channel, 8-bit or 32-bit floating point).
dst output array of the same size and type as src.
thresh threshold value.
maxval maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
type thresholding type (see the cv::ThresholdTypes).



enum cv::ThresholdTypes

type of the threshold operation

opencv3.0 函数学习 6——threshold 二值化
threshold types
Enumerator
THRESH_BINARY 

dst(x,y)={maxval0 if src(x,y)>threshotherwise  

THRESH_BINARY_INV 

dst(x,y)={0maxval if src(x,y)>threshotherwise  

THRESH_TRUNC 

dst(x,y)={thresholdsrc(x,y) if src(x,y)>threshotherwise  

THRESH_TOZERO 

dst(x,y)={src(x,y)0 if src(x,y)>threshotherwise  

THRESH_TOZERO_INV 

dst(x,y)={0src(x,y) if src(x,y)>threshotherwise  

THRESH_MASK   
THRESH_OTSU 

flag, use Otsu algorithm to choose the optimal threshold value

THRESH_TRIANGLE 

flag, use Triangle algorithm to choose the optimal threshold value



7= MASK (尚未找到相关资料)

 8=otsu法(最大类间方差法,有时也称之为大津算法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找一个合适的灰度级别 来划分。 所以 可以在二值化的时候 采用otsu算法来自动选取阈值进行二值化。otsu算法被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响。因此,使类间方差最大的分割意味着错分概率最小。
};

16=TRIANGLE   (尚未找到相关资料)