我在Vscode学OpenCV 色彩空间转换

色彩

即是颜色,一种人体视觉系统对光的反射的不同波长的感知的结果。人们又对不同的波长范围的电磁波定义可视光的“颜色”。

在日常生活、美术课中,通过把(红黄蓝)三种颜色成为”认为是能够混合得到其他所有颜色的颜料。
而对于光学,就把(红绿蓝RGB)三基色【此处为了区分名字】是能够创建其他颜色的基本。

例如,RGB值(255, 0, 0)表示纯红色,(0, 255, 0)表示纯绿色,(0, 0, 255)表示纯蓝色,(0, 0, 0)表示黑色,(255, 255, 255)表示白色。

【 1 】色彩空间(色域)

一种抽象的数学模型,以不同的维度和表示方式,色彩学中,人们建立了多种色彩模型,以一维、二维、三维甚至四维空间坐标来表示某一色彩,这种坐标系统所能定义的色彩范围即色彩空间。我们经常用到的色彩空间主要有RGB、CMYK、Lab等。

常见的:

(1)RGB色彩空间

三种基本颜色的不同组合来表示颜色,在计算机图像和电视显示技术中广泛使用。

与xyz色彩空间的转换

Insert image description here

将 RGB 色彩空间转换为 XYZ 色彩空间

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)
将 XYZ 色彩空间转换为 RGB 色彩空间

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色彩空间

青色(Cyan)、品红(Magenta)、黄色(Yellow)加上黑色(Key)四种基本颜色的不同组合来表示颜色。主要用于印刷业。[全彩印刷]

此处缩写使用最后一个字母K而非开头的B,是因为在整体色彩学中已经将B给了RGB的Blue蓝色Insert image description here

(3)HSVHue, Saturation, Value)色彩空间

HSV代表色调(Hue)、饱和度(Saturation)、明度(Value)。

  • 色调(Hue):表示颜色的种类,如红色、蓝色、绿色等。在HSV模型中,色调被表示为角度,范围从0到360度。若从红色开始按逆时针方向计算,红色为0°,绿色为120°,蓝色为240°。它们的补色是:黄色为60°,青色为180°,紫色为300°;
  • 饱和度(Saturation):表示颜色的纯度,饱和度越高,颜色越纯,饱和度越低,颜色越接近灰色。在HSV模型中,饱和度的范围是0到1。
  • 明度(Value):表示颜色的亮度。在HSV模型中,明度的范围也是0到1,0表示完全的黑色,1表示最亮的颜色。

OpenCV中,可以使用cv.cvtColor函数将RGB色彩空间转换为HSV色彩空间

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

Insert image description here
色调(Hue)是指光的颜色,与光的波长相关。不同的波长对应不同的色调,例如红色、橙色、黄色等。

饱和度(Saturation)表示颜色的纯净度或深浅程度。高饱和度的颜色是纯净的,没有混合其他颜色的成分。低饱和度的颜色则含有更多的灰色或白色成分,使其看起来较淡。

亮度(Value)反映了光的明暗程度,即颜色的明亮度。较高的亮度表示颜色较亮,较低的亮度表示颜色较暗。亮度受到颜色中白色或黑色成分的影响,白色成分增加会使亮度增加,黑色成分增加会使亮度减少。

这些概念描述了颜色的不同特性,色调决定了颜色的种类,饱和度决定了颜色的纯净度,亮度决定了颜色的明暗程度。

(4)YUV和YCbCr色彩空间

Y表示亮度信息,U和V或Cb和Cr表示色度信息,这种分离的方式使得视频压缩更为高效。

Insert image description here
Insert image description here

【 2 】色彩空间转换

是指有一种色彩空间的状态以另一种方式表现出来。
例如:RGB -> HSV 或者RGB -> GRAY
而在OpenCV中,cv的表现是BGR那么就是BGR向HSV或者GRAY等的转变

cv.cvtColor(input_image,flag)

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

cv.COLOR_BGR2GRAY:bgr->gray

cv.COLOR_BGR2HSV:bgr->hsv 

2.1 GRAY色彩空间

GRAY色彩空间,也被称作灰度色彩空间,每个像素按照一个通道去 ’ 灰度 ’ 表示。

这种灰度在先前也介绍过,当时我们以二值图像为引,二值是非黑即白的图像,而在灰度图中给其划分开了 ‘ 灰度级别 ’ (只有256个灰度级别,像素值的范围:[0,255] ,由黑向白)

它可以帮助在图像处理和计算机视觉人物中简化问题,降低复杂性,同时仍然保留了大部分的结构和形状信息。

2.1.1 转换方式:

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

这种权重分布是基于人眼对不同颜色的敏感度来设计的。人眼对绿色的敏感度最高,红色次之,蓝色最低。这是因为人眼中的视网膜上有三种类型的颜色感受器,分别对红色、绿色和蓝色光最为敏感。

2.1.2 BGR -> GRAY

由于OpenCV默认是BGR的显示方式。
可以使用cvtColor函数是OpenCV库中的一个函数,用于将图像从一个颜色空间转换到另一个颜色空间。

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
print(dst.shape)

# (864, 1920, 3)

np.zeros_like(img)将创建一个与img具有相同形状(即相同的行数和列数)和数据类型的全零数组。这意味着返回的数组将具有与img相同的维度,并且每个元素都将被初始化为零。

这个函数在上述示例中的作用是创建一个与输入图像img具有相同大小和深度的空图像,用于存储cvtColor函数的转换结果。通过使用np.zeros_like(img),我们可以确保创建的空图像与输入图像具有相同的形状和数据类型,从而避免了在转换过程中出现大小或类型不匹配的错误。
Insert image description here

原图Insert image description here

没有参数

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 如何证明Gray = 0.299*R + 0.587*G + 0.114*B

(1) 把彩色图拆分成三层图层

使用函数b,g,r=cv.split(img1)

Step1: 基本代码

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

Insert image description here

Step2:拆分

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

Insert image description here
Step3:拆分情况
[ 1 ] 灰度的src(原图img1)
Insert image description here
[ 2 ] b
Insert image description here
[ 3 ] g
Insert image description here
[ 4 ] r
Insert image description here
Step4:计算(因为是整数所以会四舍五入计算)
Insert image description here

(2)证明当图像由 GRAY 色彩空间转换为 RGB 色彩空间时,最终所有通道的值都将是相同的。

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 )
Where:
 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]]].

おすすめ

転載: blog.csdn.net/m0_74154295/article/details/134351532