How to adjust reverse range for Hue in cvInRangeS? cvInRangeS method for HSV colorspace is used like:

cvInRangeS(imgHSV, cvScalar( hMin,sMin,vMin),cvScalar(hMax,sMax, vMax),imgThresh);


For hue value we generally use a range between two numbers, a pair of small and big one like $(10-50)$, which means $10,11,12...48,49,50$ .

Sometimes I need to use a reverse range for red colors. By "reverse range", I mean a range between a big and a small number like $(170-10)$ which loops at the value of $180$: $170,171,172 ... 179,180,0,1,2,3 ... 8,9,10$).

How do I adjust this reverse range to cvInRangeS method? Can anyone explain how to make this work?
You can generate two different threshold images, one with hue in the range from 0 to 10 and the other with hue from 170 to 180. To get an image with hue in range both from 0 to 10 and 170 to 180, just add the two threshold images together.

In C++ code this would look something like:

inRange(imgHSV, Scalar(0, sMin, vMin), Scalar(10, sMax, vMax), imgThresh);
inRange(imgHSV, Scalar(170, sMin, vMin), Scalar(180, sMax, vMax), imgThresh1);

Mat imgThreshCombined = imgThresh + imgThresh1;


You could probably do the same thing by finding the threshold image with hue in the range from 10 to 170 and then just subtracting it from a white image.