查找图像等高线质心

时间:2012-07-18 13:06:18

标签: c++ c image-processing centroid

我正在尝试像这样计算图像质心:

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

我已经从算法here中对此进行了调整,但它给了我错误的results,有人能告诉我这次改编有什么问题吗?

2 个答案:

答案 0 :(得分:6)

你写过centroid.x += (points[i].x*points[(i+1)%numPoints].x)*a; 相反它应该是centroid.x += (points[i].x+points[(i+1)%numPoints].x)*a; 您必须在此处将*替换为+

答案 1 :(得分:1)

我看了一下你采取这个样本的主题,我想你应该有 centroid.x + =(points [i] .x + points [(i + 1)%numPoints] .x)* a; centroid.y + =(points [i] .y + points [(i + 1)%numPoints] .y)* a;