## Color space conversion HSV,GRAY

RGB color space is a relatively common color space. In addition, more common color spaces include GRAY color space (grayscale image), YCrCb color space, HSV color space, HLS color space, CIEL*a*b* Color space, CIEL*u*v* color space, Bayer color space, etc. Different color spaces understand color and represent color from different perspectives. Simply put, different color spaces are different representations of images. Each color space has its own problems that are good at dealing with, and different color spaces should be selected according to the problems to be solved. ——40 cases of computer vision from entry to proficiency

## 1.GRAY color space

G R A Y = 0.299 ⋅ R + 0.587 ⋅ G + 0.114 ⋅ B GRAY = 0.299 \cdot R +0.587\cdot G +0.114 \cdot B
standard conversion method is also the conversion method used in OpenCV. Sometimes the conversion can also be done in a simplified form:

G R A Y = R + G + B 3 GRAY = \frac{R+G+B}{3}

When the image is converted from the GRAY color space to the RGB color space (or BGR color space), the final values ​​of all channels will be the same, and the processing method is as follows:

R = G R A Y G = G R A Y B = G R A Y R=GRAY \\ G=GRAY \\ B=GRAY

## 2. HSV color space

RGB is a color space proposed from the perspective of hardware, and it is a widely accepted color space. However, this color space is too abstract, and there may be some differences in the process of matching with human eyes, which prevents people from directly perceiving specific colors through its values. For example, in reality, it is impossible to use the percentage of each pigment (RGB color space) to describe the color of a piece of clothing, but it is more customary to use an intuitive way to perceive the color, and the HSV color space provides such a way. Through the HSV color space, it is more convenient to perceive color through hue, saturation and lightness. In fact, except for the HSV color space, most other color spaces are not convenient for people to directly understand and explain colors. The HSV color space is oriented to visual perception. It points out the color perception of the human eye from the perspective of psychology and vision, and mainly includes three elements: hue, saturation, and lightness.

• Hue H
Hue refers to the color of light. The hue is related to the main wavelength of light in the mixed spectrum, such as red, orange, yellow, green, cyan, blue, and purple represent different hues. From the perspective of wavelength, light of different wavelengths appears as different colors, which actually reflects the difference in hue. In the HSV color space, the value range of the hue H is [0, 360], a hue value of 0 represents red, and a hue value of 300 represents magenta. Each pixel in an 8-bit bitmap can represent 28=256 values, so when representing an HSV image in an 8-bit bitmap, the value of the hue in the range [0, 360] should be mapped to [0, 255] within range. OpenCV directly divides the hue value by 2 to obtain a value between [0, 180] to accommodate the storage and representation range of 8-bit binary numbers (256 gray levels).

• Saturation S

Saturation refers to the vividness of the color, indicating the relative purity of the color. Saturation depends on the proportion of gray in the color. The smaller the proportion of gray, the higher the saturation; the greater the proportion of gray, the lower the saturation. The most saturated color is the color without any gray (including white and black) mixed, that is, pure color. Gray is a very desaturated color with a saturation value of 0. If the color has very low saturation, then it can calculate the hue unreliably, because there is no color information and only gray is left. Saturation is equal to the ratio between the purity value of the selected color and the maximum purity value of the color, and the value range is [0, 1]. When the value of saturation is 0, there is only grayscale. After the color space conversion, in order to adapt to the 256 gray levels of the 8-bit bitmap, it is necessary to map the values ​​in the new color space to the range [0, 255]. That is, the value of saturation is mapped from [0, 1] to [0, 255].

• Brightness V
Brightness refers to the brightness of the color perceived by the human eye, and reflects the brightness and darkness of the light perceived by the human eye. This indicator is related to the reflectivity of the object, and the same hue will have different brightness differences. For achromatic colors (black, white, gray), white has the highest lightness, black has the lowest lightness, and there are grays of different lightness between black and white. For color images, the higher the brightness value, the brighter the image; the lower the brightness value, the darker the image. Brightness is a key factor in visual perception, and it exists independently of other properties. When the lightness value of the color image is lower than a certain level, it presents the effect of a black and white photo. The range of lightness is the same as the range of saturation, both are [0, 1]. Similarly, the lightness value is also mapped to [0, 255] in OpenCV.

## 3. Color space conversion

The following are all python, and OpenCV uses cv2.cvtColor()functions to achieve color space conversion. This function can realize the conversion between multiple color spaces, and the syntax format is

dst = cv2.cvtColor(src,code [,dstCn])


Among them:
● dst represents the output image, which has the same data type and depth as the original input image.
● src represents the original input image. It can be an 8-bit unsigned image, a 16-bit unsigned image, or a single-precision floating-point image, etc.
● code is the color space conversion code. Table 3-6 shows some common code values.
● dstCn is the channel number of the target image. If the parameter is the default value of 0, then the number of channels is automatically obtained from the original input image and code.

### Guess you like

Origin blog.csdn.net/m0_49302377/article/details/130926066
Recommended
Ranking
Daily