Python OpenCV: Extract Blue, Green and Red Channel from Color Image

In this tutorial, we will use an example to show you how to extract blue, green and read channel from a color image in python opencv.

Python OpenCV - Extract Blue, Green and Red Channel from Color Image for Beginners

1.Read an image

import cv2
 
#read image
src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED)
print(src.shape)

2.Extract Blue, Green and Red channel

#extract blue channel
blue_channel = src[:,:,0]
#extract green channel
green_channel = src[:,:,1]
#extract red channel
red_channel = src[:,:,2]

3.Create mask images

blue_img = np.zeros(src.shape)
green_img = np.zeros(src.shape)
red_img = np.zeros(src.shape)

4.Assign the color channel of src to mask images

blue_img[:,:,0] = blue_channel
green_img[:,:,1] = green_channel
red_img[:,:,2] = red_channel

3.Save images

cv2.imwrite('D:/cv2-red-channel.png',red_img) 
cv2.imwrite('D:/cv2-green-channel.png',green_img) 
cv2.imwrite('D:/cv2-blue-channel.png',blue_img)

Moreover, we also can use cv2.split() method to do it. Here is the tutorial:

Split Image Channels to Red, Green and Blue Using Python OpenCV

Run this code, you will see these images: