1

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

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

我已经从这里的算法中改编了这个,但是它给了我错误的结果,谁能告诉我这个改编有什么问题?

4

2 回答 2

6

你已经写centroid.x += (points[i].x*points[(i+1)%numPoints].x)*a; 了,应该是centroid.x += (points[i].x+points[(i+1)%numPoints].x)*a; 你必须在这里*替换+

于 2012-07-18T13:23:15.450 回答
1

我看了一点你拿这个样本的线程,我认为你应该有 centroid.x += (points[i].x + points[(i+1)%numPoints].x)*a; centroid.y += (points[i].y + points[(i+1)%numPoints].y)*a;

于 2012-07-18T13:24:48.237 回答