OpenCV 02 (color space)

1. OpenCV color space

1.1 RGB and BGR

The most common color space is RGB, and the human eye also distinguishes colors based on the RGB color space.

OpenCV uses BGR by default .

The difference between BGR and RGB color spaces lies in the order in which pictures are arranged on the color channels.

When displaying pictures, you need to pay attention to the color space of the adapted picture and the color space of the display environment. For example, if the incoming picture is in the BGR color space and the display environment is in the RBG space, color confusion will occur.

1.2 HSV, HSL and YUV

HSV

The color space used most by OpenCV is HSV.

- Hue: Hue, that is, color, such as red, blue. Measured by angle, the value range is 0°~360°, starting from red and counting counterclockwise, red is 0°, green is 120°, blue is 240°


- Saturation: Saturation, indicating how close a color is to a spectral color. A color can be thought of as the result of mixing a certain spectral color with white. The greater the proportion of spectral colors, the closer the color is to spectral colors, and the higher the saturation of the color. The saturation is high and the color is deep and vivid. The white light component of the spectral color is 0, and the saturation reaches the highest level. Usually the value range is 0% ~ 100%. The larger the value, the more saturated the color.


- Value: Lightness. Lightness indicates the brightness of the color. For the light source color, the lightness value is related to the brightness of the illuminant; for the object color, this value is related to the transmittance or reflectance of the object. Usually the value range is 0% (black) to 100% (white).

Why use HSV?

It is convenient for OpenCV to do image processing. For example, the background color can be determined based on the hue value.

HSL

HSL is similar to HSV.

- Hue: Hue

- Saturation: Saturation

- Lightness: brightness

HSL is pure white on top, no matter what color it is.

The difference between HSV and HSL:

YUV

**YUV**, is a color encoding method. Often used in various video processing components. YUV takes into account human perception when encoding photos or videos, allowing for reduced chroma bandwidth.

"Y" stands for brightness (Luminance or Luma), that is, the gray scale value, "U" and "V" stand for chroma (Chrominance or Chroma), which are used to describe the color and saturation of the image, and are used to specify pixels s color.

The invention of Y'UV was due to the transition period between color television and black and white television.

The biggest advantage of Y'UV is that it only takes up very little bandwidth.

- 4:4:4 means full sampling.

- 4:2:2 means 2:1 horizontal sampling and full vertical sampling.

- 4:2:0 means 2:1 horizontal sampling and 2:1 vertical sampling.

- 4:1:1 means 4:1 horizontal sampling and full vertical sampling.

1.3 Color space conversion

cvtColor(img, colorspace): key API for color conversion

import cv2

def callback(value):
    pass

cv2.namedWindow('color', cv2.WINDOW_NORMAL)
cv2.resizeWindow('mouse', 640, 480)

img = cv2.imread('./cat.jpeg')

# 常见的颜色空间转换
colorspaces = [cv2.COLOR_BGR2RGBA, cv2.COLOR_BGR2BGRA, 
               cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV, 
               cv2.COLOR_BGR2YUV]
cv2.createTrackbar('curcolor', 'color', 0, 4, callback)

while True:
    index = cv2.getTrackbarPos('curcolor', 'color')

    #颜色空间转换API
    cvt_img = cv2.cvtColor(img, colorspaces[index])

    cv2.imshow('color', cvt_img)
    key = cv2.waitKey(10)
    if key & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/peng_258/article/details/132765281