圖片有分 3 channel (RGB) 與 1 channel (gray), 還有一些會多一個透明的 channel.
要知道 opencv 圖片的長像, 可以使用 image_object.shape 可以看到, (height, width) 或是 (height, width, channel) 二種.
3 轉 1:
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)
1 轉 3:
img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray
資料來源
How to convert a 1 channel image into a 3 channel with opencv2?
https://stackoverflow.com/questions/14786179/how-to-convert-a-1-channel-image-into-a-3-channel-with-opencv2