Finding an Images Contours Centroid

此生再无相见时 提交于 2019-12-11 12:47:49

问题


I am trying to calculate an image centroid like so:

// Calculate centroid
    double signedArea = 0.0;
    sskp_point centroid;
    for(int i=0;i<numPoints;i++)
    {
        double a = (points[i].x*points[(i+1)%numPoints].y)-(points[(i+1)%numPoints].x*points[i].y);
        signedArea += a;
        centroid.x += (points[i].x*points[(i+1)%numPoints].x)*a;
        centroid.y += (points[i].y*points[(i+1)%numPoints].y)*a;
        printf("points[%d] = { %f, %f }\n",i,points[i].x,points[i].y);
    }
    signedArea /= 2.0;
    centroid.x /= (6*signedArea);
    centroid.y /= (6*signedArea);
    printf("centroid = { %f, %f }\n",centroid.x,centroid.y);

I have adapted this from the algorithm here, however it is giving me the wrong results, can anyone tell me what is wrong with this adaptation?


回答1:


You have written centroid.x += (points[i].x*points[(i+1)%numPoints].x)*a; instead it should be centroid.x += (points[i].x+points[(i+1)%numPoints].x)*a; you have to replace * with + here.




回答2:


I looked a little into the thread you took this sample and i think that you should have centroid.x += (points[i].x + points[(i+1)%numPoints].x)*a; centroid.y += (points[i].y + points[(i+1)%numPoints].y)*a;



来源:https://stackoverflow.com/questions/11542121/finding-an-images-contours-centroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!