Chapter 4: Color Space Type Conversion

Color space is called color space, color space, color model, color system, color model, color model, etc.

Common color space types are:

  • RGB color space (most common)
  • 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

Each color space has a field of dealing with problems, so when we deal with specific problems, we need to use the color space type conversion.

one. Color space basics:

1. Gray color space:

GRAY (grayscale image) usually refers to an 8-bit grayscale image, which has 256 gray levels, and the range of pixel values ​​​​is [0, 255]. When the image is converted from the RGB color space to the GRAY color space, the processing is as follows:

  • GRAY = 0.299 * R + 0.587 * G + 0.114 * B

The conversion formula above is a standard conversion formula and the conversion method used in OpenCV. Sometimes it can also be simplified to:

  • $GRAY = \cfrac { R + G +B }{ 3 } $

When the image is converted from the GRAY color space to the RGB color space, all channel values ​​will be the same in the end, and the processing method is as follows:

  • R = GRAY
  • G = GRAY
  • B = GRAY

2. XYZ color space

The XYZ color space is defined by CIE. It is a color space that is easier to calculate and can be converted to and from the RGB color space.

  • Convert RGB color space to XYZ color space:

image-20211006132125441

  • Convert XYZ color space to RGB color space

image-20211006132148911

3. YCrCb color space

​ The human visual system (HSV) is less sensitive to color than to brightness. In the traditional RGB color space, the RGB three primary colors have the same importance, but the brightness information is ignored.

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

Luminance gives information about how light or dark a color is, calculated as a weighted sum of lighting intensities. In RGB light sources, the green component has the greatest influence and the blue component has the least influence.

  • Convert RGB color space to YCrCb color space:

    • Y = 0.299 * R + 0.578 * G + 0.114 * B
    • Cr = ( R - Y ) × 0.713 + delta
    • Cb = (B - Y ) × 0.546 + delta

    The value of delta in the formula is: { 128, 8-bit image 32768, 16-bit image 0.5, single-precision image \begin{cases} 128, \quad 8-bit image\\ 32768, \quad 16-bit image\\ 0.5, \quad single-precision images\end{cases}1288 bit image32768,1 6 -bit image0.5,Single Precision Image

  • Convert YCrCb color space to RGB color space:

    • R = Y + 1.403 * (Cr - delta)
    • G = Y - 0.714 * (Cr - delta) - 0.344 * (Cb - delta)
    • B = Y + 1.733 * (Cr - delta)

    The value of delta in the formula is the same as the value of delta in the above formula.

3. HSV color space

RGB is a color model proposed from the perspective of hardware, and there may be certain differences in the process of matching with human eyes. But the HSV color space is different, it is a color model for visual perception. From the perspective of psychology and vision, the HSV color space points out that the color perception of the human eye mainly includes three elements: Hue, Saturation, and Value. Hue refers to the color of light, and saturation refers to the depth of color. The brightness and brightness of the light perceived by the human eye.

  • Hue: Hue is related to the main wavelength of light in the mixed spectrum, for example, "red orange yellow green blue blue purple" respectively represent different hues. If considered from the perspective of wavelength, different wavelengths of light appear as different colors, in fact, they reflect the difference in hue.
  • Saturation: Refers to the relative purity, or how much a color mixes with white light. Pure basic colors are fully saturated, and colors like magenta (red plus white) and lavender (purple plus white) are undersaturated, with saturation inversely proportional to the amount of white light added.
  • Brightness: It reflects the brightness of the light perceived by the human eye, which is related to the reflectivity of the object. For color, if more white is added, the brightness will be higher; if more black is added, the brightness will be lower.

In terms of implementation, we distribute the colors of the physical space on the circumference, and different angles represent different colors. Therefore, different colors can be selected by adjusting the hue value, and the value range of the hue is [0, 360]. When the hue takes different values, the represented colors are different, and the angle between the two angles corresponds to the transition color between the two colors.

image-20211009104245556

​ Saturation is a proportional value, the range is [0, 1], specifically the ratio between the purity value of the selected color and the maximum purity value of the color. When the saturation is 0, there is only grayscale. Brightness indicates the brightness of the color, and the value range is also [0, 1].

​ In the HSV color model, color picking becomes more intuitive. For example, with the value "hue=0, saturation=1, lightness=1", the current color is dark red, and the color is brighter. If the value is "hue=120, saturation=0.3, lightness=0.4", the current color is light green and darker.

Before converting from RGB color space to HSV color space, you need to convert the value of RGB color space to [0, 1] before processing. The specific processing method is:

image-20211009105224843

The calculation result may have H<0. If this happens, further calculation of H is required, as follows

H = { H + 360 , H < 360 H , (other cases) H = \begin{cases} H + 360, \quad H<360 \\ H, \quad (other cases) \end{cases}H={ H+360,H<360H,( other cases )

It can be known from the above formula:

  • S ∈ [ 0 , 1 ] S \in [0, 1] S[0,1]
  • V ∈ [ 0 , 1 ] V \in [0, 1] V[0,1]
  • H ∈ [ 0 , 360 ] H \in [0, 360] H[0,360]

4. HLS color space

The HLS color space contains three elements: Hue H (Hue), Brightness/Lightness L (Lightness), Saturation S (Saturation) is similar to the HSV color space, except that the HLS color space is replaced by "Brightness/Lightness L (lightness)" "Brightness (Value)".

  • Hue: Indicates the color that the human eye can perceive. In the HLS model, all colors are distributed on a flat hue circle. The entire hue circle is a 360-degree central angle, and different angles represent different colors.
  • Brightness/Brightness: It is used to control the light and dark changes of the color, and its value range is also [0,1]. We measure how much light is reflected from the surface of the object by the size of the brightness/brightness. Brightness/lightness is very important for the eyes to perceive color, because when a colored object is in a place with too much light or too dark light, the eyes cannot accurately perceive the color of the object.
  • Saturation: Use a value of [0,1] to describe the color purity change under the same hue and the same lightness/brightness. The larger the value of saturation, the higher the purity of the color, and the brighter the color; on the contrary, the smaller the value of saturation, the lower the purity of the color, and the darker the color. This attribute is usually used to indicate the depth of the color, such as dark green, light green

5. CIEL*a*b* color space

​ CIEL * a * b color space is a uniform color space model, which is a color model for visual perception. From the perspective of uniform visual perception, the degree of difference between the two colors perceived by people should be proportional to the distance between the two colors in the color space. In a color space, if the degree of difference between two colors observed by people 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]. ​ Before converting from RGB color space to CIEL * a * b * color space, you need to convert the value of RGB color space to [0,1] before processing. ​ Since the CIEL * a * b * color space is developed on the basis of CIE's XYZ color space, in specific processing, it is necessary to convert RGB to XYZ color space first, and then convert it to CIEL a * b * color

space space. The specific implementation method is:

image-20211009111854298

In the formula:

f ( t ) = { t 1 3 , t > 0.0008856 7.787 t + 16 116 , other cases f(t) = \begin{cases} t^\cfrac { 1 }{ 3 }, \quad t > 0.0008856 \\ 7.787 t + \cfrac { 16 }{ 116 }, \quad other cases\end{cases}f(t)=t31,t>0.00088567 . 7 8 7 t+11616,other situations

delta = { 128 , 8-bit image 0 , single-precision image delta = \begin{cases} 128, \quad 8-bit image \\ 0, \quad single-precision image \end{cases}delta={ 128,8 bit image0Single Precision Image

The range of values ​​for each value in the result is:

  • L ∈ [ 0 , 100 ] L \in [0, 100] L[0,100]
  • a ∈ [ − 127 , 127 ] a \in [-127, 127] a[127,127]
  • b ∈ [ − 127 , 127 ] b \in [-127, 127] b[127,127]

6. CIEL*u*v*color space

The CIEL * u * v * color space is a uniform color model like the CIEL * a * b * color space. CIEL * u * v * color space has nothing to do with equipment, and is suitable for display and combination based on the principle of color addition. This model emphasizes the representation of red, that is, it is sensitive to changes in red, but sensitive to changes in blue. less sensitive.
The formula below gives the conversion formula from RGB color space to CIEL*u*v* color space.

Conversion from RGB color space to XYZ color space:

image-20211009114509255

Conversion from XYZ color space to CIEL*u*v* color space:

image-20211009114622048

The range of values ​​for each value in the result is:

  • L ∈ [ 0 , 100 ] L \in [0, 100]L[0,100]
  • u ∈ [ − 127 , 127 ] u \in [-127, 127] u[127,127]
  • v ∈ [ − 127 , 127 ] v \in [-127, 127] v[127,127]

7. Bayer color space

Bayer color space (Bayer model) is widely used in CCD and CMOS cameras. It is capable of acquiring color images from a single plane R, G, B interleaved table as shown.

image-20211009114825841

​ The pixel value of the output RGB image is obtained from the pixel values ​​of the same color of 1, 2 or 4 neighboring pixels of the current point. The above modes can be modified by moving a pixel to the left or a pixel above. In the color space conversion parameters of the function cv2.cvtColor(), two specific parameters x and y are usually used to indicate a specific mode. The pattern composition is specified by the values ​​in columns 2 and 3 in the second row of the graph. The picture is a typical "BG" mode.

​ There are many common modes, such as cv2.COLOR_BayerBG2BGR, cv2.COLOR_BayerGB2BGR, cv2.COLOR_BayerRG2BGR, cv2.COLOR_BayerGR2BGR, cv2.COLOR_BayerBG2RGB, cv2.COLOR_BayerGB2RGB, cv2.COLOR_Bay erRG2RGB, cv2.COLOR_BayerGR2RGB, etc.

two. type conversion function:

In OpenCV, we use the cv2.cvtColor() function for color space conversion. This function can realize the conversion of multiple color spaces.

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

    dst: Indicates the output image, which has the same data type and depth as the original input image.

    src: Indicates the original input image. It can be an 8-bit unsigned image, a 16-bit unsigned image, or a single-precision floating point number, etc.

    code: It is the color space conversion code.

    dstCn: is the channel number of the target image. If the parameter is the default 0, the number of channels is automatically obtained from the original input image and code.

NOTE: Image depth refers to the number of bits used to store each pixel and is also a measure of the color resolution of an image. Such as 8-bit unsigned image refers to the image depth. https://blog.csdn.net/qq_40041064/article/details/102971585

image-20211009143102590

image-20211009142358176

image-20211009142432255

image-20211009142508665

image-20211009142554657

image-20211009142834892

Note: The BGR color space is different from the traditional RGB color space. For a standard 24-bit bitmap, the first byte of the BGR color space stores information composed of blue, the second byte stores information composed of green, and the third byte stores information composed of red Information.

The conversion of the color space uses the following conventions:

  • The range of 8-bit image values ​​is [0,255].
  • The range of 16-bit image values ​​is [0,65 535].
  • The range of floating-point image values ​​is [0.0~1.0].

For linear transformations, these ranges are irrelevant. But for nonlinear conversion, the input RGB image must be normalized to its corresponding value range in order to obtain the correct conversion result.

​ For example, for an 8-bit image, the gray levels it can represent are 2 8 2^828 = 256, that is to say, in an 8-bit map, up to 256 states can be represented, usually a value between [0,255]. However, in many color spaces, the range of values ​​is not exactly in the range of [0,255]. At this time, the value needs to be mapped to [0,255].

​ For example, in the HSV or HLS color space, the hue value is usually in the range of [0,360), after the 8-bit image is converted to the above color space, the hue value is divided by 2, so that its value range becomes [0,180), To meet the storage range, that is, let the value distribution be within the range [0,255] that can be represented by an 8-bit map. For another example, in the CIEL * a * b * color space, the value range of the a channel and the b channel is [−127,127], in order to adapt it to the range of [0,255], each value must be added 127. However, it should be noted that due to the rounding in the calculation process, the conversion process is not accurate and reversible.

three. Type conversion instance

1. Convert the image between BGR mode and grayscale image.

import cv2

lena = cv2.imread('../lena512color.tiff')
gray = cv2.cvtColor(lena, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
print(lena.shape)
print(gray.shape)
print(rgb.shape)
cv2.imshow('lena', lena)
cv2.imshow('gray', gray)
cv2.imshow('rgb', rgb)
cv2.waitKey()
cv2.destroyAllWindows()

image-20211009145825898

Note: In the RGB image obtained by "rgb=cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)", the values ​​of the B channel, G channel, and R channel are all the same, so it still looks like a grayscale image.

2. Convert the image from BGR mode to RGB mode.

import cv2

bgr = cv2.imread('../lena512color.tiff')
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', bgr)
cv2.imshow('rgb', rgb)
cv2.waitKey()
cv2.destroyAllWindows()

Four. HSV color space discussion:

​ The RGB color space is a widely accepted color space, but the color space is too abstract, and we cannot directly perceive specific colors through its values. We are more accustomed to using an intuitive way to perceive color, and the HSV color space provides such a way. Through the HSV color space, we can more easily perceive colors through hue, saturation and brightness.

From the perspective of psychology and vision, the HSV color space proposes that the color perception of the human eye mainly includes three elements:

  • H: Hue (Hue, also known as hue).
  • S: Saturation.
  • V: Brightness (Value).

**1. Hue: **

​ In the HSV color space, the value range of hue H is [0,360]. The gray levels that can be represented by each pixel in an 8-bit image are 2 8 2^828 = 256, so when representing an HSV image in an 8-bit image, the angle value of the hue must be mapped to the range of [0, 255]. In OpenCV, you can directly divide the angle value of the hue by 2 to get a value between [0,180] to accommodate the storage and representation range of 8-bit binary (256 gray levels). Specifically as shown in the table:

image-20211009152209129

In OpenCV, after dividing the hue value by 2, you will get the hue value and the corresponding color as shown in the table. The following is the mapped hue value.

image-20211009152314816

After determining the value range, you can directly look up the corresponding value in the H channel of the image to find a specific color. For example, in an HSV image, a pixel with a value of 120 in the H channel corresponds to blue. Find the pixel with a value of 120 in the H channel, and you will find the blue pixel.

​ On the basis of the above, by analyzing the HSV values ​​​​corresponding to various objects, different objects can be found. For example, by analyzing the HSV value of the skin color, it is possible to directly find the face (such as skin) area in the image according to the HSV value of the skin color.

2. Saturation:

The range of saturation value is [0,1], so for saturation, the following questions need to be explained:

  • The R, G, and B components contained in the grayscale color are equal, which is equivalent to an extremely unsaturated color. So, the saturation value of a grayscale color is 0.
  • When displayed as a grayscale image, lighter areas correspond to colors with higher saturation.
  • If the color is low in saturation, then it will not be able to reliably calculate the hue.
  • Also map the value of the saturation S from the [0,1] range to the [0,255] range

3. Brightness:

The range of brightness is the same as the range of saturation, both are [0,1]. Likewise, luminance values ​​also map values ​​into the [0,255] range within OpenCV.

​ The larger the brightness value, the brighter the image; the lower the brightness value, the darker the image. When the brightness value is 0, the image is pure black.

1. Get the specified color:

​ There are many ways to obtain the corresponding value of the color value of the RGB color space in the HSV color space. For example, the HSV value corresponding to the RGB value can be obtained through image editing software or an online website.

It should be noted that when converting from RGB/BGR color space to HSV color space, OpenCV maps the values ​​​​of HSV space in order to meet the requirements of 8-bit images. Therefore, the HSV value obtained through software or website needs to be further mapped to be consistent with the HSV value in OpenCV.

Example: In OpenCV, test the values ​​of different colors in the RGB color space after they are converted to the corresponding values ​​in the HSV color space.

import cv2
import numpy as np

img_blue = np.zeros([1, 1, 3], dtype=np.uint8)
img_blue[0, 0, 0] = 255
blue = img_blue
blue_hsv = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
print('blue=\n', blue)
print('blue_hsv=\n', blue_hsv)

img_green = np.zeros([1, 1, 3], dtype=np.uint8)
img_green[0, 0, 1] = 255
green = img_green
green_hsv = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print('green=\n', green)
print('green_hsv=\n', green_hsv)

img_red = np.zeros([1, 1, 3], dtype=np.uint8)
img_red[0, 0, 2] = 255
red = img_red
red_hsv = cv2.cvtColor(red, cv2.COLOR_BGR2HSV)
print('red=\n', red)
print('red_hsv=\n', red_hsv)

# 输出结果
blue=
 [[[255   0   0]]]
blue_hsv=
 [[[120 255 255]]]
green=
 [[[  0 255   0]]]
green_hsv=
 [[[ 60 255 255]]]
red=
 [[[  0   0 255]]]
red_hsv=
 [[[  0 255 255]]]

2. Mark the specified color:

​ In the HSV color space, the H channel (saturation Hue channel) corresponds to different colors. Or from another perspective, the difference in color is mainly reflected in the difference in the value of the H channel. Therefore, by filtering the H channel value, a specific color can be filtered out. For example, in an HSV image, if only the pixels with a value of 240 in the H channel (adjusted to 120 in OpenCV) are displayed through the control, only the blue part will be displayed in the image.

  • Lock a specific value through the inRange() function

    In OpenCV, the function cv2.inRange() is used to judge whether the pixel value of the pixel in the image is within the specified range. The syntax is:

    • dst=cv2.inRange(src,lowerb,upperb)

      dst: Indicates the output result, the size is consistent with src.

      src: Indicates the array or image to check.

      lowerb: Indicates the lower bound of the range.

      upperb: Indicates the upper bound of the range.

    • The return value dst has the same size as src, and its value depends on whether the value at the corresponding position in src is in the interval [lowerb, upperb]

      • If the src value is within the specified interval, the value at the corresponding position in dst is 255.
      • If the src value is not within the specified range, the value at the corresponding position in dst is 0.

    Example: Use the function cv2.inRange() to mark out the values ​​within [100,200] in an image.

    import cv2
    import numpy as np
    
    img = np.random.randint(0, 256, size=[5, 5], dtype=np.uint8)
    min = 100
    max = 200
    mask = cv2.inRange(img, min, max)
    print('img=\n', img)
    print('mask=\n', mask)
    
    # 输出结果
    img=
     [[170 181 245  13 255]
     [ 79  72 133 113 138]
     [112  72 169  87  47]
     [142 210 139  94 165]
     [ 32  16   9  89  95]]
    mask=
     [[255 255   0   0   0]
     [  0   0 255 255 255]
     [255   0 255   0   0]
     [255   0 255   0 255]
     [  0   0   0   0   0]]
    
  • Display ROIs via mask-based bitwise AND operations:

    Example: Display a region of interest (ROI) within an image normally and display the rest in black.

    import cv2
    import numpy as np
    
    img = np.ones([5, 5], dtype=np.uint8) * 9
    mask = np.zeros([5, 5], dtype=np.uint8)
    mask[0:3, 0] = 1
    mask[2:5, 2:4] = 1
    
    roi = cv2.bitwise_and(img, img, mask=mask)
    print('img=\n', img)
    print('mask=\n', mask)
    print('roi=\n', roi)
    
    # 输出结果
    img=
     [[9 9 9 9 9]
     [9 9 9 9 9]
     [9 9 9 9 9]
     [9 9 9 9 9]
     [9 9 9 9 9]]
    mask=
     [[1 0 0 0 0]
     [1 0 0 0 0]
     [1 0 1 1 0]
     [0 0 1 1 0]
     [0 0 1 1 0]]
    roi=
     [[9 0 0 0 0]
     [9 0 0 0 0]
     [9 0 9 9 0]
     [0 0 9 9 0]
     [0 0 9 9 0]]
    
  • Display specific color values:

    Example: Use OpenCV to extract different colors in a picture.

    ​ It should be noted that when actually extracting a color, it is often not to extract a specific value, but to extract a color interval. For example, in HSV mode in OpenCV, blue has a value of 120 in the H channel. When extracting blue color, the value of an interval near "blue value 120" is usually taken as the extraction range. The radius of this interval is usually around 10, for example, values ​​in the range [120−10,120+10] are usually extracted to specify blue.

    ​ In contrast, the value range of the S channel and V channel in HSV mode is generally [100,255]. This is mainly because, when the saturation and lightness are too low, the calculated hue can be unreliable.

    import cv2
    import numpy as np
    
    
    img = cv2.imread('../pepper.tiff')
    cv2.imshow('img', img)
    print(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    cv2.imshow('img_hsv', img_hsv)
    
    min_red = np.array([0, 50, 50])
    max_red = np.array([20, 255, 255])
    mask = cv2.inRange(img_hsv, min_red, max_red)
    img_red = cv2.bitwise_and(img_hsv, img_hsv, mask=mask)
    cv2.imshow('img_red', img_red)
    
    min_green = np.array([40, 50, 50])
    max_green = np.array([70, 255, 255])
    mask = cv2.inRange(img_hsv, min_green, max_green)
    img_green = cv2.bitwise_and(img_hsv, img_hsv, mask=mask)
    cv2.imshow('img_green', img_green)
    
    min_yellow = np.array([20, 50, 50])
    max_yellow = np.array([40, 255, 255])
    mask = cv2.inRange(img_hsv, min_yellow, max_yellow)
    img_yellow = cv2.bitwise_and(img_hsv, img_hsv, mask=mask)
    cv2.imshow('img_yellow', img_yellow)
    
    cv2.waitKey()
    cv2.destroyAllWindows()
    

five.alpha channel:

​ On the basis of the three channels of the RGB color space, an A channel, also called the alpha channel, can be added to indicate transparency. This 4-channel color space is called the RGBA color space, and the PNG image is a typical 4-channel image. The assignment range of the alpha channel is [0,1], or [0,255], which means from transparent to opaque.

Example: Write a program to process the alpha channel of the image.

import cv2

img = cv2.imread('../lena512color.tiff')
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
b, g, r, a = cv2.split(bgra)
a[:, :] = 125
bgra_125 = cv2.merge([b, g, r, a])
a[:, :] = 0
bgra_0 = cv2.merge([b, g, r, a])

cv2.imshow('img', img)
cv2.imshow('bgra', bgra)
cv2.imshow('bgra_125', bgra_125)
cv2.imshow('bgra_0', bgra_0)

cv2.imwrite('../bgra_125.png', bgra_125)
cv2.imwrite('../bgra_0.png', bgra_0)

cv2.waitKey()
cv2.destroyAllWindows()

image-20211010135327358

The bgra_125 and bgra_0 pictures are shown above. Note: If you only use cv2.imshow to display images, although the alpha channel values ​​of each image are different, there is no difference when displaying. If you use cv2.imwrite() to save it to a local file, you can find that it is different.

Guess you like

Origin blog.csdn.net/weixin_57440207/article/details/122646984