Python OpenCV Color Spaces

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.


  1. 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.
  2. 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.
  3. 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.

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 to display the plot with the images.





Run the code and this will be the result

Python OpenCV Color Spaces
Python OpenCV Color Spaces




Learn More on Python OpenCV


Leave a Comment