In this Python OpenCV article we want to learn about Color Spaces in Python OpenCV, so we already have talked that OpenCV is one of the popular Python library for image and video processing, in this tutorial we are going to talk about conversion of color spaces in opencv, specifically RGB, HSV, and YUV, and explore their applications using OpenCV.
So Color spaces are different ways of representing and organizing colors. they define the relationship between colors in an image and allows us to manipulate and analyze them effectively. in this section, we want to talk about three popular color spaces, RGB, HSV and YUV.
- RGB Color Space: RGB stands for Red, Green and Blue. It is the most common color space used in digital imaging systems. In RGB, each pixel is represented by three color channels, and the combination of these channels determines the final color. The values of each channel range from 0 to 255, and it represents the intensity of that color component.
- HSV Color Space: HSV stands for Hue, Saturation and Value. Unlike RGB, HSV separates color information from brightness information, and this makes it more easy to work with this color. Hue represents the color itself, saturation determines the intensity or purity of the color, and value represents the brightness of the color. The values of hue, saturation and value range from 0 to 179, 0 to 255 and 0 to 255.
- YUV Color Space: YUV represents the luminance (Y) and the chrominance components (U and V). Y channel represents the brightness information, while the U and V channels carry color information. YUV is commonly used in video encoding and compression algorithms as it efficiently separates luminance and chrominance components.
Now let’s create a practical example that how we can convert one color to another color using Python OpenCV.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import cv2 import matplotlib.pyplot as plt # Load the image image = cv2.imread('lena.tif') # Convert to RGB color space rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to HSV color space hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Convert to YUV color space yuv_image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) # Display the images using Matplotlib plt.figure(figsize=(12, 6)) plt.subplot(1, 4, 1) plt.imshow(image) plt.title('Original Image') plt.axis('off') plt.subplot(1, 4, 2) plt.imshow(rgb_image) plt.title('RGB Image') plt.axis('off') plt.subplot(1, 4, 3) plt.imshow(hsv_image) plt.title('HSV Image') plt.axis('off') plt.subplot(1, 4, 4) plt.imshow(yuv_image) plt.title('YUV Image') plt.axis('off') plt.tight_layout() plt.show() |
In the above example, we have imported matplotlib.pyplot module as plt. after converting the image to different color spaces, we create a figure with a size of 12×6 inches using plt.figure(figsize=(12, 6)).
After that we use the plt.subplot() function to create subplots inside the figure. each subplot represents an image, and we specify the subplot layout using the arguments.
For each subplot, we use plt.imshow() to display the corresponding image. after we set the title of each subplot using plt.title() and turn off the axis labels.
And lastly we use plt.tight_layout() to improve the spacing between subplots and plt.show() to display the plot with the images.
Run the code and this will be the result

Learn More on Python OpenCV
- Object Tracking with Python and OpenCV
- How to Scale Image with Python and OpenCV
- How to Rotate an Image with OpenCV and Python
- How to Load an Image in OpenCV
- How to Detect Low Contrast Images with OpenCV and scikit-image
- Get Started with Python OpenCV