opencv learning four: color space conversion

1. Common color space

Insert picture description hereRGB color space
RGB (red, green, blue) color space is most commonly used in display systems. Color cathode ray tubes and color raster graphics displays use R, G, and B values ​​to drive R, G, and B electron guns to emit electrons. It also excites the R, G, and B phosphors on the phosphor screen to emit light of different brightness, and add and mix to produce various colors; the scanner also absorbs the light sent from the original by reflection or transmission. R, G, B components, and use it to represent the color of the original. The RGB color space is called the device-related color space, because different scanners scan the same image and will get different color image data; different models of monitors display the same image, but also have different color display results. The RGB space used by monitors and scanners is different from the CIE 1931 RGB true three-primary color system space, which is a device-independent color space. btw: The Color Picker of Photoshop. The color value of each color in HSB, RGB, LAB, and CMYK color spaces can be displayed.
HSV color space
The HSV (hue, saturation, value) color space model corresponds to a cone-shaped subset in the cylindrical coordinate system, and the top surface of the cone corresponds to V=1. It contains R=1, G=1 in the RGB model, B=1 Three faces, the colors represented are brighter. The color H is given by the rotation angle around the V axis. Red corresponds to an angle of 0°, green corresponds to an angle of 120°, and blue corresponds to an angle of 240°. In the HSV color model, each color differs from its complementary color by 180°. The saturation S takes a value from 0 to 1, so the radius of the top surface of the cone is 1. The color gamut represented by the HSV color model is a subset of the CIE chromaticity diagram. In this model, a color with a saturation of 100% has a purity of less than 100%. At the apex (or origin) of the cone, V=0, H and S are undefined and represent black. At the center of the top surface of the cone, S=0, V=1, H is undefined, representing white. From this point to the origin, it represents gray with dimming brightness, that is, gray with different gray levels. For these points, S=0 and the value of H is undefined. It can be said that the V axis in the HSV model corresponds to the main diagonal in the RGB color space. The color on the circumference of the top surface of the cone, V=1, S=1, this color is a pure color. The HSV model corresponds to the painter's method of color matching. The painter uses the method of changing the color density and color depth to obtain different tones of colors from a certain pure color, adding white to a pure color to change the color density, adding black to change the color depth, and adding different proportions of white to obtain black. Various shades.
HSI color space
The HSI color space starts from the human visual system, using hue (Hue), color saturation (Saturation or Chroma) and brightness (Intensity or Brightness) to describe colors. The HSI color space can be described by a conical space model. Using this cone model to describe the HIS color space is quite complicated, but it can indeed express the changes in hue, brightness and color saturation very clearly. Hue and saturation are usually called chroma, which is used to indicate the type and depth of color. Since human vision is more sensitive to brightness than to color density, in order to facilitate color processing and recognition, the human visual system often uses the HSI color space, which is more in line with human visual characteristics than the RGB color space. A large number of algorithms in image processing and computer vision can be conveniently used in the HSI color space, they can be processed separately and are independent of each other. Therefore, the HSI color space can greatly simplify the workload of image analysis and processing. HSI color space and RGB color space are just different representations of the same physical quantity, so there is a conversion relationship between them.

The HSI color model starts from the human visual system, using H for Hue, S for Saturation and I for Intensity to describe colors. Saturation is just inversely proportional to the amount of white light of a color, and it can be said to be an indicator of whether the color is vivid or not. Therefore, if we use the HIS model to process images on the monitor, we will be able to get a more realistic effect.
Hue: Refers to the wavelength of the transmission or reflection of an object. It is more common to identify it by colors such as red, orange or green, with a value ranging from 0 to 360 degrees.
Saturation: Also known as chroma, it refers to the intensity or purity of a color. Saturation represents the ratio of gray to hue and is measured from 0% (gray) to 100% (fully saturated).
Intensity: refers to the relative brightness of a color, usually measured as a percentage from 0% (black) to 100% (white).
YCrCb color space
YCrCb, or YUV, is mainly used to optimize the transmission of color video signals to make it backward compatible with old-fashioned black and white TVs. Compared with RGB video signal transmission, its biggest advantage is that it only takes up very little bandwidth (RGB requires three independent video signals to be transmitted simultaneously). Among them, "Y" represents the brightness (Luminance or Luma), which is the grayscale value; while "U" and "V" represent the chroma (Chrominance or Chroma), which is used to describe the color and saturation of the image. Specify the color of the pixel. "Brightness" is established through the RGB input signal by adding specific parts of the RGB signal together. "Chroma" defines two aspects of color-hue and saturation, which are represented by Cr and CB respectively. Among them, Cr reflects the difference between the red part of the RGB input signal and the brightness value of the RGB signal. And CB reflects the difference between the blue part of the RGB input signal and the brightness value of the RGB signal. The importance of using the YUV color space is that its luminance signal Y and chrominance signals U and V are separated. If there is only Y signal component but no U and V components, then the image represented in this way is a black and white grayscale image. Color TV adopts YUV space to solve the compatibility problem of color TV and black and white TV with brightness signal Y, so that black and white TV can also receive color TV signals. The formula for converting between YUV and RGB is as follows (RGB values ​​range from 0 to 255): Y = 0.299R + 0.587G + 0.114BU = -0.147R-0.289G + 0.436BV = 0.615R-0.515G-0.100BR = Y + 1.14VG = Y-0.39U-0.58VB = Y + 2. 03U In DirectShow, common RGB formats are RGB1, RGB4, RGB8, RGB565, RGB555, RGB24, RGB32, ARGB32, etc.; common YUV formats are YUY2, YUYV, YVYU, UYVY, AYUV, Y41P, Y411, Y211, IF09, IYUV, YV12, YVU9, YUV411, YUV420, etc. YCrCb space is also often used in face detection, because general images are based on RGB space. In RGB space, the skin color of a face is greatly affected by brightness, so it is difficult to separate skin color points from non-skin color points. That is to say, after processing in this space, the skin color points are discrete points, and there are many non-skin colors embedded in the middle, which brings difficulties to the calibration of the skin color area (face calibration, eyes, etc.). If you convert RGB to YCrCb space, you can ignore the influence of Y (brightness), because the space is affected little by brightness, and skin tones will produce good convergence. In this way, the three-dimensional space will be two-dimensional CrCb, and the skin color points will form a certain shape. For example, if you have a face, you will see an area of ​​a face. If you have an arm, you will see the shape of an arm. This is very useful for processing pattern recognition. It is good. According to experience, the CrCb value of a certain point satisfies: 133≤Cr≤173, 77≤Cb≤127, then this point is regarded as a skin color point, and the others are non-skin color points.
The YCrCb color space has a good effect on skin color detection. It is better than the HSV color space.
Looking up some online data, we know that the Cr component of the normal yellow race is about 140 ~ 175, and the Cb component is about 100 ~ 120. between.
Reference blog: YCrCb color space and skin tone detection

The difference between YCbCr and YUV The
yuv color model is derived from the rgb model, which is characterized by separating brightness and chroma, which is suitable for image processing.

The YCbCr model is derived from the yuv model, applied to digital video, ITU-R BT.601 recommendation

Through the above comparison, we can confirm that the yuv we use in encoding standards such as h.264 and mpeg is actually YcbCr. Don't be confused by the name.
In order to use the viewing angle characteristics of people to reduce the amount of data, the color image represented by the RGB space is usually converted to other color spaces. There are three color space transformations currently used: YIQ, YUV and YCrCb. Each color space produces a luminance component signal and two chrominance component signals, and the parameters used in each transformation are adapted to a certain type of display device. Among them, YIQ is suitable for NTSC color TV system, YUV is suitable for PAL and SECAM color TV system, and YCrCb is suitable for computer monitors.

YUV is not a combination of those English words, but a symbol. Y represents brightness, UV is used to represent color difference, and U and V are the two components of color.

Advantages of YUV:
1. The importance of YUV notation is that its luminance signal (Y) and chrominance signal (U, V) are independent of each other.
2. Another advantage of YUV notation is that it can use the characteristics of the human eye to reduce the storage capacity required for digital color images.

YCbCr where Y refers to the luminance component, Cb refers to the blue chrominance component, and Cr refers to the red chrominance component.
YCbCr is part of the ITU-R BT1601 recommendation during the development of the video standard of the World Digital Organization. It is actually a scaled and offset replica of YUV. Among them, Y has the same meaning as Y in YUV. Cb and Cr also refer to color, but they are different in the representation method. In the YUV family, YCbCr is the most widely used member in computer systems, and it has a wide range of applications. Both JPEG and MPEG use this format. Generally speaking, YUV mostly refers to YCbCr. YCbCr has many sampling formats, such as 4:4:4, 4:2:2, 4:1:1 and 4:2:0.

Second, call the conversion function to achieve image color space conversion

code show as below:

import cv2 as cv  #导入cv模块

#色彩空间的转换
def color_space_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)#RGB转换为gray
    cv.imshow("gray", gray)
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)#RGB转换为hsv
    cv.imshow("hsv", hsv)
    yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)#RGB转换为yuv
    cv.imshow("yuv", yuv)
    ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)#RGB转换为ycrcb
    cv.imshow("ycrcb", ycrcb)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/lena.jpg")  #读取图片位置
#blue green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("input image", src) #对窗口图片进行展示
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()  #释放所有的内存

Run screenshot:
Insert picture description hereHSV color space description:
H: 0-180 S: 0-255 V: 0-255
You can filter other colors
HSV color corresponding to the RGB component range of the corresponding color in the following table
Insert picture description here

3. Color space conversion, use the inRange function to filter the colors in the video to realize tracking a certain color

code show as below:

import cv2 as cv  #导入cv模块
import numpy as np #np科学计数的包,通过numpy对数据进行处理

def extrace_object_demo():
    capture = cv.VideoCapture("C:/Users/lenovo/Desktop/opencv/daima/banknum/test.mp4") #导入视频
    while(True):
        ret, frame = capture.read()
        if ret == False:
            break;
        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) #转换色彩空间为hsv
        #设置绿色的范围,跟踪视频中的绿色
        lower_hsv = np.array([37, 43, 46])#设置过滤的颜色的低值
        upper_hsv = np.array([77, 255, 255])#设置过滤的颜色的高值
        mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)#调节图像颜色信息(H)、饱和度(S)、亮度(V)区间,选择绿色区域
        cv.imshow("video", frame)
        cv.imshow("mask", mask)
        c = cv.waitKey(40)
        if c == 27:
            break

extrace_object_demo()
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot: The
Insert picture description hereinRange function is very simple. There are three
parameters. The first parameter: hsv refers to the original image. The
second parameter: lower_red refers to the value lower than this lower_red in the image. The image value becomes 0, which is the black
first. Three parameters: upper_red refers to the value higher than this upper_red in the image, the image value becomes 0
and the value between lower_red~upper_red becomes 255, which is white

Four, channel separation, merger, modify a channel

Involved function:
split() splits the color image into 3 channels
merge() channel merge The
code is as follows:

import cv2 as cv
import numpy as np
 
 
src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/lena.jpg")  #读取图片位置
#blue green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)  #创建一个GUI
cv.imshow("input image", src) #对窗口图片进行展示
 
#通道分离,输出三个单通道图片
b, g, r = cv.split(src)#将彩色图像分割成3个通道
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)
 
#通道合并
src = cv.merge([b, g, r]) #将三个通道进行合并
cv.imshow("changed image", src)
 
#修改某个通道的值
src[:, :, 2] = 0 #将红色通道改为0
cv.imshow("single", src)
 
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112424288