OpenCV Notes (5): Basic concepts of binary images

Part11. Related basic concepts

Beginners may be easily confused between grayscale images and binary images, as well as image grayscale and image binarization. Below, these professional terms are explained in detail.

Binary Image means that there are only two gray levels in the image. The gray value of any pixel in the image is 0 or 255, representing black and white respectively. Usually in a binary image, white pixels represent the foreground of the object , and black pixels represent the background of the object. This achieves the separation of the target of interest and the background.

Grayscale Image is not only black and white, but also various grays, so the color information is richer. Grayscale images are single-channel, which can facilitate calculation and processing. They can also be used to obtain edge information and gradient information of the image for further analysis and processing.

Usually, each pixel of a grayscale image is represented by 8 bits, so there are 0-255 grayscale values, a total of 256 (2 to the 8th power). If each pixel is represented by 16 bits, there are 65536 grayscale values ​​(2 to the 16th power).

Edges are the basic features of images. The edge is the intersection between an image area and another attribute area. It is the place where the area attributes mutate. It is the place with the greatest uncertainty in the image and the place where the image information is most concentrated. The edge of the image contains rich information.

Gradient is the derivation of a two-dimensional discrete function . If the image is viewed as a two-dimensional discrete function, the gradient can be used to measure the rate of change of the grayscale of the image.

Color images have richer detailed information than binary images and grayscale images. Each pixel of a color image is usually represented by three channels: red (R), green (G), and blue (B). The pixel value of each channel is between [0, 255].

Image grayscale is the process of converting a color image into a grayscale image . Grayscale will make each pixel in the pixel matrix satisfy:

R=G=B

After the image is grayscaled, the matrix dimension is reduced and the computing speed will be greatly improved. At the same time, grayscale images can also reflect illumination intensity, and the gradient information of the image is still retained. Therefore, we give priority to grayscale processing of color images.

Image Binarization , also known as image thresholding , is the simplest method of image segmentation . Binarization can convert grayscale images into binary images. The step of binarization is to set the grayscale of pixels greater than a certain critical grayscale value to the maximum grayscale value (255), and to set the grayscale of pixels smaller than this value to the minimum grayscale value (0), thereby achieving Binarization. Binary images are very useful in many scenarios, such as contour analysis, contour matching, contour feature extraction, object measurement, morphological processing, etc.

Part22. Grayscale and binarization

There are many ways to achieve image grayscale and image binarization.

12.1 Grayscale method

Common grayscale methods include:

  • Maximum value grayscale processing: use the maximum value of the three color channels of red, green, and blue as the grayscale value.

Calculation formula:

  • Floating-point grayscale processing: Multiply the three color channels of red, green, and blue by different floating-point weights, where the sum of the RGB weights is 1 to obtain a grayscale value.

Calculation formula:

  • Integer grayscale processing: In order to avoid floating point number operations, integer arithmetic is used, in which the sum of the weights of RGB is 100, and a grayscale value is obtained.

Calculation formula:

  • Shift grayscale processing: Shifting is faster than integer grayscale processing.

Calculation formula:

  • Average grayscale processing: Add the values ​​of the three color channels of red, green, and blue and divide by 3 to obtain a grayscale value.

Calculation formula:

  • Weighted average grayscale processing: weighted average the three-channel components of the color image according to the importance of each channel, and then use the weighted average obtained as the grayscale value of the grayscale image.

Calculation formula:

  • Single-channel method: only take green as the gray value.

Calculation formula:

OpenCV can convert color images into grayscale images through the cvtColor() function.

int main(int argc,char *argv[])
{
    Mat src = imread(".../street.jpg");
    imshow("src",src);
    Mat gray;
    cvtColor(src,gray,COLOR_BGR2GRAY);
    imshow("gray",gray);

    waitKey(0);
    return 0;
}
4a6773bf9225b4d9968f6e746b45de54.jpeg
Grayscale.png

The function of the cvtColor() function is color space conversion. Common color spaces include: RGB, HSV, YCrCb, etc. OpenCV provides hundreds of color space conversion methods. In our daily use, the most widely used ones are BGR↔GRAY and BGR↔HSV. Subsequent articles will also introduce color space conversion in detail.

22.2 Binarization method

In OpenCV, image binarization can be achieved by using the threshold() function of threshold segmentation, the inRange() function of color image segmentation, the Canny() function of edge detection, etc.

First, we introduce the simplest image binarization method, which is binarized according to a fixed threshold . First grayscale the image , then traverse each pixel in the grayscale image, and assign different values ​​to the pixels at the corresponding positions in the output image according to whether its pixel value is greater than a fixed threshold. The specific mathematical formula is as follows:

,

Among them, x represents the value of the pixel on the grayscale image, and thresh represents the fixed threshold. The Binary(x) function reflects the value of the corresponding pixel point in the output image after the grayscale image is binarized .

In the following example, the original image is first grayscaled, then the mean function mean() is used to obtain the mean of the grayscale image, and then the mean is used as the fixed threshold t. Then start traversing the grayscale image, and when the grayscale value of the pixel is greater than t, assign it a value of 255. Otherwise, the value is assigned to 0.

int main(int argc,char *argv[])
{
    Mat src = imread(".../street.jpg");
    imshow("src",src);
    Mat dst;
    cvtColor(src,dst,COLOR_BGR2GRAY);
    Scalar m = mean(dst);
    int t = m[0];
    int height = src.rows;
    int width = src.cols;

    Mat result = Mat::zeros(height,width,CV_8UC1);

    for (int row = 0;row < height;row++) {
        for (int col = 0;col < width;col++) {
            int p = dst.at<uchar>(row,col);

            if (p>t) {
                result.at<uchar>(row,col) = 255;
            } else {
                result.at<uchar>(row,col) = 0;
            }
        }
    }

    imshow("result",result);
    waitKey(0);
    return 0;
}
0fe601864831e58a9cd546f6ac31bfa0.jpeg
Binarization.png

Of course, this method has major drawbacks. It can only process a single image and cannot automatically process a large number of images. Subsequent articles will introduce in detail the use of threshold() function to achieve image binarization. Through this function, OpenCV provides a series of classic binarization-related algorithms.

Let's use an example to first experience the use of the threshold() function:

int main(int argc,char *argv[])
{
    Mat src = imread(".../street.jpg");
    imshow("src",src);
    Mat gray;
    cvtColor(src,gray,COLOR_BGR2GRAY);
    Scalar m = mean(gray);
    int t = m[0];

    Mat thresh;
    threshold(gray, thresh,t,255, THRESH_BINARY);
    imshow("thresh1",thresh);

    double value= threshold(gray, thresh,0,255, THRESH_BINARY|THRESH_OTSU);
    imshow("thresh2",thresh);

    cout << "thresh1 = " << t <<", thresh2 = " << value <<endl;
    waitKey(0);
    return 0;
}

Output result:

thresh1 = 115, thresh2 = 121
f71f8b0424be346f2fd0d8c3942ac145.jpeg
Multiple threshold segmentation.png

The above two threshold segmentations have slightly different effects to the naked eye. In fact, they use different thresholds for segmentation. It can be seen from the output results that the thresholds of the two are different.

Among them, here

threshold(gray, thresh,t,255, THRESH_BINARY);

Equivalent to the above example of using fixed threshold segmentation and traversing grayscale images.

Part33. Summary

This article mainly introduces grayscale images and binary images, as well as the concepts of image grayscale and image binarization. And tell us how to achieve image grayscale and image binarization. Some common algorithms for image binarization will be explained in special articles next.

Grayscale images and binary images are the basis of digital image processing. When processing images, we will first grayscale and binarize the image, and then do the next step.

[ Java and Android Technology Stack] Public Account

Pay attention to Java/Kotlin server, desktop, Android, machine learning, and client-side intelligence

For more exciting content, please pay attention to:

Guess you like

Origin blog.csdn.net/SLFq6OF5O7aH/article/details/134257601