数组的加权质心 [英] Weighted Centroid of an Array
问题描述
所以我有一个二维数组代表一个坐标平面,一个图像。在该图像上,我正在寻找红色像素,并根据我的相机找到的所有红色像素找到(希望)红色LED目标的位置。目前,我只是将十字准线拍到所有红色像素的质心上:
//伪代码
for(cycle_through_pixels)
{
if(is_red(pixel [x] [y]))
{
vals ++; //红色像素总数
cx + = x; //求x的
cy + = y; //求y的
}
}
cx / = vals; //除以总计得到平均值x
cy / = vals; //除以总计得到平均值y
draw_crosshairs_at(pixel [cx] [cy]); //找到了质心
这种方法的问题在于,这种算法自然会使质心更接近最大的斑点(红色像素最多的区域),当有一些红色由于眩光或其他轻微干扰而闪烁时,我仍然看到我的十字准线跳离目标。
我的问题是:
如何更改此模式以寻找更多加权质心?简而言之,我想让较大的红色斑点比较小的斑点更重要,甚至可能完全忽略远处的小斑点。
您可以在图像中找到连接组件,并且只包含这些组件在您的质心计算中总大小超过某个阈值。
So I have a 2-dimensional array representing a coordinate plane, an image. On that image, I am looking for "red" pixels and finding (hopefully) the location of a red LED target based on all of the red pixels found by my camera. Currently, I'm simply slapping my crosshairs onto the centroid of all of the red pixels:
// pseudo-code
for(cycle_through_pixels)
{
if( is_red(pixel[x][y]) )
{
vals++; // total number of red pixels
cx+=x; // sum the x's
cy+=y; // sum the y's
}
}
cx/=vals; // divide by total to get average x
cy/=vals; // divide by total to get average y
draw_crosshairs_at(pixel[cx][cy]); // found the centroid
The problem with this method is that while this algorithm naturally places the centroid closer to the largest blob (the area with the most red pixels), I am still seeing my crosshairs jump off the target when a bit of red flickers off to the side due to glare or other minor interferences.
My question is this:
How do I change this pattern to look for a more weighted centroid? Put simply, I want to make the larger blobs of red much more important than the smaller ones, possibly even ignoring far-out small blobs altogether.
You could find the connected components in the image and only include those components that have a total size above a certain threshold in your centroid calcuation.
这篇关于数组的加权质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!