我进行了轮廓点的平移(大约300-1000点),即每个点我改变了它的X和Y坐标,用这个公式将矢量XOY的中心移动到重心:
vector<vector<Point> > contours_poly( contours.size() );
vector<vector<Point> > contours( contours.size() );
for(unsigned int i=0;i<contours.size();i++)
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 2, true );
convexHull( Mat(contours[i]), hull[i], false );
}
/// centre de gravité
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ ){
mu[i] = moments( contours[i], false );
}
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 );
}
/// Translation* the program bug here !!
for(i=0; i < contours.size(); i++)
{ contours_poly[1][i].x -= mc[1].x;
contours_poly[1][i].y -= mc[1].y;
}
/// draw the new contour
for(unsigned int i=0;i<contours.size();i++)
{
drawContours( drawing, contours_poly, i, colors, 1, 8, vector<Vec4i>(), 0, Point() );
}
但我不知道是否通过修改大量的点生成了错误,或者公式是假的,因为结果如下:the application has requested the Runtime to terminate it in an unusual way.
感谢。
答案 0 :(得分:0)
你不应该改变for
循环的界限吗?
for(i=0; i < contours[0].size(); i++)
{
contours_poly[0][i].x = contours_poly[0][i].x - mc[1].x;
contours_poly[0][i].y = contours_poly[0][i].y - mc[1].y;
contours_poly[1][i].x = contours_poly[1][i].x - mc[1].x;
contours_poly[1][i].y = contours_poly[1][i].y - mc[1].y;
}