### table of Contents

# Preface

In the previous, we only introduced three types of images, namely binary images, grayscale images and RGB images. But the image we use now is definitely an RGB image, but it is only a type of color space. In the actual image, there are many other color spaces, which will certainly not be unfamiliar to readers who know PS.

Such as GRAY color space (grayscale image), XYZ color space, YCrCb color space, HSV color space, HLS color space, CIEL *a* b *color space, CIEL* u *v* color space, Bayer color space, etc.

Each image has its content that is good at processing, so we need to master the conversion of these color space images in order to deal with image problems more conveniently.

# GRAY color space

GRAY is the grayscale image we introduced earlier, usually referring to an 8-bit grayscale image, which has 256 gray levels, and the pixel value range is [0,255].

The mathematical formula of RGB conversion bit GRAY is as follows:

Gray=0.229*R+0.587*G+0.114*B

When the image is converted from GRAY color space to RGB color space, the final values of all channels are the same, and the processing method is as follows:

R=Gray

G=Gray

B=Gray

# XYZ color space

XYZ color space is defined by CIE (International Commission on Illumination). It is a more convenient color space for calculation. Unlike RGB conversion bit GRAY, it can only be converted in one direction. XYZ color space and RGB conversion will not lose any value. .

To convert RGB color space to XYZ color space, the conversion formula is:

To convert XYZ color space to RGB color space, the conversion formula is:

# YCrCb color space

The human visual system's sensitivity to color is lower than its sensitivity to brightness. In the traditional RGB color space, the three primary colors of RGB have the same importance, but the brightness information is ignored. Therefore, there is the YCrCb color space.

In the YCrCb color space, Y represents the brightness of the light source, and chromaticity information is stored in Cr and Cb, where Cr represents the red component information, and Cb represents the blue component information.

Brightness gives information about how bright or dark the color is, and this information can be calculated by the weighted sum of the intensity components in the illumination. Among RGB light sources, the green component has the greatest influence and the blue component has the least influence.

The mathematical formula for converting YCrCb color space from RGB color space is as follows:

Y = 0.229 *R + 0.587* G + 0.114 * B

Cr = (RY) * 0.713 + delta

Cb = (BY) * 0.564 + delta

The value of delta is:

The mathematical formula for converting from YCrCb color space to RGB is as follows:

R = Y + 1.403 * (Cr-delta)

G = Y-0.714 * (Cr-delta) -0.344 * (Cb-delta)

B = Y + 1.773 * (Cb-delta)

# HSV color space

RGB is a color model proposed from the perspective of hardware, and there may be some differences in the process of matching with the human eye. The HSV color space is a color model oriented to visual perception. The HSV color space is based on psychology and vision. It points out that human eye color perception mainly contains three elements: hue, saturation, and brightness.

Speaking of which, I believe anyone who has used PS should know what HSV can do, right? However, we still introduce some of these 3 elements. After all, this blog post is dedicated to the theoretical knowledge of color space, and there should be no vacancies.

Hue (H): refers to the color of light, and the hue is related to the main light wavelength in the mixed spectrum. For example, "red orange, yellow, green, blue, blue and purple" respectively indicate different hues. If you consider from the perspective of wavelength, different wavelengths of light show different colors, in fact, they reflect the difference in color tone.

Saturation (S): refers to the depth of color, relative to purity, or the amount of white light mixed by a color. Pure spectral colors are fully saturated. Colors like deep red (red and white) and lavender (purple and white) are under-saturated, and saturation is inversely proportional to the amount of white light added.

Brightness (V): It reflects the degree of brightness of the light felt by the human eye, and this index is related to the reflectance of the object. In terms of color, if the more white is mixed in, the higher the brightness; if the more black is mixed in, the lower the brightness.

In the specific implementation, we distribute the colors of the physical space on the circle, and different angles represent different colors. Therefore, by adjusting the hue value, we can select different colors, and the value range of hue is [0, 360]. The color value is different, and the color is also different, as shown in the following table:

Hue value (degrees) | colour |
---|---|

0 | red |

60 | yellow |

120 | green |

180 | Blue |

240 | blue |

300 | Magenta |

The value of saturation is [0,1]. When the value of saturation is 0, only grayscale is available. The greater the saturation, the richer the color value. As for the brightness, its value range is also [0,1].

For example, if the blogger now takes hue=0, saturation=1, and brightness=1, he can extract the color deep red.

After introducing the theoretical knowledge, HSV, like the color space above, also needs to be converted with RGB. However, before we convert here, we need to convert the value of the RGB color space to [0,1] before processing. The specific treatment is as follows:

V=max(R,G,B) brightness

Here, the calculation result of H may be less than 0. If this happens, further processing and calculation of H are required. As follows:

The calculation result of the above formula must be consistent with the range of hue, brightness and saturation mentioned above. As for the conversion of HSV to RGB, those interested can refer to the development document.

# HLS color space

HLS is similar to HSV color space, and both have three elements. Only the HLS color space is different from L and V, where the L (luminance/brightness) of the HLS color space replaces the brightness.

So what is brightness/luminance?

In fact, the brightness/brightness is used to control the light-dark transformation of the color, and its value range is also [0,1]. In our program, we can use the brightness/brightness to measure how much light is reflected from the surface of the object. Luminance/brightness is important for the eyes to perceive color, because when a colored object is in a place where the light is too strong or too dark, the eye cannot accurately obtain the color of the object.

To be honest, editing formulas is a bit laborious. If you are interested, you can query the development documents yourself. Follow-up development in python, we all use cv2.cvtColor() to convert. To use it, you only need to understand what it does, and you don’t need to know how it is implemented internally, but the internal implementation is just these mathematical formulas above.

# CIEL *a* b* color space

The CIEL *a* b* color space is a uniform color space model, which is a color model oriented to visual perception. From the perspective of uniform visual perception, the degree of difference between the two colors that people perceive should be proportional to the distance between the two colors in the color space. In a certain color space, if the degree of difference between two colors observed by a person is proportional to the Euclidean distance between the corresponding points of the two colors in the color space, the color space is called a uniform color space.

*The L component in the* CIEL *a* b *color space is used to represent the brightness of the pixel, and the value range is [0,100], which means from pure black to pure white; the a* component represents the range from red to green, and the value range is [-127,127] ; The b* component represents the range from yellow to blue, and the value range is [-127.127].

Since CIEL *a* b *is developed on CIE's XYZ color space, when converting, you need to first convert RGB to XYZ color space, and then convert to CIEL* a *b* . Query development documents of interest for specific mathematical formulas.

# CIEL *u* v* color space

The CIEL *u* v *color space is the same as the CIEL* a *b* color space, which is a uniform color model. The CIEL *u* v* color space has nothing to do with the device. It is suitable for display and combination based on the principle of additive color. This model emphasizes the expression of red, that is, it is more sensitive to changes in red, but not to changes in blue. sensitive.

Similarly, the CIEL *u* v *color space also needs to first convert RGB to XYZ color space, and then convert it to CIEL* u *v* . If you are interested in specific formulas, you can consult the development documentation.

# Bayer color space

Bayer color space is widely used in CCD and CMOS cameras.

The theoretical knowledge of color space is basically explained here, and those who are interested can expand the last few mathematical formulas by themselves.