HSV colour space and CvInRangeS function

user992520 picture user992520 · Jan 31, 2012 · Viewed 22.6k times · Source
cvInRangeS(imgHSV, cvScalar(15, 234, 120), cvScalar(21, 234, 120), imgThreshed);

I am wondering what each parameter in the cvScalar function represents. I thought it would be HSV but it doesnt seem to be thresholding the desired color. Could someone explain the parameters a bit clearer?

Answer

Abid Rahman K picture Abid Rahman K · Jan 31, 2012

cvInrangeS function checks that array elements lie between two scalars.

First cvScalar value denotes inclusive lower boundary. Second cvScalar value denotes exclusive upper boundary.

cvInrangeS doesn't care the values you give as either RGB or BGR or HSV etc. It just goes through the array elements you give as input (first parameter)(here it is an image) and checks if elements are in the given range you specified. If so, it is selected, otherwise rejected. And feed result to last parameter, your output image.

Check out its documentation

Now here if you give a HSV image. So cvScalar denotes lowest and highest color of range you want to extract.

If it is RGB image, you specify min and max RGB colors.

And better use HSV because it provides good result. Read here.

And now if you want to convert RGB to HSV values, Try here and here.

(Remember, in OpenCV red and blue interchanged. It is BGR, not RGB).