How to find HSV range of an Object for Computer Vision applications?

Praveen
programming_fever
Published in
3 min readJul 28, 2020

HSV color space is more often used in computer vision owing to its superior performance compared to RGB color space in varying illumination levels. Often thresholding and masking is done in HSV color space. So it is very important to know the HSV values of the color which we want to filter out. HSV color space of OpenCV is a bit complicated than other software programmes like Gimp, Photoshop etc.So I have written a small python script to grab frames from a camera, print the HSV value and save(press ‘s’ ) our range array as a .npy file in our disk so we can access it later.

If a Computer Vision (CV) related application deals with detecting or tracking a specific object, then it is necessary to determine the range of HSV (Hue, Saturation, and Value) values of that object. This range is required to be specified as part of the coding to detect that object. If the correct range is not specified, the CV algorithm may pick-up noises as well, besides the actual object, leading to false detection and tracking.

In the below OpenCV code snippet, a sketch pen(pink colour) is about to be detected and tracked when it is moved in front of a webcam. To identify the sketch pen(pink colour)alone, not any other objects/ noises, it is necessary to specify a correct range of corresponding HSV numbers.

Refer to the Wikipedia What is the HSV Color Model? to know more about HSV. Here is the quick look at what HSV is.

HSV (hue, saturation, value) or HSB (hue, saturation, brightness) are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes. In these models, colors of each hue are arranged in a radial slice, around a central axis of neutral colors which ranges from black at the bottom to white at the top.

The exact HSV range can be determined programmatically using OpenCV for an object to be identified or tracked. In the below video, a sketch pen(pink colour) which needs to be detected and tracked, is used to determine its HSV range.

The Python script launches window as shown in the above video which shows sliders to adjust the HSV Min and Max range.Adjust the Min and Max slide bars in Track bars window till you get the desired object alone appear in White in Thresh window. Take the corresponding HSV range and use them in your code .We will also save(press ‘s’ ) our range array as a .npy file in our disk so we can access it later.

This below script will let you use track bars to adjust the hue, saturation, and value channels of the image. Adjust the track bars until only your target object is visible and the rest is black.

Full Code

other related projects

you can find me elsewhere on

Instagram @programming_fever

Facebook @GeekyPRAVE

Twitter @GeekyPRAVEE

--

--