Opencv-C++ Notes (11): opencv-image binarization and LUB lookup table

I. Overview

We generated an image with only black and white in the previous section of the program. The gray value of this "black or white" image pixel has only two values, the maximum value and the minimum value, no matter what data type it is in. Therefore it is called a binary image. There are few types of binary images, which can be highly compressed and save storage space. The process of converting non-binary images into binary images through calculation is called image binarization. Two functions, threshold() and adaptiveThreshold() are provided in OpenCV4 to implement image binarization. We first introduce the usage of the threshold() function. The function prototype of this function is given in Code Listing 3-17.

double cv::threshold(InputArray src,OutputArray dst,double  thresh,double  maxval,int  type)
  • src: The image to be binarized. The image can only be of two data types: CV_8U and CV_32F. The requirements for the number of image channels are related to the selected binarization method.
  • dst: The binarized image, which has the same size, data type, and number of channels as the input image.
  • thresh: The threshold for binarization.
  • maxval: The maximum value of the binarization process. This function is only used in the two binarization methods of THRESH_BINARY and THRESH_BINARY_INV, but it also needs to be input when using other methods.
  • type: A flag to select the image binarization method.

This function is an integration of many binarization methods, all of which implement a function, that is, given a threshold, calculate the relationship between the gray value of all pixels and the threshold, and obtain the final comparison result. The gray value of the output result of some threshold comparison methods in the function is not binary, but has a value range, but in order to reflect its most commonly used function, we still call it a binarization function or a threshold comparison function. Some parameters and return values ​​of the function are only useful for specific algorithms, but even if these algorithms are not used, they need to be explicitly given when using the function, and cannot be defaulted. The last parameter of the function is a flag to select the binarization calculation method. You can choose the binarization method and control which parameters affect the calculation result of the function. The range and meaning of the flag that can be selected are given in Table 3-2.
insert image description here

Next, the binarization principle and required parameters corresponding to each flag will be introduced in detail.

Two, THRESH_BINARY and THRESH_BINARY_INV

These two flags are opposite binarization methods. THRESH_BINARY compares the grayscale value with the threshold (the third parameter thresh). If the grayscale value is greater than the threshold, change the grayscale value to the fourth parameter maxval in the function. value, otherwise change the grayscale value to 0. The THRESH_BINARY_INV flag is just the opposite of this process. If the gray value is greater than the threshold, the gray value is changed to 0, otherwise the gray value is changed to the value of maxval. The calculation formulas of these two flags are given in formula (3.7).
insert image description here

Three, THRESH_TRUNC

This flag is equivalent to resetting a new maximum value for the gray value of the image, and resetting all the gray values ​​greater than the new maximum value to the new maximum value. The specific logic is to compare the gray value with the threshold thresh , if the gray value is greater than thresh, change the gray value to thresh, otherwise keep the gray value unchanged. This method does not use the value of the fourth parameter maxval in the function, so the value of maxval has no effect on this method. The calculation formula for this sign is given in formula (3.8).

insert image description here

Four, THRESH_TOZERO and THRESH_TOZERO_INV

These two flags are opposite threshold comparison methods. THRESH_TOZERO means to compare the gray value with the threshold thresh. If the gray value is greater than thresh, it will remain unchanged, otherwise the gray value will be changed to 0. The THRESH_TOZERO_INV method is the opposite, comparing the gray value with the threshold thresh, if the gray value is less than or equal to thresh, it will remain unchanged, otherwise the gray value will be changed to 0. Neither of these two methods uses the value of the fourth parameter maxval in the function, so the value of maxval has no effect on this method. The calculation formulas of these two flags are given in formula (3.9).
insert image description here
The first five flags all support the input of multi-channel images, and threshold comparisons are performed for each channel during calculation. In order to understand the above threshold comparison method more intuitively, we assume that the gray value of the image is a continuously changing signal, compare the threshold comparison method to a filter, and draw the shape of the signal after the continuous signal passes through the filter. The result is shown in Figure 3-14 , the red line in the figure is the set threshold, and the black line is the signal shape after the original signal passes through the filter.
insert image description here

5. THRESH_OTSU and THRESH_TRIANGLE

These two flags are methods for obtaining thresholds, not threshold comparison methods. These two flags can be used together with the previous five flags, such as "THRESH_BINARY|THRESH_OTSU". The first five flags need to manually set the threshold value when calling the function. If the threshold value set is unreasonable if you don’t know the image, it will have a serious impact on the processed effect. ) and triangle method (TRIANGLE)** combined with the gray value distribution characteristics of the image to obtain the threshold value of binarization, and the threshold value is given in the form of the return value of the function. Therefore, if any of these two flags is set in the last parameter of the function, the third parameter thresh of the function will be automatically given by the system, but it still cannot be defaulted when calling the function, but the program will not use this value. It should be noted that so far OpenCV
4 only supports the input of images of type CV_8UC1 for these two flags.

The threshold() function uses only one threshold value globally. In actual situations, due to uneven illumination and the existence of shadows, only one threshold value globally will make the white area in the shadow area also binarized into black by the function, so the adaptiveThreshold() function Two binarization methods of local adaptive threshold are provided, and the function prototype of this function is given in Code Listing 3-18.

void cv::adaptiveThreshold(InputArray src,
                                 OutputArray dst,
                                double  maxValue,
                                int  adaptiveMethod,
                                 int  thresholdType,
                                 int  blockSize,
                                double   C
                                  )
  • src: the image to be binarized, the image can only be of CV_8UC1 data type.
  • dst: The binarized image, which has the same size and data type as the input image.
  • maxValue: The maximum value of binarization.
  • adaptiveMethod: The self-made method for determining the threshold, which is divided into two types: the mean method ADAPTIVE_THRESH_MEAN_C and the Gaussian method ADAPTIVE_THRESH_GAUSSIAN_C.
  • thresholdType: The flag for selecting the image binarization method, which can only be THRESH_BINARY and THRESH_BINARY_INV.
  • blockSize: Adaptively determine the size of the pixel neighborhood of the threshold, generally an odd number of 3, 5, or 7. C: A constant to subtract from the average or weighted average, which can be positive or negative.

This function converts the grayscale image into a binary image, adaptively calculates the threshold value in the blockSize* blockSize neighborhood through the mean method and the Gaussian method, and then performs binarization. The principle is the same as the previous one, so I won’t go into details here.

In order to intuitively experience the effect of image binarization, sample programs for binarizing color images and grayscale images are given in Code Listing 3-19

#include<iostream>
#include<vector>
#include<string>
#include <opencv2/opencv.hpp>
#include "opencv/highgui.h"

using namespace std;
using namespace cv;

int main(int argc,char** argv) {
    
    
    cout<<"OpenCv Version: "<<CV_VERSION<<endl;
    Mat img=imread("699342568.jpg");
    if(img.empty()){
    
    
        cout<<"请确认输入的图片的路径是否正确"<<endl;
        return -1;
    }

    Mat gray;
    cvtColor(img,gray,COLOR_BGR2GRAY);
    Mat img_B,img_B_V,gray_B,gray_B_V,gray_T,gray_T_V,gray_TRUNC;

    //彩色图像二值化
    threshold(img,img_B,125,255,THRESH_BINARY);
    threshold(img,img_B_V,125,255,THRESH_BINARY_INV);
    imshow("img_B",img_B);
    imshow("img_B_V",img_B_V);

    //灰度图像二值化
    threshold(gray,gray_B,125,255,THRESH_BINARY);
    threshold(gray,gray_B_V,125,255,THRESH_BINARY_INV);
    imshow("gray_B",gray_B);
    imshow("gray_B_V",gray_B_V);

    //灰度图像TOZERO变换
    threshold(gray,gray_T,125,255,THRESH_TOZERO);
    threshold(gray,gray_T_V,125,255,THRESH_TOZERO_INV);
    imshow("gray_T",gray_T);
    imshow("gray_T_V",gray_T_V);

    //灰度图像TRUNC变换
    threshold(gray,gray_TRUNC,125,255,THRESH_TRUNC);
    imshow("gray_TRUNC",gray_TRUNC);

    //灰度图像大津法和三角法二值化
    Mat img_Thr=imread("threshold.jpg",IMREAD_GRAYSCALE);
    Mat img_Thr_0,img_Thr_T;
    threshold(img_Thr,img_Thr_0,100,255,THRESH_BINARY|THRESH_OTSU);
    threshold(img_Thr,img_Thr_T,125,255,THRESH_BINARY|THRESH_TRIANGLE);
    imshow("img_Thr",img_Thr);
    imshow("img_Thr_0",img_Thr_0);
    imshow("img_Thr_T",img_Thr_T);

    //灰度图像自适应二值化
    Mat adaptive_mean,adaptive_gauss;
    adaptiveThreshold(img_Thr,adaptive_mean,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,55,0);
    adaptiveThreshold(img_Thr,adaptive_gauss,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,55,0);

    imshow("adaptive_mean",adaptive_mean);
    imshow("adaptive_gauss",adaptive_gauss);
    waitKey(0);
    return 0;
}

insert image description here

6. LUT lookup table

There is only one threshold in the threshold comparison method. If multiple thresholds need to be compared, a look-up table (Look-Up-Table, LUT) needs to be used. In simple terms, the LUT lookup table is a mapping table of pixel gray values, which uses the pixel gray value as an index, and uses the value after the gray value mapping as the content of the table. For example, we have an array with a length of 5 to store characters. The LUT lookup table maps 0 to a, 1 to b, and so on through this array. The mapping relationship is . The LUT() function is provided in OpenCV
4 to realize the LUT lookup table function of the image pixel gray value, and the prototype of the function is given in the code list 3-20.

 void cv::LUT(InputArray src,
                   InputArray lut,
                   OutputArray dst
                  )
  • src: input image matrix, its data type can only be CV_8U.

  • lut: A lookup table of 256 pixel gray values, single channel or the same number of channels as src.

-dst: output image matrix with the same size as src and the same data type as lut.

The data type required by the first input parameter of this function must be CV_8U type, but it can be a multi-channel image matrix. According to the parameter description of the second parameter, we can know that the input is a 1×256 matrix, which stores the value after the gray value of each pixel is mapped, and its form is shown in Figure 3-17. If the second parameter is a single channel, each channel in the input variable is mapped according to a LUT lookup table; if the second parameter is multi-channel, the i-th channel in the input variable is mapped according to the second parameter The ith channel LUT lookup table for mapping. Different from the previous function, the data type of the function output image is not consistent with the data type of the original image, but is consistent with the data type of the LUT lookup table. This is because the original gray value is mapped to the new space, so Need to be consistent with the data types in the new space.
insert image description here

In order to understand the effect of the LUT lookup table after processing the image, a sample program for processing the grayscale image and the color image through the LUT() function is given in the code list 3-21, and the single-channel and three-channel lookup tables are used in the program. Map a color image

#include<iostream>
#include<vector>
#include<string>
#include <opencv2/opencv.hpp>
#include "opencv/highgui.h"

using namespace std;
using namespace cv;

int main(int argc,char** argv) {
    
    
    cout<<"OpenCv Version: "<<CV_VERSION<<endl;
    //LUT查找表第一层
    uchar lutFirst[256];
    for(int i=0;i<256;++i){
    
    
        if(i<=100)lutFirst[i]=0;
        else if(i>100&&i<=200)lutFirst[i]=100;
        else lutFirst[i]=255;
    }
    Mat lutOne(1,256,CV_8UC1,lutFirst);

    //LUT查找表第二层
    uchar lutSecond[256];
    for(int i=0;i<256;++i){
    
    
        if(i<=100)lutSecond[i]=0;
        else if(i>100&&i<=150)lutSecond[i]=100;
        else if(i>150&&i<=200)lutSecond[i]=150;
        else lutSecond[i]=255;
    }
    Mat lutTwo(1,256,CV_8UC1,lutSecond);

    //LUT查找表第三层
    uchar lutThird[256];
    for(int i=0;i<256;++i){
    
    
        if(i<=100)lutThird[i]=0;
        else if(i>100&&i<=200)lutThird[i]=100;
        else lutThird[i]=255;
    }
    Mat lutThree(1,256,CV_8UC1,lutThird);

    //拥有三通道的LUT查找表矩阵
    vector<Mat>mergeMats;
    mergeMats.push_back(lutOne);
    mergeMats.push_back(lutTwo);
    mergeMats.push_back(lutThree);
    Mat LutTree;
    merge(mergeMats,LutTree);

    //计算图像的查找表
    Mat img=imread("lena.jpeg");
    if(img.empty()){
    
    
        cout<<"请确认输入的图片路径是否正确"<<endl;
        return -1;
    }
    Mat gray,out0,out1,out2;
    cvtColor(img,gray,COLOR_BGR2GRAY);
    LUT(gray,lutOne,out0);
    LUT(img,lutOne,out1);
    LUT(img,LutTree,out2);
    imshow("out0",out0);
    imshow("out1",out1);
    imshow("out2",out2);
    waitKey(0);
    return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/jiyanghao19/article/details/131286142