从直方图曲线中选择最佳值范围 [英] Selecting best range of values from histogram curve

查看:143
本文介绍了从直方图曲线中选择最佳值范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情景:

我正在尝试跟踪两个不同颜色的对象。开始时,提示用户将第一个彩色对象(例如,可能是红色)保持在相机前面的特定位置(在屏幕上用矩形标记)并按任意键,然后我的程序获取该部分帧(ROI)并分析其中的颜色,找到要跟踪的颜色。同样对于第二个对象也是。然后像往常一样,在HSV颜色平面中使用 cv.inRange 函数并跟踪对象。

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.

什么已完成:

我获取了要跟踪的对象的ROI,将其转换为HSV并检查了Hue直方图。我有两个案例如下:

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))

问题:

如何从这些直方图中获得最佳的色调值范围?

按最佳范围我的意思是,ROI中大约80-90%的像素位于该范围内。

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.

这是一个非常简单的不太科学但快速的O(n)方法。通过低通滤波器运行直方图。例如。移动平均线。您的平均值的长度可以说是20.在这种情况下,新修改的直方图的第10个值将是:

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

其中h1,h2 ......是直方图中的值。下一个值:

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

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

使用先前计算的mh10可以更容易地计算,删除它的第一个组件并添加一个新组件到最后:

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.

更复杂的方法是对平均值进行加权,例如使用高斯曲线。但那不再是线性的。它将是O(k * n),其中k是平均值的长度,也是高斯的长度。

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.

这篇关于从直方图曲线中选择最佳值范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆