Serengeti logo BLACK white bg w slogan

Python Book of Recipes #5 -A Delectable Palette: Exploring the Flavors of Color Spaces in Computer Vision

Boško Savić, Senior Software Developer

Color is a fundamental aspect of our visual perception, and understanding color is crucial in various computer vision tasks.

Color spaces serve a vital role in computer vision tasks such as image processing, object detection, segmentation and tracking. By representing colors in a specific color space, we can extract meaningful features, identify patterns and perform various operations to analyze and understand visual data.

In this recipe, we will walk through the process of building a color space converter using Python, OpenCV and Streamlit. We will leverage the capabilities of OpenCV, a popular computer vision library, to handle image operations and color space conversions. Streamlit, a user-friendly library for creating interactive web applications, will enable us to create a visually appealing and intuitive interface for our color space converter.


  • Factory pattern
  • OpenCV (to handle image processing)
  • NumPy (for array operations)
  • Streamlit (to build an interactive application)

STEP 1: Preparing Workspace

  • Set up the necessary Python environment with OpenCV, NumPy and Streamlit.

STEP 2: Cooking the Color Space Converter

In our color space converter code, we can observe the application of the Factory Pattern.

We have a base class called ColorSpaceConverter, which defines the common interface for all color space converters. This base class acts as our factory, and the individual color space converter classes (e.g., RGBConverter, HSVConverter, etc.) are inherited from it.

Each converter class will have a convert() method that performs the specific color space conversion using OpenCV functions.

By applying the Factory Pattern, we ensure a clean and modular design for our color space converter code, promoting code reusability, maintainability and scalability.

image 11

STEP 3: Assembling the Colorful Dish

Build the Streamlit application that will display the converted images and their color space components in separate sections using the st.image() function.

Run the streamlit and enjoy exploring the visualization of color space components.

The full code can be found on the link.

In our experiment, I will utilize Serengeti images - one in its natural form, and another with darker tones - to explore and compare the nuances of various color spaces.

image 12

RGB (Red, Green, Blue)

RGB is the most widely used color space, representing colors as combinations of the primary colors, red, green and blue. It is an additive color model where different intensities of these three primary colors create a wide range of colors.

image 13


  • Widely supported and used in various applications
  • Intuitive representation of color based on primary colors.
  • Straightforward conversion to other color spaces.


  • Color information is not decoupled from intensity information, making it less suitable for some image processing tasks.
  • Not perceptually uniform, which can affect certain color-based algorithms.

HSV (Hue, Saturation, Value)

HSV separates the color information into three components: hue, saturation and value. Hue represents the dominant color information, saturation determines the intensity or purity of the color, and value represents the brightness or darkness of the color. HSV is useful for tasks such as color segmentation or color-based object tracking.

image 14


  • Separates color information from brightness information
  • Intuitive representation of color hue and saturation.
  • Useful for color-based image segmentation and tracking tasks.


  • Limited in capturing subtle color variations due to fixed saturation and value ranges.
  • Hue can be sensitive to lighting variations.


Lab is a device-independent color space that aims to represent colors in a perceptually uniform manner. It consists of three components: L* for lightness, a* for the color along the green-red axis, and b* for the color along the blue-yellow axis. Lab is often used in color-based image analysis and computer vision algorithms.

image 15


  • Perceptually uniform color space
  • Provides a more consistent representation of color perception.
  • Useful for color-based analysis, color differences and color matching tasks.


  • More computationally intensive to calculate and convert compared to other color spaces.
  • Can have larger storage requirements for each pixel compared to RGB or other color spaces.

YUV (Luma, Chrominance)

YUV is a color space that separates color information into luminance (Y) and chrominance (U and V) components. The Y channel represents the grayscale or brightness information, while U and V channels contain the color information. YUV is commonly used in video compression algorithms, such as MPEG.

image 16


  • Separates brightness and color information
  • Enables more efficient compression algorithms for video.
  • Useful for video processing tasks, including compression, noise reduction and edge detection.


  • Limited in representing color information accurately, particularly in terms of color saturation.
  • YUV representation may vary depending on the specific implementation or video standard.

YCrCb (Luma, Chrominance Red, Chrominance Blue)

YCrCb is a color space widely used in digital image and video processing. It separates the color information into three components: luma (Y), chrominance red (Cr) and chrominance blue (Cb). The Y channel represents grayscale or brightness information, while the Cr and Cb channels carry color information.

image 17


  • Compression Efficiency. YCrCb color space is highly efficient for video and image compression algorithms.
  • Skin Tone Detection. YCrCb color space is particularly useful in computer vision applications that involve the skin tone detection or tracking.


  • Limited in representing color information accurately, particularly in terms of color saturation.
  • Color Shifts: Converting images or videos between RGB and YCrCb color spaces may introduce color shifts or artifacts.


It's important to note that the suitability of a color space depends on the specific task and the characteristics of the images being processed. Different color spaces have their advantages and disadvantages, and the choice of color space should align with the requirements and constraints of the application at hand.

Let's do business

The project was co-financed by the European Union from the European Regional Development Fund. The content of the site is the sole responsibility of Serengeti ltd.