[C++ OpenCV] Threshold binarization, threshold anti-binarization, truncation, threshold zeroing, threshold anti-zeroing, adaptive threshold usage method and timing

Binary image

1. Threshold concept

Threshold: Simply put, it is a ruler for segmenting image pixels. There are two forms of fixed threshold and adaptive threshold in binarization processing.

So when to use fixed threshold and when to use adaptive threshold?

Answer: When the image quality is good and the target and background are easy to distinguish, a fixed threshold can be used

When the image quality is poor and there are excessive shadows , although the Otsu method and the triangle method can be used to find the threshold by yourself, the threshold value of the entire image is the same, so the final segmentation effect is poor.

Therefore, you can use adaptive , or divide the entire image into several rows and columns, use the Otsu method or the triangle method for each part, and finally integrate the graphics, so that the thresholds of each part are different and the segmentation effect will be better.

2. Fixed threshold binarization threshold()

double**threshold**(    
​
InputArray src,
 OutputArray dst,
 double thresh,
 double maxval,
 int type     阈值类型如下
 );

2.1 Threshold type

binary:binary

type effect
THRESH_BINARY The gray value higher than the threshold is the maximum, and the rest are 0
THRESH_BINARY_INV The gray value higher than the threshold is 0, and the rest is the maximum
THRESH_TOZERO Values ​​above the threshold are set to 0, values ​​below the threshold are retained.
THRESH_TOZERO_INV Those above the threshold are retained, those below the threshold are 0
THRESH_TRUNC Anything above the threshold is the threshold, and anything below the threshold is retained.
THRESH_OTSU Otsu method automatically finds the threshold THRESH_OTUS Otsu threshold algorithm is used in conjunction with other thresholds and is most suitable for double peaks.
THRESH_TRIANGLE The triangle method automatically calculates the threshold value based on the histogram. It is most suitable for a single wave peak and was initially used for medical segmentation of cells, etc.

img

THRESH_OTUS The Otsu threshold algorithm is used in conjunction with other thresholds and is most suitable for double peaks.

THRESH_TRIANGLE The triangular binarization method automatically calculates the threshold based on the histogram. It is most suitable for a single wave peak and was initially used for medical segmentation of cells, etc.

Otsu's method and the Triangle method are two methods commonly used to automatically determine thresholds in image processing.

2.2 Otsu method principle:

The Otsu method is an adaptive threshold determination method based on histograms. Its basic principle is to find a threshold such that after the image is divided into foreground (target) and background parts according to the threshold, the inter-class variance between the target and the background maximum. The inter-class variance is used to measure the degree of difference between the target and the background. The larger the variance, the greater the difference between the target and the background, and the better the segmentation effect.

The steps of the Otsu method are as follows: 1) Calculate the grayscale histogram of the image. 2) For each possible threshold t (0 to 255), calculate the average number of pixels and pixel values ​​of the target and background. 3) Based on the current threshold t, calculate the inter-class variance as a measurement index: inter-class variance = number of target pixels * number of background pixels * (average target gray value - average background gray value)^2. 4) Find the threshold that maximizes the variance between classes as the final threshold.

2.3 Principle of triangle method:

The triangle method is a threshold selection method based on histograms. Its basic principle is to find the threshold corresponding to the peak of the histogram, by defining the slope corresponding to the peak of the histogram as the height of the triangle, and to find the threshold that maximizes the area of ​​the triangle. as the final threshold.

The steps of the triangle method are as follows: 1) Calculate the grayscale histogram of the image. 2) Find the peak of the histogram, that is, the gray level with the largest number of pixels in the histogram. 3) Based on the gray level corresponding to the peak, find two gray levels on both sides whose number of pixels in the histogram is less than half the number of pixels at the peak as the boundary. 4) Calculate the slope of the histogram between the base gray level and the two boundaries, where the slope is defined as the height of the triangle. In this case, the distance between these two boundaries can be taken as the base of the triangle. 5) Find the threshold corresponding to the slope that maximizes the triangle area as the final threshold. Another approach is to use the entire range of the histogram as the base, that is, use the distance between the minimum and maximum gray levels of the entire histogram as the base of the triangle.

3. Adaptive threshold processing

The fixed threshold method above is suitable for ideal scenes. In actual use, due to changes in the light conditions of the picture, the fixed threshold often cannot separate the target from the background well. In this case, the adaptive threshold method is needed. Adaptive thresholding dynamically adjusts the threshold based on the value of a small area to make the final output more reasonable. The function used is adaptiveThreshold()

adaptiveThreshold(
    src, 
    dst,
    maxval, 
    thresh_type, //只能为BINARY或者其INV
    type, 
    Block 
    Size, 
    C             )
  • thresh_type: Threshold calculation method, including the following 2 types:

type meaning
cv2.ADAPTIVE_THRESH_MEAN_C The weight of each pixel is equal when calculating the mean
cv2.ADAPTIVE_THRESH_GAUSSIAN_C When calculating the mean, the weight of each pixel is obtained through the Gaussian equation based on its distance to the center point.
  • Block Size: The size of the blocks in the image

  • C: Constant term in threshold calculation method

Regarding the values ​​of Block Size and C, it is generally more appropriate for Block Size to be 3 to 17, and C should not be too large. It can be 3 to 9. The specific value needs to be tested and adjusted by yourself.

The picture below shows the effect of thresholding an image using different methods. It can be seen that compared to the general thresholding operation, adaptive thresholding is very effective when there is a large difference between light and dark in the image.

img

The function cvAdaptiveThreshold can indeed binarize grayscale images, but its main function should be edge extraction, and parameter C is mainly used to control the type and thickness of edges.

The key is the block_size parameter inside, which is the size of the block that determines the local threshold.

1) When the block is very small, such as block_size=3 or 5 or 7, the degree of "adaption" is very high, that is, it is easy for the pixel values ​​in the block to be similar, so that it cannot be binarized and can only wait at the edge. Binarization is implemented where the gradient is large, and the result appears to be an edge extraction function.

2) When block_size is set to a relatively large value, such as block_size=21 or 31 or 41, cvAdaptiveThreshold is the binarization function. 3) Both src and dst must be single-channel images. Parameter blockSize: 1. Take an odd number . If you take an even number, an error will be reported after running! ! Looking at the source code, I found that a mask is needed, so the parameters must be odd numbers. OpenCV also made a detection. There is CV_Assert (blockSize %2 == 1 && blockSize > 1) at the beginning of the function adaptiveThreshold.

2.cvAdaptiveThreshold can be used for edge extraction or binarization , which is determined by the neighborhood you choose. If the neighborhood you choose is very small (such as 3×3), then it is obvious that the threshold is " The "degree of adaptation" is very high, which is reflected in the effect of edge detection in the resulting image. If the neighborhood selected is relatively large (such as 31×31), then the "adaptive degree" of the threshold will be relatively low, which will appear as a binarization effect in the resulting image.

3. Generally, the filter width should be larger than the width of the object to be recognized. The block_size is too small to represent the background. If it is too large, it will affect nearby objects.

After selecting the appropriate block_size, we can select a larger threshold param1 to better suppress noise.

4.1 THRESH_BINARY和THRESH_BINARY_INV

Mat img = imread("lena.png");
    if (img.empty())
    {
        cout << "读取失败!"<<endl;
        return -1;
    }
    //GARY PICTURE
    Mat gary;
    cvtColor(img, gary, COLOR_BGR2GRAY);
​
    //ORINGIN'SBINARY
    Mat ori_bin, ori_bin_inv, gary_bin, gary_bin_inv;
    threshold(img,ori_bin, 125, 255, THRESH_BINARY);
    threshold(img, ori_bin_inv, 125, 255, THRESH_BINARY_INV);
    imshow("ori_bin", ori_bin);
    imshow("ori_bin_inv", ori_bin_inv);
​
​
    threshold(gary, gary_bin, 125, 255, THRESH_BINARY);
    threshold(gary, gary_bin_inv, 125, 255, THRESH_BINARY_INV);
    imshow("ori_bin", gary_bin);
    imshow("ori_bin_inv", gary_bin_inv);

This code performs binary binary processing on the original color image and the grayscale image respectively; the following four images are obtained. There are also original images and grayscale images to skip:

 

 

It can be seen that multi-channel separates each channel, binarizes it, and then combines it together, so it is not a simple black and white picture but some pictures with distinctive colors, a combination of RGB

 

After converting to a single-channel grayscale image, it can be seen that the left and right images are complementary.

4.2 THRESH_TOZERO and THRESH_TOZERO_INV

Mat img = imread("lena.png");
    if (img.empty())
    {
        cout << "读取失败!"<<endl;
        return -1;
    }
    
    Mat gary;
    cvtColor(img, gary, COLOR_BGR2GRAY);
​
    //ORINGIN'S
    Mat ori_tozero, ori_bin_tozero_inv, gary_tozero, gary_tozero_inv;
    threshold(img, ori_tozero, 125, 255, THRESH_TOZERO);
    threshold(img, ori_bin_tozero_inv, 125, 255, THRESH_TOZERO_INV);
    imshow("THRESH_TOZERO", THRESH_TOZERO);
    imshow("THRESH_TOZERO_INV", THRESH_TOZERO_INV);
​
​
    threshold(gary, gary_tozero, 125, 255, THRESH_TOZERO);
    threshold(gary, gary_tozero_inv, 125, 255, THRESH_TOZERO_INV);
    imshow("gary_tozero", gary_tozero);
    imshow("gary_tozero_inv", gary_tozero_inv);

 

From left to right, they are the original image, zero, and inv; it can be seen that zero retains the ones above the threshold and sets 0 for those below the threshold, while inv sets 0 for the ones above the threshold and retains the ones below the threshold.

 

From left to right are grayscale image, zero, inv;

4.3 THRESH_TRUNC

Set the ones above the threshold as the threshold, and keep the ones below the threshold. It is relatively simple, just compare the before and after of the two pictures.

 

4. Timing of use

THERSH_BINARY, THRESH_TRUNC, THRESH_TOZERO, THRESH_OTSU, THRESH_TRIANGLE, ADAPTIVE_THRESH_MEAN_C, and ADAPTIVE_THRESH_GAUSSIAN_C are all binary processing methods. How do I know when to use which one?

A : Here are some guidelines for choosing an appropriate binarization method:

  1. THRESH_BINARY : Set the pixels whose grayscale value is greater than the threshold to the maximum value (usually 255), and set the pixels less than or equal to the threshold to 0. Suitable for images with obvious contrast , where there is a clear grayscale difference between the target and the background.

  2. THRESH_TRUNC : Set the pixels whose gray value is greater than the threshold as the threshold, and keep the pixels less than or equal to the threshold unchanged. It is suitable for situations where you want to retain the target grayscale information while performing partial suppression .

  3. THRESH_TOZERO : Keep the pixels whose gray value is greater than the threshold unchanged, and set the pixels less than or equal to the threshold to 0. Suitable for situations where you want to retain detail information in areas with higher brightness .

  4. THRESH_OTSU : Use Otsu's method to automatically calculate the threshold to maximize the inter-class variance between the target and the background. It is suitable for situations where there is obvious grayscale separation between the target and the background of the image , but the specific threshold value is uncertain.

  5. THRESH_TRIANGLE : Use the triangle method to automatically calculate the threshold to maximize the triangle area based on the histogram. It is suitable for situations where there is obvious grayscale separation between the target and the background of the image, but the specific threshold value is uncertain.

  6. ADAPTIVE_THRESH_MEAN_C: Adaptive thresholding method based on local image mean, where the threshold is calculated based on the neighborhood around each pixel. Suitable for situations where different areas of the image have different lighting conditions .

  7. ADAPTIVE_THRESH_GAUSSIAN_C: Adaptive thresholding method based on local image weighted average, where the threshold is calculated based on the neighborhood around each pixel. Use Gaussian weights to calculate local averages, suitable for situations where different areas of the image have different lighting conditions.

Choosing the appropriate method depends on the characteristics of the image and application requirements. If the image has significant object-background separation, try using THRESH_BINARY, THRESH_OTSU, or THRESH_TRIANGLE. If the image has different lighting conditions or needs to preserve detail information, you can consider using the adaptive threshold method (ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C).

What is the difference between the underlying implementation processes of ADAPTIVE_THRESH_GAUSSIAN_C and ADAPTIVE_THRESH_MEAN_C?

Answer : ADAPTIVE_THRESH_MEAN_C and ADAPTIVE_THRESH_GAUSSIAN_C are two different adaptive threshold methods. There are the following differences in the underlying implementation process:

The implementation process of ADAPTIVE_THRESH_MEAN_C (adaptive threshold method based on local mean) is as follows:

  1. For each pixel in the image, a local neighborhood (size determined by the specified block size parameter) is determined.

  2. Calculate the average (local mean) of pixels within this neighborhood.

  3. Use this local mean as the threshold for the current pixel.

  4. Sets the pixel to white or black based on comparing the current pixel's gray value with its corresponding local mean.

The implementation process of ADAPTIVE_THRESH_GAUSSIAN_C (adaptive threshold method based on local weighted average) is as follows:

  1. For each pixel in the image, a local neighborhood (size determined by the specified block size parameter) is determined.

  2. Calculate the weighted average of the pixels in this neighborhood, where the weights are calculated by the Gaussian window function (the closer to the center pixel, the greater the weight).

  3. Use the weighted average as the threshold for the current pixel.

  4. Sets the pixel to white or black based on comparing the current pixel's grayscale value with its corresponding threshold.

the difference:

  1. The thresholds are calculated differently: ADAPTIVE_THRESH_MEAN_C uses the local mean as the threshold, while ADAPTIVE_THRESH_GAUSSIAN_C uses the local weighted average as the threshold.

  2. The weights are calculated differently: ADAPTIVE_THRESH_MEAN_C does not consider the weights between pixels, while ADAPTIVE_THRESH_GAUSSIAN_C uses a Gaussian window function to calculate the weights so that pixels closer to the central pixel have higher weights.

  3. The effects are different: Due to the difference in weight calculation, ADAPTIVE_THRESH_GAUSSIAN_C has a better effect on image smoothing and noise suppression, while ADAPTIVE_THRESH_MEAN_C is simpler and more direct.

The choice of which method to use depends on the characteristics of the image and application requirements. If there is significant noise in the image or you need better smoothing, consider using ADAPTIVE_THRESH_GAUSSIAN_C. If simple averaging is sufficient, or if faster calculations are required, ADAPTIVE_THRESH_MEAN_C can be selected.

Guess you like

Origin blog.csdn.net/dagengen12138/article/details/131196730