And*_*rew 5 math image-processing centroid
所以我有一个二维数组代表一个坐标平面,一个图像.在该图像上,我正在寻找"红色"像素,并根据我的相机找到的所有红色像素找到(希望)红色LED目标的位置.目前,我只是将十字准线拍到所有红色像素的质心上:
// 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
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,虽然这个算法自然地使质心更接近最大的斑点(红色像素最多的区域),但是当一点红色闪烁到一边时,我仍然看到我的十字准线从目标上跳下来眩光或其他轻微干扰.
我的问题是:
如何更改此模式以查找更加重量的质心?简而言之,我想让较大的红色斑块比较小的斑点更重要,甚至可能完全忽略远处的小斑点.