2012-07-18 50 views
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); 

我從算法here適應這一點,但它給我的錯誤results,誰能告訴我是什麼這適應錯了嗎?

回答

6

你已經寫centroid.x += (points[i].x*points[(i+1)%numPoints].x)*a; 相反,它應該是centroid.x += (points[i].x+points[(i+1)%numPoints].x)*a; 你有+我更換*

+1

我真是個傻瓜,謝謝。 – user293895 2012-07-18 13:24:13

+1

它發生了。人非聖賢孰能。 :d – 2012-07-18 13:25:15

1

我看了一下,你拿這個樣本的線程,我認爲你應該有 centroid.x + =(points [i] .x + points [(i + 1)%numPoints] .x)*一個; centroid.y + =(points [i] .y + points [(i + 1)%numPoints] .y)* a;