Color spaces in OpenCV in Python

In this post, we are going to cover an important topic of image processing that is, Color spaces in OpenCV in Python. The color space of a picture gives us a specific hue that represents using color spaces. There are multiple color spaces, and each one has a unique meaning. RGB (Red, Green, Blue), CMYK (Cyan, Magenta, Yellow, Black), HSV (Hue, Saturation, Value), and others are a few of the widely in-use color spaces. OpenCV has a  built-in color space by default RGB. Various Blue, Green, and Red intensities result in various shades of color in this additive color model. An additive color model means the addition of all the colors will result in white color. The CMYK is the subtractive color model which means its additive gives black color.

RGB three circle

In the HSV color space meaning Hue, Saturation, and Value. It makes an effort to capture colors as they are seen by the human eye. The value ranges from 0-255, Saturation ranges from 0-255, and Hue ranges from 0-179. It mostly serves the goal of color segmentation.

 Visualizing the different color channels of an RGB image using OpenCV

#importing cv2 
import cv2

#cv2 reads the image using imread method
image_rgb = cv2.imread('rgb.png')
B, G, R = cv2.split(image_rgb)

#cv2  shows the image using imshow method
# we have done the same in the given 4 args
#we have tried to visualize the color scheme
#"Blue " shows blue color and similarly for greena and red.

cv2.imshow("original", image_rgb)

cv2.imshow("Blue", B)

cv2.imshow("Green", G)

cv2.imshow("Red", R)
#destroyall windows results in close all windows 
at any time after exiting the script


rgb original

rgb original

Color spaces in OpenCV in Python


Visualizing the different color channels of an RGB image using OpenCV


Visualizing the different color channels of an RGB image


With this, we have come to the end of this tutorial. Learn, Image Segmentation in Python Using Color Spaces.

Leave a Reply

Your email address will not be published. Required fields are marked *