OpenCV cvtColor

Salman Mehmood Oct 10, 2023
OpenCV cvtColor

This article aims to learn what the cvtColor() method does and how to use this method to convert the rgb image to an hsv image in OpenCV. We also discuss where the hsv image is useful and why we need to use it.

Use the cvtColor() Method to Create HSV Image in OpenCV

Here, we have added one image that shows the difference between the RGB and the HSV image. Whenever you consider any image in the RGB color, each pixel has three values representing the red, green and blue colors.

RGB and HSV image

Similarly, in the HSV color model image, each pixel is represented with the three values hue, saturation, and value. The hue represents the angle, the saturation represents the saturation of the color, and the value represents the intensity of the color.

So this way, the HSV color model works. When we convert any RGB image into the HSV image, each pixel value gets converted into the hue saturation and value format; we call this color model HSV.

Let’s see how to convert this RGB color model into the HSV color model image.

First of all, we are importing the packages opencv and numpy, and in the next line, we are trying to access our camera using VideoCapture() and passing zero, so it will access the primary camera on this system.

We store them in the V object, whatever video feeds we get.

import numpy as np
import cv2

V = cv2.VideoCapture(0)

Once we have captured the video from the camera, we will iterate through each frame inside of that video. Now we need to read each frame from the video capture, and this frame is nothing but the one image from our video that is the form of the numpy array.

RET, F = V.read()
cv2.imshow("BGR Frame", F)

This is the original frame, and we will convert the frame into HSV using the cvtColor() method. This method brings the different color models to the users, and among them, the most common method is COLOR_BGR2HSV.

We have to pass the two parameters in the cvtColor(); one is our original image, and the second is what kind of conversion we want to do on this image or a frame. We passed the COLOR_BGR2HSV method as a parameter, which means we are telling cvtColor() to convert this image color from BGR to HSV.

import numpy as np
import cv2

V = cv2.VideoCapture(0)

while True:
    RET, F = V.read()
    cv2.imshow("BGR Frame", F)
    HSV = cv2.cvtColor(F, cv2.COLOR_BGR2HSV)
    cv2.imshow("HSV Frame", HSV)
    if cv2.waitKey(1) == ord("q"):
        break
V.release()
cv2.destroyAllWindows()

We can see that our program is running, and on the left side, we are putting the original content that we are accessing from the camera in BGR format.

On the right side, we can see the hue saturation value image, and in this portion, the different color shades are included in the specific color range.

Use cvtColor Method to Create HSV Image in OpenCV

Let’s talk about why we need to convert this image into the HSV format and the benefit of converting the BGR image into the HSV format?

It is useful in any computer vision or machine learning project because each area is represented with other color shades. If you are just interested in the object which is marked with a specific color, so in that case, you can ignore the rest color area and extract the specific part of the color area.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Python OpenCV