How to arrive at HSV and RGB range of an Object for Computer Vision applications?16 Aug 2018
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) or RGB (Red, Green, and Blue) 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 Tennis ball is about to be detected and tracked when it is moved in front of a webcam. To identify the ball alone, not any other objects/ noises, it is necessary to specify a correct range of corresponding HSV numbers.
Refer to the article What is the HSV Color Model? to know more about HSV. Here is the quick look at what HSV is.
The exact HSV or RGB range can be determined programmatically using OpenCV for an object to be identified or tracked. In the below clip, a Tennis ball, which needs to be detected and tracked, is used to determine its HSV range.
Grab the python code ColorPicker.py from my GIT repository, copy the same to a local machine, and issue one of the below commands in Command Line Interface (CLI), based on your requirement. The source code is taken from Adrian Rosebrock’s repository.
To determine HSV range based on an image,
python ColorPicker.py --filter HSV --image /path/image.png
To determine RGB range based on an image,
python ColorPicker.py --filter RGB --image /path/image.png
To determine HSV range based on webcam video,
python ColorPicker.py --filter HSV --webcam
To determine RGB range based on webcam video,
python ColorPicker.py --filter RGB --webcam
This script launches three windows as shown in the above clip:
- Original: shows original video/image
- Trackbars: shows sliders to adjust the HSV/RGB Min and Max range
- Thresh: shows video/image adjusted to the selected HSV/RGB range
Adjust the Min and Max slide bars in Trackbars window till you get the desired object alone appear in White in Thresh window. Take the corresponding HSV/RGB range and use them in the code as indicated earlier.