我使用以下代码来使用 opencv 计算对象的力矩和位置:
// calc object moments
CvMoments *moments = (CvMoments*) malloc(sizeof(CvMoments));
cvMoments(threshy, moments, 1);
// get the moment values
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
// save the X and Y position from previous iteration.
int lastX = currentX;
int lastY = currentY;
// calculate the new X and Y positions.
currentX = moment10/area;
currentY = moment01/area;
只要场景中只有一个对象,这种方法就非常有效。但是,如果有多个对象,currentX
和 currentY
将得出所有对象的中点。
有没有办法计算每个物体的力矩(和坐标)?
最佳答案
为此,您必须使用 countours。对于 C++ api,您有一个名为 cv::findContours 的函数,您可以在其中传递阈值图像,然后计算每个计数/对象的时刻。查找更多详细信息OpenCV documentation .
关于opencv - 如何计算每个对象的力矩和位置而不是对象之间的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553516/