OpenCV Python Color Spaces Introduction

In this OpenCV Python article we are going to talk about Color Spaces Introduction.  so first of all let’s talk about different types of color spaces in OpenCV.

 

 

What are OpenCV Color Spaces?

In OpenCV Color spaces represent colors in different ways, There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two which are most widely used ones, BGR ↔ Gray and BGR ↔ HSV.

 

 

What is BGR Color?

BGR color is a color representation commonly used in digital imaging, particularly in computer vision and image processing. BGR stands for Blue-Green-Red, which refers to the order of color channels used to represent an image in memory.

 

 

What is RGB Color?

RGB color is a color model used in digital imaging, computer graphics and displays. RGB stands for Red, Green, and Blue, which are the primary colors of light. In the RGB color model, colors are created by different intensities of these three primary colors.

 

 

What is HSV Color?

HSV color, also known as HSB (Hue, Saturation, Brightness), is a color model that represents colors based on three components: hue, saturation, and value (or brightness). The HSV color model is often used in graphics software, image processing and color selection tools because it provides an easy way to specify and manipulate colors.

 

 

 

How to Convert Color Spaces in Python OpenCV?

For color conversion, we use the function cv.cvtColor(input_image, flag) where flag determines the type of conversion.

For BGR → Gray conversion we use the flags cv.COLOR_BGR2GRAY. Similarly for BGR → HSV, we use the flag cv.COLOR_BGR2HSV. To get other flags, just run following commands in your Python terminal :

 

 

 

This is the complete source code for this article

Let’ describe the code:

 

Importing Libraries:

  • We import the necessary libraries: cv2 for image processing and matplotlib.pyplot for displaying images.

 

Loading the Image:

  • We load an image named “lena.tif” using cv2.imread() and store it in the variable img1.

 

Converting to Different Color Spaces:

  • We convert the loaded image (img1) to different color spaces:
      • bgrgray: Convert from BGR (Blue-Green-Red) to Grayscale using cv2.cvtColor() with cv2.COLOR_BGR2GRAY.
      • bgrhsv: Convert from BGR to HSV (Hue-Saturation-Value) using cv2.cvtColor() with cv2.COLOR_BGR2HSV.
      • brightLAB: Convert from BGR to LAB (CIE L*a*b*) using cv2.cvtColor() with cv2.COLOR_BGR2LAB.

 

Displaying Images Using Matplotlib:

  • We create a new figure using plt.figure(figsize=(10, 8)) with a specified size.

 

Showing the Plot:

  • We call plt.show() to display the plot with all subplots.

 

 

 

 

 

 

Run the complete code and this will be the result

OpenCV Python Color Spaces Introduction
OpenCV Python Color Spaces Introduction

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×