OpenCV python (3) [Image preprocessing: color space conversion] && color recognition

1. The difference between color space and color gamut

Regarding these two nouns, they were often confused during the period when I first started learning. The following is my current personal understanding of color space and color gamut.
Color space refers to the use of numbers to represent colors, and color gamut represents the range of a color space. Therefore, color gamut conversion actually represents a change to a subset of the color space, that is, an increase or decrease in the range of pixel values, while color space conversion represents the replacement of this color model, which not only changes the range, but also changes Change the color represented by each number or array, and even change the dimension, that is, change the depth of the image.

2. Color space

The color space described below is the color space that I personally need to use at present.

1. Gray color space

The gray color space is what we usually call a grayscale image, usually an 8-bit grayscale image, that is, the range of 0~255. The lower the pixel value, the darker the grayscale value , the closer to black, and vice versa. , when the boundary value is reached, that is, when the pixel value is 0, the pixel point is pure black, and when the pixel value is 255, the pixel point is pure white .
Using this color space can greatly reduce the amount of computation, and it is also convenient for subsequent image processing operations. For example, for image processing operations that require one-dimensional images such as RGB image binarization, you can first convert the RGB image into a GRAY image, and then perform Binarization operation .

2. RGB color space

In general, the images we obtain through cameras or other hardware devices are RGB images.
An RGB image consists of three color channels : red (R), green (G), and blue (B) . Each channel has a byte size, that is, the range is 0~255, that is, one pass has 256 pixel values. Then corresponding to 3 channels, the types of colors that can be represented can reach 256×256×256, that is, 16,777,216 colors. The RGB color space includes almost all the colors that human vision can perceive, so it is also one of the most widely used color spaces at present. one.
It can be seen from the above that each pixel in the RGB color space has 3 pixel values, that is, 1 pixel is represented by an array composed of 3 pixel values. Corresponding to the three channels of R, G, and B, namely Red, Green, and Blue, when the RGB value of the pixel point is (255, 0, 0), the point is red, and when the RGB value is (0, 255, 0 ) , The point is green , and when the RGB value is (0, 0, 255), the point is blue . And (0, 0, 0) means black, and (255, 255, 255) means white . It can be seen that the RGB color space is superimposed by the pixel values ​​of each channel, and different colors are obtained from the gray level through three layers of superposition.
In addition, (255, 255, 0) represents yellow , and (0, 255, 255) represents cyan .

3. HSV color space

As mentioned above, the color types and ranges of the RGB color space are very complicated and not obvious enough. The HSV color space can divide color blocks within a certain range.
The HSV color space consists of hue H, saturation S, and brightness V. The range of H is 0 to 180, the range of S is 0 to 255, and the range of V is 0 to 255. The specific color intervals are shown in the table below .
insert image description here

3. Color space conversion: cvtColor

1. Get RGB, GRAY, HSV images

In the first article, OpenCV python (1): Installation && Obtaining, Displaying, and Saving Images , there are methods to obtain RGB images through hardware such as cameras or directly from videos and pictures. These methods are described in detail in the above articles I will not go into details here, and mainly list how to obtain images in the GRAY and HSV color spaces.
In fact, the acquisition of GRAY images is also mentioned in the previous article. In the imread method, you can choose to obtain grayscale images, as shown in the following two methods for imread to obtain grayscale images. You can also check the imread function in the previous article.

cv2.imread('xx.jpg', 0)	# 0为灰度图
cv2.imread('xx.jpg', cv2.IMREAD_GRAYSCALE)	# 灰度图

When you need to use the camera or video to obtain images, you need to save and then obtain them before using imread, which is very troublesome and inefficient. At this time, if you want to obtain grayscale images or HSV images, you can use cvtColor provided by opencv-python function.

获取灰度图:img_g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
获取HSV图:img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
'''
img 为获取到的RGB图像,此处则将RGB转换为GRAY和HSV
cv2.COLOR_BGR2GRAY为 BGR to GRAY
cv2.COLOR_BGR2HSV为 BGR to HSV
BGR也是RGB颜色空间,只是顺序调换了,前面输入的img为RGB颜色空间即可。
'''

2. Obtain RGB, GRAY, HSV image cases

(1), the camera obtains the image and then converts it

The program looks like this:

import cv2  # 导入opencv库

if __name__ == '__main__':
    video = cv2.VideoCapture(0)  # 选择摄像头0、1... 或 选择视频路径
    while True:
        ret, img_RGB = video.read()
        # 获取摄像头或视频的一帧,ret判断获取是否成功,成功则为True;img为获取的图像
        if ret:
            img_GRAY = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2GRAY)  # 获取GRAY图像
            img_HSV = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2HSV)  # 获取HSV图像

            cv2.imshow("RGB", img_RGB)  # 显示RGB图像
            cv2.imshow("GRAY", img_GRAY)  # 显示GRAY图像
            cv2.imshow('HSV', img_HSV)  # 显示HSV图像

            cv2.waitKey(1)  # 等待时间
        else:
            # print("摄像头未打开。")
            pass  # 图像获取失败,可能未接入摄像头

(2), import image direct conversion & re-conversion

The program looks like this:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img_RGB = cv2.imread('img/2.jpg', 1)    # 获取路径img/2.jpg的图像,图像类型为RGB图像
        img_GRAY = cv2.imread('img/2.jpg', 0)  # 获取路径img/2.jpg的图像,图像类型为GRAY图像
        img_HSV = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2HSV)     # 获取HSV图像

        cv2.imshow("RGB", img_RGB)  # 显示RGB图像
        cv2.imshow("GRAY", img_GRAY)  # 显示GRAY图像
        cv2.imshow('HSV', img_HSV)  # 显示HSV图像

        cv2.waitKey(1)  # 等待时间

The effect is as follows. After obtaining the red envelope image, convert the red envelope into a gold envelope, that is, convert the RGB image into an HSV image:
insert image description here

3. Convert GRAY and HSV images back to RGB images

As shown in the figure above, you can see the gray packet and the gold packet, that is, the Gray image and the HSV image of the red packet. On the basis of the above program, convert them into RGB images respectively, the program is as follows:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img_RGB = cv2.imread('img/2.jpg', 1)    # 获取路径img/0.jpg的图像,图像类型为RGB图像
        img_GRAT = cv2.imread('img/2.jpg', 0)  # 获取路径img/0.jpg的图像,图像类型为GRAY图像
        img_HSV = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2HSV)     # 获取HSV图像

        img_GRAT2RGB = cv2.cvtColor(img_GRAT, cv2.COLOR_GRAY2BGR)   # 将灰度图转换为RGB图
        img_HSV2RGB = cv2.cvtColor(img_HSV, cv2.COLOR_HSV2BGR)   # 将HSV图转换为RGB图

        cv2.imshow("RGB", img_RGB)  # 显示RGB图像
        cv2.imshow("GRAY2RGB", img_GRAT2RGB)  # 显示GRAY图像
        cv2.imshow('HSV2RGB', img_HSV2RGB)  # 显示HSV图像

        cv2.waitKey(1)  # 等待时间

The effect is as shown below. It can be seen that when the HSV image is converted back to the RGB image again, the original information of the image is not changed, and the original image is restored; while the GRAY image is converted back to the RGB image again, the image loses a lot of color information . This is because the number of channels in the GRAY color space is 1, while the number of channels in the RGB color space and the HSV color space are both 3. When the number of 3 channels is converted to the number of 1 channel, a lot of color information will inevitably be lost, but at the same time , It will also drastically reduce the runtime of subsequent image processing operations .
insert image description here

4. Color recognition: HSV+inRange

The following will describe how to combine the above color space conversion to perform color recognition and binarization .

1. inRange function

inRange is a binarization function based on HSV color space provided by opencv-python . The specific usage method is as follows:

img_bin = cv2.inRange(img_hsv, lower, upper)	#低于lower、高于upper为0,中间为255

2. Case

The program looks like this:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img_RGB = cv2.imread('img/3.jpg', 1)    # 获取路径img/0.jpg的图像,图像类型为RGB图像
        img_HSV = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2HSV)     # 获取HSV图像

        img_bin1 = cv2.inRange(img_HSV, (0, 43, 46), (10, 255, 255))  # 低于lower、高于upper为0,中间为255
        img_bin2 = cv2.inRange(img_HSV, (156, 43, 46), (180, 255, 255))  # 低于lower、高于upper为0,中间为255
        img_bin = cv2.add(img_bin1, img_bin2)      # 将两张图像相加,即汇总所有白色像素

        cv2.imshow("RGB", img_RGB)  # 显示RGB图像
        cv2.imshow('HSV', img_HSV)  # 显示HSV图像
        cv2.imshow('BIN1', img_bin1)    # 显示红色范围一的二值化图像
        cv2.imshow('BIN2', img_bin2)    # 显示红色范围二的二值化图像
        cv2.imshow('BIN', img_bin)  # 显示红色总范围的二值化图像

        cv2.waitKey(1)  # 等待时间

The effect is as follows:
insert image description here
the realized function is to binarize the red area in the image into white pixels, and binarize the rest of the colors into black pixels. When positioning again, it becomes a one-dimensional image, and the red area can be found by traversing the image. .
The purpose of adding the two images here is to sum up the whites in the two ranges, that is, to ensure that the reds in the two ranges are recognized . The reason is as shown in the figure below. Red has two colors in the HSV color space. range.
The rule of adding binarized images is very simple. In the case of ensuring that the images are equal in size: 0+255=255; 255+0=255; 0+0=0; 255+255=255 .
The 1-3 addition formulas are easy to understand. The fourth addition formula is actually because of a saturation operation that comes with opencv image addition, that is, when the number exceeds 255, it is set to 255.
insert image description here
I am a student and I am currently studying. This article can be regarded as my study notes. Please correct me if I am wrong.

Guess you like

Origin blog.csdn.net/xztli/article/details/126155365