简体   繁体   中英

Selecting best range of values from histogram curve

Scenario :

I am trying to track two different colored objects. At the beginning, user is prompted to hold the first colored object (say, may be a RED) at a particular position in front of camera (marked on screen by a rectangle) and press any key, then my program takes that portion of frame (ROI) and analyze the color in it, to find what color to track. Similarly for second object also. Then as usual, use cv.inRange function in HSV color plane and track the object.

What is done :

I took the ROI of object to be tracked, converted it to HSV and checked the Hue histogram. I got two cases as below :

在此输入图像描述在此输入图像描述

( here there is only one major central peak. But in some cases, I get two such peaks, One a bigger peak with some pixel cluster around it, and second peak, smaller than first one, but significant size with small cluster around it also. I don't have an sample image of it now. But it almost look like below (created in paint))

在此输入图像描述

Question :

How can I get best range of hue values from these histograms?

By best range I mean, may be around 80-90% of the pixels in ROI lie in that range.

Or is there any better method than this to track different colored objects ?

If I understand right, the only thing you need here is to find a maximum in a graph, where the maximum is not necessarily the highest peak, but the area with largest density.

Here's a very simple not too scientific but fast O(n) approach. Run the histogram trough a low pass filter. Eg a moving average. The length of your average can be let's say 20. In that case the 10th value of your new modified histogram would be:

mh10 = (h1 + h2 + ... + h20) / 20

where h1, h2... are values from your histogram. The next value:

mh11 = (h2 + h3 + ... + h21) / 20

which can be calculated much easier using the previously calculated mh10, by dropping it's first component and adding a new one to the end:

mh11 = mh10 - h1/20 + h21/20

Your only problem is how you handle numbers at the edge of your histogram. You could shrink your moving average's length to the length available, or you could add values before and after what you already have. But either way, you couldn't handle peaks right at the edge.

And finally, when you have this modified histogram, just get the maximum. This works, because now every value in your histogram contains not only himself but it's neighbors as well.

A more sophisticated approach is to weight your average for example with a Gaussian curve. But that's not linear any more. It would be O(k*n), where k is the length of your average which is also the length of the Gaussian.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM