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 ?
To determine the range in a histogram, observe the highlighted blue line. The range in a histogram is determined by the width that the bar cover along the x-axis. The range in a histogram is only the approximate value since the data in a histogram is not the raw data values.
In a histogram, the range is the width that the bars cover along the x-axis. These are approximate values because histograms display bin values rather than raw data values. In these histograms, distribution A has an approximate range of 65 – 40 = 25 and for distribution C it is 90 – 20 = 70.
Parts of a Histogram Y-axis: The Y-axis shows the number of times that the values occurred within the intervals set by the X-axis. The bars: The height of the bar shows the number of times that the values occurred within the interval, while the width of the bar shows the interval that is covered.
Answer: The Answer is Mode. Because The highest peak of the histogram represents the location of the mode of the data set. The mode is the data value that occurs the most often in a data set.
The width of the bins should be equal, and you should only use round values like 1, 2, 5, 10, 20, 25, 50, 100, and so on to make it easier for the viewer to interpret the data. These histograms were created from the same example dataset that contains 550 values between 12 and 69. Ideal: This one is good.
In this case, the normal curve has the same height as the histogram instead of the same area. All the calculations are the same as for the Match area option, except that the adjustment value in cell P11 of Figure 7 is now calculated by the formula =MAX (I9:I11)/MAX (S6:S106).
Let us create our own histogram. Download the corresponding Excel template file for this example. Step 1: Open the Data Analysis box. This can be found under the Data tab as Data Analysis: Step 3: Enter the relevant input range and bin range. In this example, the ranges should be: Make sure that “Chart Output” is checked and click “OK”.
What is a histogram? A histogram is a chart that plots the distribution of a numeric variable’s values as a series of bars. Each bar typically covers a range of numeric values called a bin or class; a bar’s height indicates the frequency of data points with a value within the corresponding bin.
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. E.g. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With