opencv channel separation split, merge

1.Channel classification split()

Channel separation can be used for color image processing. The image object can be an ordinary 3-channel BGR color image, and the three channels after separation are b, g, and r.
If it is a BGRA 4-channel image with an alpha channel, they are b, g, r, and a after separation. ]
If the image is an image in other color spaces such as an HSV image, the three separated images are h, s, and v respectively.

import cv2
def show_img(win_name,img,wait_time=0,img_ratio=0.5,is_show=True):
    if is_show is not True:
        return 
    rows = img.shape[0]
    cols = img.shape[1]
    cv2.namedWindow(win_name, cv2.WINDOW_NORMAL )#cv2.WINDOW_AUTOSIZE)
    cv2.resizeWindow(win_name,(int(cols*img_ratio),int(rows*img_ratio)))
    cv2.imshow(win_name,img)
    if wait_time >= 0:
        cv2.waitKey(wait_time)
        
img = cv2.imread('..\\lena.jpg')
#img = cv2.imread('..\\opencv-logo.png',cv2.IMREAD_UNCHANGED)
if img is not None and len(img.shape)==3: #彩色图像才可以做通道分离
    print('img.shape:',img.shape)
    show_img('img',img,-1)  
    if img.shape[2] == 3:                 #如果是3通道,分离出3个图像实例
        b,g,r = cv2.split(img)     
        show_img('b',b,-1)
        show_img('g',g,-1)
        show_img('r',r,-1)
        cv2.waitKey(0)
    elif img.shape[2] == 4:               #如果是4通道
        b,g,r,a = cv2.split(img)
        show_img('b',b,-1)
        show_img('g',g,-1)
        show_img('r',r,-1)
        show_img('a',a,-1) 
        cv2.waitKey(0)

2. Index mode channel separation
uses numpy array slicing or indexing operations, such as using img[:,:,0] to separate the 0 channel or b channel, img[:,:,1] corresponds to the g channel, img[:,: ,2] corresponds to the r channel, and if there is img[:,:,3], it corresponds to the alpha channel.

if img.shape[2] == 3:                 #如果是3通道,分离出3个图像实例
        b = img[:,:,0]
        g = img[:,:,1]    
        r = img[:,:,2]             
        show_img('b',b,-1)
        show_img('g',g,-1)
        show_img('r',r,-1)
        cv2.waitKey(0)
elif img.shape[2] == 4:               #如果是4通道
        b = img[:,:,0]
        g = img[:,:,1]    
        r = img[:,:,2]    
        a = img[:,:,3] 
        show_img('b',b,-1)
        show_img('g',g,-1)
        show_img('r',r,-1)
        show_img('a',a,-1) 
        cv2.waitKey(0)

3. Channel merging merge()
The existing multiple channel images are constructed into a tuple and passed to merge() to achieve image merging.
The parameter of merge() is a tuple composed of multi-channel images

import cv2
img = cv2.imread('lena.jpg')  
b = img[:,:,0]
g = img[:,:,1]    
r = img[:,:,2]             
img2 = cv2.merge((b,g,r)) #传入bgr构成的元组
cv2.imshow('merged',img2) 
cv2.waitKey(0)

4. Separate grayscale images

import cv2
img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE) 
print('img.shape:',img.shape)
res = cv2.split(img)
#单通道的灰度图用split()分离后,实际得到的是一个包含了array类型的list,效果和对一个numpy数组做split操作是类似的。

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129762727