Convert color image to grayscale image

digital image

     Most of the images we come into contact with now are digital images. After the image is digitized, each pixel can be regarded as a small square, and each small square stores the pixel information of the image. If a digital image is abstracted, it is a two-dimensional matrix (grayscale image) or a three-dimensional matrix (color image) .

color image

     Any color is composed of three primary colors red, green and blue. Represented by a two-dimensional matrix of red, green, and blue triplets (thus forming three channels), and abstracted together to form a three-dimensional array. Each value of the triplet is also between 0-255 , and 0 indicates that the corresponding primary color does not exist in the pixel, and 255 indicates that the corresponding primary color obtains the maximum value in the pixel. By adjusting the brightness of the gray value of each channel, the three primary colors in the three channels can be matched differently, thus forming a colorful world of colors! ! We can regard these three primary colors (red, green, blue) as three kinds of pigments, and the gray level in each color channel is regarded as the palette of each pigment. The larger the gray level, the corresponding channel The closer the color is to the three primary colors. For example, for an 8bit color picture, the gray level is 0~255. If the gray level in the 0th channel (R) is 255, the color palette displayed in this channel is red. If the gray level is less than 255, the red will be more It becomes lighter and lighter, and when it reaches 0, it means that the base color of red is gone in channel 0, and so on, the first channel (G) and the second channel (B) are also the same principle, and then the base colors of these three swatches overlap Together, it is like mixing the three primary color pigments together, thus forming a color image.

Grayscale image

     The brightness of each pixel is represented by a numerical value, the value range is 0-255, 0 represents black, 255 represents white , and other values ​​represent gray levels between black and white, abstracted to form a two-dimensional array. A grayscale image has no color, and its color is between black and white. 255 means white, 0 means black, and the gray level is between different levels of gray.

image conversion

     The most basic thing to convert a color image into a grayscale image is to consider how to allocate the grayscale levels in the three channels . If you simply take out all the grayscale values ​​​​in the R channel directly, it will also form a grayscale image. Similarly, Taking out the G channel and B channel is also a grayscale image, but we generally don't do this. After consulting the official manual, we can think about converting the gray value of the three channels of the color image through the following methods:

    Floating point arithmetic: Gray=0.299R+0.587G+0.114B

    Integer method: Gray=(R30+G59+B*11)/100

    Shift method: Gray=(R28+G151+B*77)>>8

    Average method: Gray=(R+G+B)/3

    Maximum method: Gray = max(R,G,B)

    Minimum method: Gray = min(R,G,B)

    Only take green: Gray=G

    R, G, and B in the above formula represent the grayscale values ​​in these three channels . Why is there such a different ratio conversion? Because the sensitivity of our human eyes to colors is different, we are more sensitive to green, followed by red, and finally blue. Therefore, the gray value in different channels is weighted, and the gray value obtained after weighting is the gray value of our converted gray image, so the gray image obtained in this way is more in line with the intuitive image of our human eyes. After conversion, it is stored in the corresponding two-dimensional array. This array is a two-dimensional array in the abstract sense of the converted grayscale image, and it is displayed as a grayscale image. This is how a color image is converted to a grayscale image.

Write an image color space conversion grayscale image algorithm

(1) Guide the package, read the picture, and divide the three channel values;

(2) Use the average value, maximum value, minimum value, classic weighting of the three channels and the cvtColor function conversion of the OpenCV library as the value of the final grayscale image;

a. Create a storage image matrix, calculate the average value, maximum value, minimum value, classic weighted transformation and cvtColor function results;

b. Display pictures through imshow;

c. Use the original RGB image to obtain the following image after processing;

 

Conclusion: The result calculated by using the weighting formula is basically the same as the result of the built-in function of OpenCV, so the two are the closest. The picture generated by the maximum value is brighter, the average value is centered, and the minimum value is darker.

(3) Optimize the program code with Numpy built-in functions to improve its running speed.

a. Use the time library to time the optimized code;

 

b. Display pictures through imshow and add monitor "ESC", cancel all pictures when clicked;

 

c. Optimize the picture to be consistent with the data generated by the for loop;

d. The comparison shows the running speed between the two;

Conclusion: The processing speed of data processing by matrix operation in Numpy has been significantly improved compared with the traditional for loop assignment processing of each pixel, which fully demonstrates the superiority of Numpy in matrix operation processing.

References: https://blog.csdn.net/qq_44820108/article/details/121702791

Guess you like

Origin blog.csdn.net/qq_42829848/article/details/126875511