Opencv-C++ Notes (8): opencv-color model and conversion

1. RGB color model

The RGB color model has been introduced earlier. The name of the model is composed of the English initials of three colors, namely red (Red), green (Green) and blue (Blue). Although the color model is named with red first, it is in the opposite order in OpenCV. The first channel is the blue (B) component, the second channel is the green (G) component, and the third channel is the Red (R) component.

Depending on the storage order, OpenCV4 provides a reverse order format of this order for storing images whose first channel is the red component, but the color spaces of images in these two formats are the same, as shown in Figure 3. -1 shown. The three channels have the same range for color description, so the spatial composition of the RGB color model is a cube.

In the RGB color model, all colors are obtained by mixing these three colors in different proportions. If the three color components are all 0, it is represented as black. If the three color components are the same and are the maximum value, is represented as white. Each channel represents the process of a certain color from 0 to 1. Images with different bits represent the process of subdividing this color change into different levels. For example, each channel of an image in 8U3C format quantizes this process into 256 levels. , represented by 0 to 255, respectively. Adding the fourth channel on the basis of this model is the RGBA model. The fourth channel represents the transparency of the color. When there is no transparency requirement, the RGBA model will degenerate into an RGB model.

insert image description here

Two, YUV color model

The YUV model is the color coding method used by the TV signal system. These three variables represent the brightness of the pixel (Y) and the signal difference between the red component and the brightness (U) and the difference between the blue and brightness (V). This color model is mainly used for the transmission of video and images, and the generation of this model is closely related to the development of TV sets. Since color televisions came after black and white televisions were invented, video signals for color televisions need to be compatible with black and white televisions. Color TVs need three channels of data to display color, while black and white TVs only need one channel of data. Therefore, in order to make the video signal compatible with color TVs and black and white TVs, the RGB encoding method is converted to YUV encoding method. Its Y channel is the brightness of the image. Black and white TVs only need to use this channel to display black and white video images, and color cameras can display color images on color TVs by converting YUV encoding into RGB encoding, which is a better solution. The problem that the same video signal is compatible with different types of TVs. The conversion relationship between the RGB model and the YUV model is shown in the formula, where the value range of RGB is 0-255.
insert image description here

3. HSV (HSB) color model

HSV is an abbreviation for Hue, Saturation, and Value. It can also be seen from the name that the model describes colors through these three characteristics. Chroma is the basic attribute of color, which is the color we usually say, such as red, blue, etc.; saturation refers to the purity of the color. The higher the saturation, the purer and brighter the color, and the lower the saturation, the color will gradually become gray. Darken, the value range of saturation is from 0 to 100%; brightness is the brightness of the color, and its value range is from 0 to the maximum value allowed in the computer. Since the value ranges of hue, saturation, and brightness are different, the color space model is represented by a cone, and its shape is shown in Figure 3-2. Compared with the shortcomings of the unintuitive connection between the three color components of the RGB model and the final color, the HSV model is more in line with the way humans perceive colors: color, depth, and brightness.
insert image description here
RGB=>HSB

V=max(R,G,B)
S=(V-min(R,G,B))*255/V if V!=0, 0 otherwise

   (G - B)*60/S,  if V=R

H= 180+(B - R)*60/S, if V=G
240+(R - G)*60/S, if V=B

If H<0, then H=H+360
Use the formula above from 0° to 360° to calculate the hue (hue) value, make sure they can be used for 8 bits after dividing by 2.

4. LAB color model

The Lab color model makes up for the deficiency of the RGB model. It is a device-independent color model and a color model based on physiological characteristics. In the model, L represents Luminosity, and a and b are two color channels, both of which range from -128 to +127, where the corresponding color of channel a changes from green to large. Red, b channel value from small to large corresponds to the color from blue to yellow. The color space it constitutes is a sphere, the form of which is shown in Figure 3-3
insert image description here

Five, GRAY color model

The GRAY model is not a color model, it is a grayscale image model, and its name uses the uppercase letters of the English word gray. The grayscale image has only a single channel, and the grayscale value is represented from black to white in sequence from 0 to maximum according to the number of image digits. For example, in the 8UC1 format, black to white is quantized into 256 levels, expressed by 0-255, where 255 means white. Color images have the characteristics of rich colors and high information content, but grayscale images still have certain advantages in image processing. For example, grayscale images have the advantages of the same size and the same compression format, small capacity, easy collection, and convenient transmission. The commonly used RGB model is converted into a grayscale image as shown in the formula.
insert image description here

6. CMYK color mode

This is a color mode used in color printing. It consists of four colors: Cyan, Magenta, Yellow and Black. The reason why black is represented by K is to avoid confusion with blue (Blue, represented by B) in the three primary colors of RGB. The creation basis of this mode is different from RGB, it does not rely on adding light, but subtracting light, because unlike monitors or TVs, printing paper cannot create light sources, it does not emit light, it can only absorb and Reflect light. Therefore, through the combination of the above four colors, most of the colors in the visible spectrum can be produced.

RGB<=CMYK

R = (255 - C) * ((255 - K) / 255)
G = (255 - M) * ((255 - K) / 255)
B = (255 - Y) * ((255 - K) / 255)

7. Gray model

It is not a color model, but a grayscale image model, whose name uses the uppercase letters of the English word gray. The grayscale image has only one channel, and the grayscale value represents from black to white in sequence from 0 to maximum according to the number of image bits. Commonly used RGB models are converted into grayscale images:
insert image description here

Eight, the conversion of different colors

For the mutual conversion between different color models of images, OpenCV 4 provides the cvtColor() function to realize the conversion function. The function prototype of this function is as follows

void cv::cvtColor(InputArray src,
                  OutputArray dst,
                  int code,
                  int dstCn = 0)

src: The original image of the color model to be converted.
dst: the destination image after converting the color model.
code: the flag of color space conversion, such as from RGB space to HSV space. Common symbols and their meanings are given in Table 3-1.
dstCn: the number of channels in the target image, if the parameter is 0, the number of channels is automatically derived from src and code.

The function is used to convert an image from one color model to another. The first two parameters are used to input the image to be converted and the target image after the converted color space, and the third parameter is used to declare the specific conversion model space of the function. In general, the fourth parameter does not require special settings, and the default parameters can be used. It should be noted that the value range of the image before and after the function transformation, because the pixels of the 8-bit unsigned image are from 0 to 255, the pixels of the 16-bit unsigned image are from 0-65535, and the pixels of the 32-bit floating-point image are from 0 to 1, so be sure to pay attention to the pixel range of the target image. In the case of linear transformations, the range issue does not need to be considered, and the pixels of the target image will not go out of range. If in the case of nonlinear transformation, the input RGB image should be normalized to an appropriate range to obtain the correct result, for example, to convert an 8-bit unsigned image into a 32-bit floating-point image, you need to first divide the image pixels by 255 scaled to the 0 to 1 range to prevent erroneous results.
[Note] If the alpha channel (the fourth channel in the RGB model, indicating transparency) is added during the conversion process, its value will be set to the maximum value of the corresponding channel range: CV_8U is 255, CV_16U is 65535, CV_32F is 1 for
insert image description here
intuitive To feel how the same image looks in different color spaces, the program for converting the previous color models to each other is given in Code Listing 3-2, and the running results are shown in Figure 3-4. It should be noted that the Lab color model has negative numbers, and images displayed through the imshow() function cannot display negative numbers, so the results show how the images displayed by the Image Watch plug-in in the Lab model look like. In the program, we first convert the CV_8U type to CV_32F type and then convert the color model in order to prevent the value from being out of bounds after the conversion.

#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("/home/wyh/Documents/C++demo/699342568.jpg");
    if(img.empty()){
    
    
        cout<<"请确认图像文件名称是否正确"<<endl;
        return -1;
    }
    Mat dst;
    resize(img,dst,Size(img.cols*0.5,img.rows*0.5));
    Mat gray,HSV,YUV,Lab,img32;
    dst.convertTo(img32,CV_32F,1.0/255);//将CV_8U类型转换成CV_32F类型
    cvtColor(img32,HSV,COLOR_BGR2HSV);
    cvtColor(img32,YUV,COLOR_BGR2YUV);
    cvtColor(img32,Lab,COLOR_BGR2Lab);
    cvtColor(img32,gray,COLOR_BGR2GRAY);
    imshow("原图",dst);
    imshow("HSV",HSV);
    imshow("YUV",YUV);
    imshow("Lab",Lab);
    imshow("gray",gray);
    waitKey(10000);
    return 0;
}

insert image description here

Guess you like

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