OpenCV质量中心点

Fel*_*ipe 2 c++ opencv measurement image-processing

我找到了不规则形状的质心,但现在我需要计算到任何给定点的距离.我知道mc是一个点矢量,但我怎样才能找到mc的坐标,这样我才能计算出质心与其他点之间的距离.谢谢

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 );
}
Run Code Online (Sandbox Code Playgroud)

ero*_*ras 6

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

int size = contours.size();
Run Code Online (Sandbox Code Playgroud)

指数是:i = 0 ... size.索引点i

mc[i];
Run Code Online (Sandbox Code Playgroud)

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

float xCoor = mc[i].x;
float yCoor = mc[i].y;
Run Code Online (Sandbox Code Playgroud)

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

编辑: 我认为你知道如何找到质量中心,并且只是询问如何获得坐标.但是如果你想获得质量中心以及从中心到其他点的距离那么你可以做到以下几点:

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);
Run Code Online (Sandbox Code Playgroud)

是从质量中心到其他点的距离.

希望有所帮助!