Visualize the Different Color Channels of an RGB Image in Python using OpenCV

In this tutorial, we will learn to visualize different color channels of an RGB Image using OpenCV in Python.

RGB (Red, Green, and Blue) model is the standard color model used in image processing. Each color in RGB has values ranging from 0-255. The RGB model can generate around 16 million color shades.

We will be using the OpenCV library to split the channels of the RGB image. In order to install the OpenCV library on your local machine, fire up Command Prompt/Terminal and write:

pip install opencv-python

Code to split RGB image into different color channels

Input Image

Visualize the Different Color Channels of an RGB Image in Python using OpenCV

Importing OpenCV library.

# Import OpenCV library
import cv2 as cv

Read input image. imread() function will read the input image.

# Open/Read input image
img=cv.imread("rgb.png")

Splitting the RGB input image into different color channels using split() function. This function helps to convert multi-channel images into single-channel images.

# Splitting the image into different color channels 
b,g,r=cv.split(img)

cv.imshow("Red",r)
cv.imshow("Green",g)
cv.imshow("Blue",b)

cv.waitKey() function displays the output image for a specified period of time or until any key is pressed.

# Displays the output window untill any key is pressed
cv.waitKey(0)

Combining all code

# Import OpenCV library
import cv2 as cv

# Open/Read input image
img=cv.imread("rgb.png")

# Splitting the image into different color channels 
b,g,r=cv.split(img)

cv.imshow("Red",r)
cv.imshow("Green",g)
cv.imshow("Blue",b)

# Displays the output window untill any key is pressed
cv.waitKey(0)

Output:

Red channel:

opencv rgb channel

Green channel:

Visualize the Different Color Channels of an RGB Image in Python using OpenCV

Blue channel:

Blue channel - RGB

We have successfully visualized different color channels of an RGB image using OpenCV in Python.

Must read: How to convert RGB image to a numpy array

Leave a Reply

Your email address will not be published.