I am learning OpenCV color space conversion in Vscode

color

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.

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

# 读取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

在这里插入图片描述

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.在这里插入图片描述

(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

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

cv.cvtColor(input_image,flag)

input_image 是需要进行空间转换的图像
flag为转换后的类型

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 ’ 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

在这里插入图片描述

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

# 读取一张彩色图片
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
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 picture在这里插入图片描述

no parameters

import cv2 as cv

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

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

# 打印转换后的图像的通道数,应该为1
print(gray.shape)

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")
# img1=cv.imread("Pic/test_img.jpg",0)  实现下面的同理
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 the image is converted from the GRAY color space to the RGB color space, the final values ​​of all channels will be the same.

从灰度图像(GRAY)转换回RGB图像时,所有的R、G、B通道的值都会是相同的。这是因为灰度图像只有一个通道,所以在转换回RGB图像时,这个单一的通道的值会被复制到R、G、B三个通道。

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

首先读取一个灰度图像,然后将其转换为RGB图像。然后,它分离出R、G、B三个通道,并检查这三个通道的值是否相同。如果所有的输出都是True,那么就证明了在从灰度图像转换为RGB图像时,所有的R、G、B通道的值都是相同的。
在这里插入图片描述

RGB三个通道的值
在这里插入图片描述

【3】类型转换函数

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

cv2.cvtColor() 是OpenCV中的一个函数,用于进行颜色空间的转换。它接受三个参数:

  • src:输入图像,可以是一个NumPy数组或一个OpenCV的Mat对象。
  • code:颜色空间转换的代码,指定了要进行的转换类型。常见的转换类型包括:
  • cv2.COLOR_BGR2GRAY:将BGR图像转换为灰度图像。
  • cv2.COLOR_BGR2HSV:将BGR图像转换为HSV色彩空间。
  • cv2.COLOR_BGR2RGB:将BGR图像转换为RGB色彩空间。
  • 其他转换类型可以在OpenCV的文档中找到。
  • dstCn(可选):目标图像的通道数。默认值为0,表示与输入图像的通道数相同。

函数的返回值是转换后的图像,以NumPy数组的形式返回。

【4】标记指定颜色

在 HSV 色彩空间中,H 通道(饱和度 Hue 通道)对应不同的颜色。

1.通过inRange函数锁定特定值

OpenCV 中通过函数 cv2.inRange()来判断图像内像素点的像素值是否在指定的范围内,其
语法格式为:
dst = cv2.inRange( src, lowerb, upperb )
式中:
 dst 表示输出结果,大小和 src 一致。
 src 表示要检查的数组或图像。
 lowerb 表示范围下界。
 upperb 表示范围上界。
返回值 dst 与 src 等大小,其值取决于 src 中对应位置上的值是否处于区间[lowerb,upperb]
内:
 如果 src 值处于该指定区间内,则 dst 中对应位置上的值为 255。
 如果 src 值不处于该指定区间内,则 dst 中对应位置上的值为 0

HSV色彩空间

蓝色的RGB值为[[[255 0 0]]],转换为HSV值为[[[120 255 255]]]。
绿色的RGB值为[[[0 255 0]]],转换为HSV值为[[[60 255 255]]]。
红色的RGB值为[[[0 0 255]]],转换为HSV值为[[[0 255 255]]]。

рекомендация

отblog.csdn.net/m0_74154295/article/details/134351532