02 OpenCV image channel processing

1-channel extraction and merging

In digital image processing, an image channel refers to the separation of color information in an image into different color components. Common image channels include RGB channels, grayscale channels, HSV channels, etc.

The RGB channel refers to separating the image into three color channels of red, green and blue, and each channel represents the brightness of the corresponding color. This way is the most common way, it is very important for color image processing.

The grayscale channel refers to converting the color information in the image into a grayscale brightness value, and using a single channel to represent the entire image. This method is more suitable for black-and-white images or when only the brightness information of the image needs to be considered in color images.

The HSV channel refers to separating the color information in the image into three channels: hue (H), saturation (S) and brightness (V). This method is more intuitive than RGB in controlling color changes, and is more suitable for color adjustment in image processing.

In image processing, it is common to use different channels to process and manipulate images. For example, using grayscale channels can make images easier to process because only a single channel needs to be considered, while using RGB channels can make images easier to display on color display devices. When processing and analyzing images, selecting appropriate channels and channel combinations is very important and can help us better understand and control images.

1 Array form

The data of the specified channel can be extracted in the form of an array, or the data of the specified channel can be extracted using the split of cv2.
Through indexing, each channel can be directly extracted from the image. The channel information is in the last column of each row of the array.

# 以下代码使用数组的形式对通道进行处理
import cv2  
lena = cv2.imread("lenacolor.png")  
cv2.imshow("lena1", lena)  
b = lena[:, :, 0]  
g = lena[:, :, 1]  
r = lena[:, :, 2]  
cv2.imshow("b", b)  
cv2.imshow("g", g)  
cv2.imshow("r", r)  

The above code can extract and display the data of the three channels of BGR respectively. It should be noted that the channel order of cv2 is not the familiar RGB.

lena[:, :, 0] = 0  
cv2.imshow("lenab0", lena)  
lena[:, :, 1] = 0  
cv2.imshow("lenab0g0", lena)  
cv2.waitKey()  
cv2.destroyAllWindows()

The above code can realize the batch assignment of a certain channel, and assign a value of 0 to a certain channel, that is, delete this channel to some extent. The processed image will undergo changes in chroma.
image.png

2 split and merge forms

The function cv2.split() is able to split the channels of an image. For example, the following statement can be used to split the color BGR image img to obtain B-channel image b, G-channel image g and R-channel image r.

b, g, r=cv2.split(img)

b=cv2.split(img)[0]
g=cv2.split(img)[1]
r=cv2.split(img)[2]

The above are two methods of extracting channels using split.
After processing the BGR channel, in order to display the graphics, the three channels need to be merged.

bgr = cv2.merge([b, g, r])  
rgb = cv2.merge([r, g, b])  
cv2.imshow("real", bgr)  
cv2.imshow("fake", rgb)

Normally, we still need to combine the graphs in the order of BGR. But if we do not follow this order or bring in data from other bands, a false picture is produced (a concept that will be more familiar to students in the field of remote sensing).
image.png

Guess you like

Origin blog.csdn.net/nkufang/article/details/128993656