# How to determine range of HSV values of the image?

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:

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:

-

## migrated from stackoverflow.comNov 6 '12 at 13:39

This question came from our site for professional and enthusiast programmers.

can you convert the color space of that rectangle into HSV and take a value from the object in HSV domain? –  james Nov 4 '12 at 23:13
@james I edited my question. I can convert my image to hsv. Problem is which h-s-v ranges will i choose. –  gurcan15 Nov 5 '12 at 20:20
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. –  james Nov 5 '12 at 20:46
@james I edited it again. –  gurcan15 Nov 5 '12 at 20:59
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. –  james Nov 5 '12 at 23:35

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.

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 : Selecting best range of values from histogram curve

-
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. –  gurcan15 Nov 5 '12 at 20:04