OpenCV 质量中心点

OpenCV mass center point

我找到了一个不规则形状的质心,但现在我需要计算到任何给定点的距离。 我知道 mc 是一个点向量,但是我怎样才能找到 mc 的坐标,这样我就可以计算质心和其他一些 point.Thanks

之间的距离
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
    mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
}

首先你应该通过索引得到点。让 :

int size = contours.size();

索引是:i = 0 ... size。索引 i 处的点是

mc[i];

可以通过以下方式到达该点的坐标:

float xCoor = mc[i].x;
float yCoor = mc[i].y;

当然,如果您想读取所有 mc 点的所有坐标,您可以从 i = 0 to size 循环读取这些值。

编辑: 我假设你知道如何找到质心,只是问如何获得坐标。但是如果你想获得质心和质心到其他点的距离,那么你可以这样做:

float distance;
float totalX=0.0, totalY=0.0;    
for(int i=0; i<size; i++) {
    totalX+=mc[i].x;
    totalY+=mc[i].y;
}

Point2f massCenter(totalX/size, totalY/size); // condition: size != 0
Point2F someOtherPoint(someXVal, someYVal);

distance = massCenter.distance(someOtherPoint);

是质心到其他点的距离。

希望对您有所帮助!

mc[i].xmc[i].y 是索引 i 的点的 x 和 y 坐标。

计算质心:

cv::Point2f baricenter(0,0);
for( int i = 0; i < mc.size(); i++ )
    barycenter += mc[i];
barycenter.x /= mc.size();
barycenter.y /= mc.size();

检查你的向量中是否至少有一个点。