opencv3.0 函数学习 7——adaptiveThreshold 区域自适应二值化

adaptiveThreshold  区域(局部)自适应二值化

局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值。这样做的好处在于每个像素位置处的二值化阈值不是固定不变的,而是由其周围邻域像素的分布来决定的。亮度较高的图像区域的二值化阈值通常会较高,而亮度较低的图像区域的二值化阈值则会相适应地变小。不同亮度、对比度、纹理的局部图像区域将会拥有相对应的局部二值化阈值。常用的局部自适应阈值有:1)局部邻域块的均值;2)局部邻域块的高斯加权和。


函数参数

void cv::adaptiveThreshold ( InputArray  src,
    OutputArray  dst,
    double  maxValue,           (定义为255 )
    int  adaptiveMethod,    (需调节)
int  thresholdType,       (需调节)
    int  blockSize,                (需调节)
    double  C                               (需调节)
  )    

Applies an adaptive threshold to an array.

The function transforms a grayscale image to a binary image according to the formulae:

  • THRESH_BINARY

    dst(x,y)={maxValue0 if src(x,y)>T(x,y)otherwise  

  • THRESH_BINARY_INV

    dst(x,y)={0maxValue if src(x,y)>T(x,y)otherwise  

    where T(x,y)  is a threshold calculated individually for each pixel (see adaptiveMethod parameter).

The function can process the image in-place.

Parameters
src Source 8-bit single-channel image.
dst Destination image of the same size and the same type as src.
maxValue Non-zero value assigned to the pixels for which the condition is satisfied
adaptiveMethod Adaptive thresholding algorithm to use, see cv::AdaptiveThresholdTypes 
thresholdType Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV, seecv::ThresholdTypes.
blockSize Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
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.


cv::AdaptiveThresholdTypes

adaptive threshold algorithm see cv::adaptiveThreshold

Enumerator

ADAPTIVE_THRESH_MEAN_C 

暂时使用的是第一种,均值减去加权

第二种高斯加权还在学习中

the threshold value T(x,y)  is a mean of the blockSize×blockSize  neighborhood of (x,y)  minus C



ADAPTIVE_THRESH_GAUSSIAN_C 

the threshold value T(x,y)  is a weighted sum (cross-correlation with a Gaussian window) of the blockSize×blockSize  neighborhood of (x,y)  minus C . The default sigma (standard deviation) is used for the specified blockSize . Seecv::getGaussianKernel


Mat cv::getGaussianKernel ( int  ksize,
    double  sigma,
    int  ktype = CV_64F 
  )    

Returns Gaussian filter coefficients.

The function computes and returns the ksize×1  matrix of Gaussian filter coefficients:

G i =αe (i(ksize1)/2) 2 /(2sigma 2 ) , 

where i=0..ksize1  and α  is the scale factor chosen so that  i G i =1 .

Two of such generated kernels can be passed to sepFilter2D. Those functions automatically recognize smoothing kernels (a symmetrical kernel with sum of weights equal to 1) and handle them accordingly. You may also use the higher-level GaussianBlur.

Parameters
ksize Aperture size. It should be odd ( ksizemod2=1  ) and positive.
sigma Gaussian standard deviation. If it is non-positive, it is computed from ksize assigma = 0.3\*((ksize-1)\*0.5 - 1) + 0.8.
ktype Type of filter coefficients. It can be CV_32F or CV_64F .



 


enum cv::ThresholdTypes

type of the threshold operation

threshold.png
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

你可能感兴趣的:(opencv)