I am learning OpenCV color space conversion in Vscode

Article directory

  • color
  • 【1】Color space (color gamut)
    • (1) **RGB color space**
      • Conversion to xyz color space
        • Convert RGB color space to XYZ color space
        • Convert XYZ color space to RGB color space
    • (2) **CMYK color space**
    • (3) **HSV** (**Hue, Saturation, Value**) color space
    • (4) **YUV and YCbCr color space**
  • 【2】Color space conversion
    • 2.1 GRAY color space
      • 2.1.1 Conversion method:
      • 2.1.2 BGR -> GRAY
      • 2.1.3 How to prove `Gray = 0.299*R + 0.587*G + 0.114*B`
        • (1) Split the color image into three layers
        • (2) Prove that when the image is converted from the GRAY color space to the RGB color space, the final values of all channels will be the same.
  • 【3】Type conversion function
    • 【4】Mark specified color
    • 1. Lock specific value through inRange function
    • HSV color space

Color

That is, color is the result of the human visual system’s perception of different wavelengths of light reflection. People also define the “color” of visible light for electromagnetic waves in different wavelength ranges.

In daily life and art classes, the three colors (red, yellow and blue) are considered to be pigments that can be mixed to obtain all other colors.
For optics, the three primary colors (red, green and blueRGB) [here to distinguish the names] are the basis for creating other colors.

For example, the RGB value (255, 0, 0) represents pure red, (0, 255, 0) represents pure green, (0, 0, 255) represents pure blue, (0, 0, 0) represents black, (255 , 255, 255) means white.

【 1 】Color space (color gamut)

An abstract mathematical model with different dimensions and representations. In color science, people have established a variety of color models to represent a certain color with one-dimensional, two-dimensional, three-dimensional or even four-dimensional spatial coordinates. This coordinate system is The range of colors that can be defined is the color space. The color spaces we often use mainly include RGB, CMYK, Lab, etc.

Common:

(1)RGB color space

Different combinations of three basic colors are used to represent colors and are widely used in computer graphics and television display technology.

Conversion to xyz color space

Convert RGB color space to XYZ color space

import cv2 as cv

# Read RGB image
img_rgb = cv.imread("image.jpg")

#Convert RGB image to XYZ image
img_xyz = cv.cvtColor(img_rgb, cv.COLOR_BGR2XYZ)
Convert XYZ color space to RGB color space

import cv2 as cv

# Read XYZ image
img_xyz = cv.imread("image.jpg")

# Convert XYZ image to RGB image
img_rgb = cv.cvtColor(img_xyz, cv.COLOR_XYZ2BGR)

(2)CMYK color space

Colors are represented by different combinations of the four basic colors of Cyan, Magenta, Yellow and Black. Mainly used in printing industry. [Full color printing]

The abbreviation here uses the last letter K instead of the beginning B, because in the overall color science, B has been given to the Blue of RGB

(3)HSV (Hue, Saturation, Value) color space

HSV stands for Hue, Saturation, and Value.

  • Hue: Indicates the type of color, such as red, blue, green, etc. In the HSV model, hue is represented as an angle, ranging from 0 to 360 degrees. If calculated counterclockwise starting from red, red is 0°, green is 120°, and blue is 240°. Their complementary colors are: yellow is 60°, cyan is 180°, and violet is 300°;
  • Saturation: Indicates the purity of a color. The higher the saturation, the purer the color. The lower the saturation, the closer the color is to gray. In the HSV model, saturation ranges from 0 to 1.
  • Value: represents the brightness of a color. In the HSV model, lightness also ranges from 0 to 1, with 0 representing complete black and 1 representing the brightest color.

In OpenCV, you can use the cv.cvtColor function to convert the RGB color space to the HSV color space.

hsv_image = cv.cvtColor(rgb_image, cv.COLOR_RGB2HSV)


Hue refers to the color of light, which is related to the wavelength of light. Different wavelengths correspond to different hues, such as red, orange, yellow, etc.

Saturation represents the purity or depth of a color. Highly saturated colors are pure and have no components mixed with other colors. Low-saturation colors contain more gray or white components, making them appear lighter.

Brightness (Value) reflects the brightness of light, that is, the brightness of color. Higher brightness means lighter colors, lower brightness means darker colors. Brightness is affected by the white or black component of the color. An increase in the white component will increase the brightness, and an increase in the black component will decrease the brightness.

These concepts describe the different characteristics of color. Hue determines the type of color, saturation determines the purity of the color, and brightness determines the lightness or darkness of the color.

(4)YUV and YCbCr color space

Y represents brightness information, and U and V or Cb and Cr represent chrominance information. This separation method makes video compression more efficient.


【 2 】Color space conversion

It means that the state of a color space is expressed in another way.
For example: RGB -> HSV or RGB -> GRAY
In OpenCV, the performance of cv is BGR, then it is the transformation of BGR to HSV or GRAY, etc.

cv.cvtColor(input_image,flag)

input_image is the image that needs to be spatially transformed
flag is the converted type

cv.COLOR_BGR2GRAY:bgr->gray

cv.COLOR_BGR2HSV:bgr->hsv

2.1 GRAY color space

GRAY color space, also known as grayscale color space, each pixel is represented by a channel.

This kind of grayscale has been introduced before. At that time, we used binary images as a guide. Binary values are images that are either black or white, and they are divided into ‘greyscale levels’ (Only 256 gray levels, pixel value range: [0, 255], from black to white)

It can help simplify problems and reduce complexity in image processing and computer vision while still retaining most of the structural and shape information.

2.1.1 Conversion method:

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

This weight distribution is designed based on the human eye’s sensitivity to different colors. The human eye is most sensitive to green, followed by red, and blue is the least sensitive. This is because there are three types of color receptors on the retina of the human eye, which are most sensitive to red, green and blue light.

2.1.2 BGR -> GRAY

Because OpenCV defaults to BGR display mode.
You can use the cvtColor function, which is a function in the OpenCV library to convert an image from one color space to another.

cvtColor(src, code[, dst[, dstCn]]) -> dst

The conventional ranges for R, G, and B channel values are:
. – 0 to 255 for CV_8U images
. – 0 to 65535 for CV_16U images
. – 0 to 1 for CV_32F images

parameter:

import numpy as np
import cv2 as cv

# Read a color image
img = cv.imread('./Pic/test_img.jpg')

#Create an empty image of the same size as the input image to store the conversion results
dst = np.zeros_like(img)

# Use the cvtColor function to convert the image from BGR color space to grayscale color space
# We provided the dst parameter, so the function will store the conversion result in this image
# We also provide the dstCn parameter, specifying the number of channels of the output image as 1
cv.cvtColor(img, cv.COLOR_BGR2GRAY, dst=dst, dstCn=1)

# Print the number of channels of the converted image, which should be 1
print(dst.shape)

# (864, 1920, 3)

np.zeros_like(img) will create an all-zero array with the same shape (i.e. the same number of rows and columns) and data type as img. This means that the returned array will have the same dimensions as img and each element will be initialized to zero.

The role of this function in the above example is to create an empty image with the same size and depth as the input image img, used to store the conversion result of the cvtColor function. By using np.zeros_like(img) we can ensure that the empty image created has the same shape and data type as the input image, thus avoiding size or type mismatch errors during conversion.

Original image

no parameters

import cv2 as cv

# Read a color image
img = cv.imread('pic.jpg')

# Use the cvtColor function to convert the image from BGR color space to grayscale color space
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Print the channel number of the converted image, which should be 1
print(gray.shape)

2.1.3 How to prove Gray = 0.299*R + 0.587*G + 0.114*B

(1) Split the color image into three layers

Use the function b,g,r=cv.split(img1)

Step1: Basic code

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1=cv.imread("Pic/test_img.jpg")
# img1=cv.imread("Pic/test_img.jpg",0) implements the same logic as below
src=cv.cvtColor(img1,cv.COLOR_BGR2GRAY)
plt.imshow(img1[:,:,::-1])

Step2: Split

b,g,r=cv.split(img1)
img1


Step3: Split situation
[ 1 ] Grayscale src (original image img1)

[2]b

[3]g

[ 4 ] r

Step4: Calculation (because it is an integer, it will be rounded)

(2) Prove that when an image is converted from GRAY color space to RGB color space, the final values of all channels will be the same.

When converting from a grayscale image (GRAY) back to an RGB image, the values of all R, G, and B channels will be the same. This is because the grayscale image has only one channel, so when converting back to an RGB image, the value of this single channel will be copied to the R, G, and B channels.

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# Read grayscale image
img_gray = cv.imread("Pic/test_img.jpg", 0)

#Convert grayscale image to RGB image
img_rgb = cv.cvtColor(img_gray, cv.COLOR_GRAY2BGR)

# Separate RGB channels
b, g, r = cv.split(img_rgb)

# Check whether the values of the three channels R, G, and B are the same
print("R == G: ", np.all(r == g))
print("R == B: ", np.all(r == b))
print("G == B: ", np.all(g == b))

First read a grayscale image and then convert it to an RGB image. Then, it separates out the three channels R, G, and B and checks whether the values of these three channels are the same. If all outputs are True, it proves that the values of all R, G, and B channels are the same when converting from a grayscale image to an RGB image.

The values of the three RGB channels

【3】Type conversion function

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

cv2.cvtColor() is a function in OpenCV used for color space conversion. It accepts three parameters:

  • src: Input image, which can be a NumPy array or an OpenCV Mat object.
  • code: The code for color space conversion, specifying the type of conversion to be performed. Common conversion types include:
  • cv2.COLOR_BGR2GRAY: Convert BGR image to grayscale image.
  • cv2.COLOR_BGR2HSV: Convert BGR image to HSV color space.
  • cv2.COLOR_BGR2RGB: Convert BGR image to RGB color space.
  • Other conversion types can be found in OpenCV’s documentation.
  • dstCn (optional): Number of channels of the target image. The default value is 0, which means the same number of channels as the input image.

The return value of the function is the converted image, returned as a NumPy array.

【4】Mark specified color

In the HSV color space, the H channel (saturation Hue channel) corresponds to different colors.

1. Lock specific values through the inRange function

In OpenCV, the function cv2.inRange() is used to determine whether the pixel value of the pixel in the image is within the specified range.
The syntax format is:
dst = cv2.inRange( src, lowerb, upperb )
In the formula:
? dst represents the output result, the size is consistent with src.
? src represents the array or image to be checked.
? lowerb represents the lower bound of the range.
? upperb represents the upper bound of the range.
The return value dst is 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]
Inside:
? If the src value falls within the specified interval, the value at the corresponding position in dst is 255.
? If the src value is not within the specified interval, the value at the corresponding position in dst is 0

HSV color space

The RGB value of blue is [[[255 0 0]]], and the converted HSV value is [[[120 255 255]]].
The RGB value of green is [[[0 255 0]]], and the converted HSV value is [[[60 255 255]]].
The RGB value of red is [[[0 0 255]]], and the converted HSV value is [[[0 255 255]]].