Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
Thank you, i have just used this method. It works perfect, but i'm worried because i think using double inRange methods per frame effects my program's efficiency in bad way. Because of this, I am looking for new solutions. – gurcankavakci Nov 28 '12 at 14:05
Did you try the other method where you threshold the image with hMin = 10 and hMax = 170 and then subtract it from a white image? It might be faster, I'm not sure. – ppalasek Nov 28 '12 at 14:15
@gurcan15 Did you measure the slow-down? I wouldn't imagine it being so slow as you say. Threshold with (10, 170) and then subtraction should be the fastest you can get with built-in inRange method. One image pass for inRange and one for subtraction (vs. 2 for inRange and 1 for addition). The only faster alternative I can think of is to implement your own version of inRange or inRangeHSV which will loop ranges on it's own. – penelope Nov 28 '12 at 14:40
@penelope I didn't measure the slow-down, I just think, it should be slower. Subtraction is alternative, I will try it. And I think it will be faster like you said. – gurcankavakci Nov 28 '12 at 14:46
@gurcan15 I think the slowest part of this approach is traversing the image pixels. For a normal (10-70) range you just do it once. Proposed way does it trice, while the way with the subtraction would traverse the image twice. BUT, if going from trice to twice offers a substantial speedup (for your needs), you should seriously consider making your own implementation: the speedup from twice to once should be substantial if that's the case. – penelope Nov 28 '12 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.