Understanding Color Models using OpenCV-Python

In this blog, we will see how to convert images from one color model to another and how different colors can be obtained from these color models.

In OpenCV, the command for converting an image to another color-space is

cv2.cvtColor(input_image, conversion_method) 

for example, BGR to HSV conversion can be done by using cv2.COLOR_BGR2HSV method

In OpenCV, more than 150 color-space conversion methods are available. To get the other conversion methods, type the following commands

In the previous blog, we learned how we can construct all colors from each model. Now, let’s get the feeling of this with OpenCV.

Here, I will create three trackbars to specify each of B, G, R colors and a window which shows the color obtained by combining different proportions of B, G, R. Similarly for HSI and CMYK models.

In OpenCV, Trackbar can be created using the cv2.createTrackbar() and its position at any moment can be found using cv2.getTrackbarPos(). 

RGB Trackbar

You can move these trackbars to obtain different colors. A snapshot of output is shown below

HSI Trackbar

We get the following output as

Similarly, you can create trackbar for any color model. Play with these trackbars to get intuition about color models. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Leave a Reply