Adaptive thresholding operation: adaptiveThreshold () function

In the image thresholding operation, more attention from the binarized image, the isolated target and background regions, but only difficult to achieve the desired effect of the division to set a fixed threshold. The adaptive threshold is determined on the binarization threshold value according to the pixel position of the pixel value distribution of pixels neighboring blocks . This benefit:

1. binarization threshold of each pixel position is not fixed, but its distribution in the neighborhood around the pixel determined.

2. high luminance image area binarization threshold value is usually higher, and the binarization threshold value becomes smaller will be adapted to the low brightness image area.

3. Different brightness, contrast, local image area will have a texture corresponding to the local binarization threshold value.

Function prototype

1.    void adaptiveThreshold(InputArray src, OutputArray dst,  
2.                           double maxValue, int adaptiveMethod,  
3.                           int thresholdType, int bolckSize, double C)  

Parameter Description

Parameter 1: InputArray the src type, the input image, to fill a single channel, a single floating point type Mat to 8.
Parameter 2: the result of calculation is stored in this function. That is, an output image (the same size and type of the input image).
3 parameters: the maximum value satisfies the preset condition.
Parameter 4: specifies an adaptive threshold algorithm. Alternatively or ADAPTIVE_THRESH_MEAN_C ADAPTIVE_THRESH_GAUSSIAN_C two kinds. (Specific see explanation below).
Parameter 5: the type specified threshold. Alternatively or THRESH_BINARY THRESH_BINARY_INV two kinds. (I.e., binary or reverse binary threshold value threshold).
Parameter 6: block size indicates the neighborhood used to calculate the area threshold, typically chosen 3,5,7 ...... like.
Parameter 7: parameter C indicates a parameter related to the algorithm, which is extracted from a mean or a weighted mean constant, can be negative. (Specific see explanation below).
// ------------------------------------------------ -------------------------------------------------- -------------------------------------------------- ----------------------

Interpretation of the parameters and parameter 7 4 content:
Calculating adaptive thresholding process probably is calculated for each individual pixel dots threshold value, i.e. the threshold value of each pixel is different, the surrounding pixel is a weighted average of the pixel B in the zone B *, is then subtracted a constant C, thereby obtaining the threshold point. 6 is specified by the parameter B, the constant C is specified by the parameter 7.

ADAPTIVE_THRESH_MEAN_C, for the average local neighborhood block , the algorithm is to determine the mean of the block, minus Changshu C.

ADAPTIVE_THRESH_GAUSSIAN_C, for the local neighborhood Gaussian weighted sum block . The algorithm is a pixel (x, y) around them in accordance with the distance from the center point in the area weighted according to a Gaussian function, minus the constant C.

For example: If the average method, the average mean of 190, the difference delta (i.e., constant C) is 30. Then the pixel gradation is less than 160 0, 160 greater than or equal to 255 pixels. As shown below:

If the reverse is binarized, as shown below:

delta (constant C) negative values ​​are possible.

Code demonstrates

/*
    Adaptive Threshold: adaptiveThreshold () function
*/
 
#include <opencv2/core/core.hpp>              
#include <opencv2/highgui/highgui.hpp>              
#include <opencv2/imgproc/imgproc.hpp>             
#include <iostream>            
using namespace std;
using namespace cv;
 
int main ()
{
    // ------------ [1] reads the source image and check whether the image reading success ------------     
    Mat srcImage = imread ( " D: \\ \\ \\ build.jpg ImageTest OutPutResult " );
     IF (! srcImage.data)
    {
        cout << " read the image error, please re-enter the correct path \ the n-! " ;
        system("pause");
        return -1;
    }
    imshow ( " [] source image " , srcImage);
     // ----------------------- gradation conversion [2] -----------------------     
    Mat srcGray;
    cvtColor (srcImage, srcGray, CV_RGB2GRAY);
    imshow ( " [] grayscale " , srcGray);
     // ------------ [3] to initialize the relevant variables ---------------   
    Mat dstImage ;         // initializes the adaptive threshold parameter 
    const  int maxVal = 255 ;
     int blockSize = . 3 ;     // value 3,5,7 .... etc. 
    int constValue = 10 ;
     int adaptiveMethod = 0 ;
     int thresholdtype = . 1 ;
     / *
        Adaptive Threshold Algorithm
        0:ADAPTIVE_THRESH_MEAN_C
        1:ADAPTIVE_THRESH_GAUSSIAN_C
        --------------------------------------
        Threshold Type
        0:THRESH_BINARY
        1:THRESH_BINARY_INV
    * / 
    // --------------- [4] an adaptive image thresholding operation ----------------------- - 
    adaptiveThreshold (srcGray, dstImage, maxVal, adaptiveMethod, thresholdtype, blockSize, constValue);
 
    imshow ( " [] Adaptive Threshold " , dstImage);
    waitKey(0);
    return 0;
}

show result

An adaptive threshold can be found well observed edge information. Threshold selection algorithm is done automatically, it is convenient.

Filtering 

In addition, do not do the filtering effect on the image segmentation processing is relatively large.

1. adaptiveThreshold分割

    Mat img=imread("D:/ImageTest/sudoku.png",CV_LOAD_IMAGE_COLOR);
    Food DST1;
    Food dst2;
    Food dst3;
    :: cvtColor CV (IMG, IMG, COLOR_RGB2GRAY); // for gradation processing 
    medianBlur (IMG, IMG, . 5 ); // median filtering 
    threshold (IMG, DST1, 127 , 255 , THRESH_BINARY); // Thresholding 
    adaptiveThreshold (IMG, dst2, 255 , ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, . 11 , 2 ); // automatic threshold segmentation, neighborhood average 
    adaptiveThreshold (IMG, dst3, 255 , ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, . 11 , 2 ); // automatic thresholding, Gaussian neighborhood
     //ADAPTIVE_THRESH_MEAN_C : threshold value is the mean of neighbourhood area
    //ADAPTIVE_THRESH_GAUSSIAN_C : threshold value is the weighted sum of neighbourhood values where weights are a gaussian window. 
    imshow("dst1", dst1);
    imshow("dst2", dst2);
    imshow("dst3", dst3);
    imshow("img", img);
    waitKey(0);

Effect of contrast, it is clear that the addition of neighborhood weight processing more desirable:

Dividing the variance between the maximum filtering process was added 2 class

Mat img=imread("D:/ImageTest/pic2.png",CV_LOAD_IMAGE_COLOR);
    Food DST1;
    Food dst2;
    Food dst3;
    :: cvtColor CV (IMG, IMG, COLOR_RGB2GRAY); // for gradation processing
     //     medianBlur (IMG, IMG,. 5); 
    threshold (IMG, DST1, 127 , 255 , THRESH_BINARY);
    threshold (IMG, dst2, 0 , 255 , THRESH_OTSU); // maximum between-class variance divided Otsu to the Choose The Optimal threshold algorithm value 
    Mat IMG2 = img.clone ();
    The Gaussian Blur (IMG2, IMG2, Size ( . 5 , . 5 ), 0 ); // a Gaussian filter to remove small noise 
    threshold (IMG2, dst3, 0 , 255 , THRESH_OTSU);
    imshow("BINARY dst1", dst1);
    imshow("OTSU dst2", dst2);
    imshow("GaussianBlur OTSU dst3", dst3);
    imshow("original img", img);
    waitKey(0);

Results are as follows, and a filter filtering the difference is clearly not obvious:

 

 

 

 

Reference article: https: //blog.csdn.net/sinat_36264666/article/details/77586964

             https://blog.csdn.net/abcvincent/article/details/78822191

Guess you like

Origin www.cnblogs.com/GaloisY/p/11037350.html