Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Black color object detection HSV range in opencv

Tags:

c++

opencv

hsv

What is the range of Black color object detection?

i tried following code

cvInRangeS(imgHSV, cvScalar(0, 0, 0, 0), cvScalar(0, 255, 255, 0), imgThreshold);

but its not working.

like image 796
sushma ahirwar Avatar asked Aug 20 '14 06:08

sushma ahirwar


2 Answers

For black and white colors in HSV range you have to set hue at maximum range (0 to 180), and saturation at maximum range (0 to 255). You can play with the value, for example, 0 to 30 or 40 for black, and 200 to 255 for white.

// for black
cvInRangeS(imgHSV, cvScalar(0, 0, 0, 0), cvScalar(180, 255, 30, 0), imgThreshold);

// for white
cvInRangeS(imgHSV, cvScalar(0, 0, 200, 0), cvScalar(180, 255, 255, 0), imgThreshold);

Or you can use the C++ interface:

// for black
cv::inRange(imgHSV, cv::Scalar(0, 0, 0, 0), cv::Scalar(180, 255, 30, 0), imgThreshold);

// for white   
cv::inRange(imgHSV, cv::Scalar(0, 0, 200, 0), cv::Scalar(180, 255, 255, 0), imgThreshold);
like image 163
Elvis Dukaj Avatar answered Oct 06 '22 00:10

Elvis Dukaj


Black colour in HSV and HSL colour space, is detected with low Value (or Lightness in HSL).

White colour in HSL detected with high Value. White colour is HSV detected with high Lightness and Low Saturation.

for white

cv::inRange(imgHSL, cv::Scalar(0, 0, 200, 0), cv::Scalar(180, 255, 255, 0), imgThreshold);

or

cv::inRange(imgHSV, cv::Scalar(0, 0, 200, 0), cv::Scalar(180, 20, 255, 0), imgThreshold);
like image 42
robbycandra Avatar answered Oct 05 '22 22:10

robbycandra