memyself
memyself

Reputation: 12628

how to calculate moments and position per object instead of average across objects?

I'm using the following code to calculate the moments and the position of an object using 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;

this works great, as long as there's only one object in the scene. If however there are multiple objects, currentX and currentY will result in the mid-point of all objects.

Is there a way to calculate the moments (and coordinates) of each object?

Upvotes: 2

Views: 781

Answers (1)

Banana
Banana

Reputation: 1366

You will have to use countours for that. For C++ api you have a function called cv::findContours where you pass your thresholded image, after that you calculate the moments over each countour/object. Look up for more details in OpenCV documentation.

Upvotes: 2

Related Questions