Is there any method to find range of hsv values of image (object) in rectangle inside big image? I mean, there is fixed size rectangle on my image(frame from camera) and i want to find range of hsv values of object which is inside this rectangle. Object is changeable. If i put red ball inside rectangle it should find range of red ball's hsv values, if i put my hand inside rectangle it should find range of my skin's hsv values. Is there any way to do this?

Edit: I get part of image with ROI, and convert it to hsv format. But problem is behind red ball, I get wide variety of colours. So which range will I choose from this image?

Example: my frame: enter image description here

Now I want to find best range of hsv values of image in rectangle box for tracking it. (Now it should be green and ranges should be h:53-90 s:74-147 v: 160-255)

If i can find this values, by using inRange method, i can convert it to binary image for tracking. Like this: enter image description here

  • $\begingroup$ can you convert the color space of that rectangle into HSV and take a value from the object in HSV domain? $\endgroup$
    – james
    Nov 4, 2012 at 23:13
  • $\begingroup$ @james I edited my question. I can convert my image to hsv. Problem is which h-s-v ranges will i choose. $\endgroup$
    – gurcan15
    Nov 5, 2012 at 20:20
  • $\begingroup$ the problem is the recognition/location of your object then. If it is just a red ball, it is probably easy to get a solution, but you said the object can be anything which makes this question intractable (I didn't downgrade your question, though). you must at least give some reasonable range for your question. Is the object solid and in the same color? do u just need any point inside the object? we want to know how u define the object. $\endgroup$
    – james
    Nov 5, 2012 at 20:46
  • $\begingroup$ @james I edited it again. $\endgroup$
    – gurcan15
    Nov 5, 2012 at 20:59
  • 1
    $\begingroup$ I agree with Abid that u need to establish a histogram and rank the colors according to their occurrence. the first few colors in the list can give u an ok range. $\endgroup$
    – james
    Nov 5, 2012 at 23:35

1 Answer 1


You can simply specify a ROI for that region and convert it into HSV.

For eg (below is pseudo-code in Python-OpenCV)

# define ROI of RGB image 'img'
roi = img[r1:r2, c1:c2]

# convert it into HSV
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)

Now it gives you the hsv values of the region.

But one or two difficulties there:

  1. Your object may comprise some part of the ROI. There may be some background region also inside your ROI. ie if your object is red and background is blue, your ROI will contain two different values, one for RED and other for BLUE. Which one will your program select?
  2. But colors won't be constant like RED or BLUE. You get wide variety of colours. Below is a plot of different colours for a ROI with green object.

enter image description here

But it has values from yellow to slight blue with green as major.

So you will have to select color with maximum probability, may be from checking its histogram.

Check this SOF for more details : https://stackoverflow.com/questions/13111504/selecting-best-range-of-values-from-histogram-curve

  • $\begingroup$ Abid Rahman K, you are right, your difficulties are my major problems. I specified ROI and got hsv format of image. Problem is which range will I choose from this image. There is red ball in rectangle but behind red ball, I get wide variety of colours. Your histogram solution may help me. I will examine it. $\endgroup$
    – gurcan15
    Nov 5, 2012 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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