For an image, we can do some processing on its channels. The normal image includes three channels of BGR. How can we separate and merge the channels together? The first two apis you need to know to realize channel separation and merging.- Split (mat): split the picture channel. The input is a mat type picture. After the split, multiple channels are output.
- Merge ((ch1, ch2,...)): Combines multiple channels together.
import cv2
import numpy as np
# 创建一个黑色图片
img=np.zeros((480,640,3),np.uint8)
# 通道分割
b,r,g=cv2.split(img)
# 修改部分通道
g[10:100,10:100]=255
r[10:100,10:100]=255
# 通道合并
img2=cv2.merge((r,g,b))
cv2.imshow('img',img)
cv2.imshow('b',b)
cv2.imshow('g',g)
cv2.imshow('r',r)
cv2.imshow('rg',img2)
cv2.waitKey(0)