9
votes

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.

3
Hello thank you for answer. But its also not working...sushma ahirwar
A better way to find is to make program that will print color of the black region you manually click using mouse. This way you can print a good range for what you can consider as Black. It is hard to tell a value as it can depend on lighting and other conditions which may be different for us. One more advise, if you are only interested in black color, try single gray-scaled image.Pervez Alam
See the colour wheel implementation using opencv here, where you can manually select the colour using mouse and corresponding hsv value. For black the value range may close to zero in HSV space, also it's recommended to use new C++ API instead deprecated C.Haris

3 Answers

22
votes

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);
5
votes

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);
2
votes

Hue is like the dominant light wavelength your eye receives. But black light wavelength is beyond visible light wavelength range. The hue doesn't count black light directly.

Value is the lightness/darkness value. Any hue can be regarded as black in a bad lighting condition.

Saturation is also referred to as "chroma". It depicts the signal intensity level of any hue. If S=0, any hue looks like "black" in color. On the contrary, if you want to segment true black color (rather than the "black" triggered by "darkness") from images, set a small saturation threshold is always the first job. Then combine with Hue and Value masks as the secondary mask will give you a more accurate answer.