I am learning OpenCV color space conversion in Vscode


It is color, 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.
As for optics, it is 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.


(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

Insert image description here

Convert RGB color space to XYZ color space

Insert image description here

import cv2 as cv

# 读取RGB图像
img_rgb = cv.imread("image.jpg")

# 将RGB图像转换为XYZ图像
img_xyz = cv.cvtColor(img_rgb, cv.COLOR_BGR2XYZ)
Convert XYZ color space to RGB color space

Insert image description here

import cv2 as cv

# 读取XYZ图像
img_xyz = cv.imread("image.jpg")

# 将XYZ图像转换为RGB图像
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 B has been given to the Blue of RGB in overall color science.Insert image description here

(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)

Insert image description here
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.

Insert image description here
Insert image description here

【2】Color space conversion

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 Transformation from BGR to HSV or GRAY


input_image 是需要进行空间转换的图像



2.1 GRAY color space

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

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 grayscale images.'Grayscale level' (There are only 256 grayscale levels, the range of pixel values: [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 is a function in the OpenCV library that is used to convert an image from one color space to another.

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

Insert image description here

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

Insert image description here

import numpy as np
import cv2 as cv

# 读取一张彩色图片
img = cv.imread('./Pic/test_img.jpg')

# 创建一个与输入图像同样大小的空图像,用于存储转换结果
dst = np.zeros_like(img)

# 使用cvtColor函数将图片从BGR色彩空间转换到灰度色彩空间
# 我们提供了dst参数,所以函数将把转换结果存储在这个图像中
# 我们也提供了dstCn参数,指定输出图像的通道数为1
cv.cvtColor(img, cv.COLOR_BGR2GRAY, dst=dst, dstCn=1)

# 打印转换后的图像的通道数,应该为1

# (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.
Insert image description here

Original pictureInsert image description here

no parameters

import cv2 as cv

# 读取一张彩色图片
img = cv.imread('pic.jpg')

# 使用cvtColor函数将图片从BGR色彩空间转换到灰度色彩空间
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# 打印转换后的图像的通道数,应该为1

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

(1) Split the color image into three layers

Use functionb,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",0)  实现下面的同理

Insert image description here

Step2: Split


Insert image description here
Step3: Split situation
[ 1 ] Grayscale src (original image img1)
Insert image description here
[ 2 ] b
Insert image description here
[ 3 ] g
Insert image description here
[ 4 ] r
Insert image description here
Step4: Calculation (because it is an integer, it will be rounded)
Insert image description here

(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.

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

# 读取灰度图像
img_gray = cv.imread("Pic/test_img.jpg", 0)

# 将灰度图像转换为RGB图像
img_rgb = cv.cvtColor(img_gray, cv.COLOR_GRAY2BGR)

# 分离RGB通道
b, g, r = cv.split(img_rgb)

# 检查R、G、B三个通道的值是否相同
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.
Insert image description here

The values ​​of the three RGB channels
Insert image description here

【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 value through inRange function

OpenCV uses the function cv2.inRange() to determine whether the pixel value of the pixel in the image is within the specified range. Its
syntax format is:
dst = cv2.inRange( src, lowerb, upperb )
 dst represents the output result, the size is the same as 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 within the interval [lowerb,upperb]
:  If the src value is not within the specified interval, the value at the corresponding position in dst is 0
 If the src value is within the specified interval, the value at the corresponding position in dst is 255.

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]]].

Guess you like

Origin blog.csdn.net/m0_74154295/article/details/134351532