Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You are talking about mask so above code is working fine but generally speaking you should take care about difference between geometric centre and centre of mass.

In geometric centre all points has same weight while in centre of mass each point is weighted with its intensity.

You should note that image moments calculates centre of mass.

For an image mask, geometric and centre of mass are the same because relevant pixels in mask are all 1 but generally speaking this is not true.

here is the code:

// GET GEOMETRIC CENTRE
//---------------------
Mat mask;
compare(difference,Scalar(50),mask,CMP_GE);

// inRange function is also available
//inRange(difference,Scalar(50),Scalar(255),mask);

//mask is now logical image (0/1)
Moments m = moments(mask); // true flag is not needed because is a mask (0/1)
Point geometricCentre(m.m10/m.m00, m.m01/m.m00);

// GET CENTRE OF MASS
//---------------------
Mat tmp;
tmp = difference * mask;  // keep only wanted pixels (mask == 1 for useful pix and 0 for unwanted one)

// tmp is a gray mask
m = moments(tmp,false);   // avoid true flag for centre of mass
Point massCentre(m.m10/m.m00, m.m01/m.m00);

In addition true flag can be used to calculate geometric center on a gray scale mask

m = moments(tmp,true);   // true flag is needed because is a gray mask
Point geometricCentreAgain(m.m10/m.m00, m.m01/m.m00);
click to hide/show revision 2
remove wrong matrix multiplication

You are talking about mask so than above code is working fine but generally speaking you should take care about difference between geometric centre and centre of mass.

In geometric centre all points has same weight while in centre of mass each point is weighted with its intensity.

You should note that image moments calculates centre of mass.

For an image mask, geometric and centre of mass are the same because relevant pixels in mask are all 1 has same intensity but generally speaking this is not true.

here is the code:

// GET GEOMETRIC CENTRE
//---------------------
Mat mask;
compare(difference,Scalar(50),mask,CMP_GE);

// You could use inRange function is also available
 //inRange(difference,Scalar(50),Scalar(255),mask);

//mask is now logical image (0/1)
image
Moments m = moments(mask); // true flag is not needed because is a mask (0/1)
mask
Point geometricCentre(m.m10/m.m00, m.m01/m.m00);

// GET CENTRE OF MASS
//---------------------
Mat tmp;
tmp = difference * mask;  // keep tmp(difference.size(),difference.type());
tmp.setTo(0);
difference.copyTo(tmp,mask);  // copy only wanted pixels (mask == 1 for useful pix and 0 for unwanted one)

// pixels
//matrix multiplication might be used if mask is 0/1
//Mat tmp = difference.mul(mask);

// tmp is a gray mask
mask now
m = moments(tmp,false);   // avoid true flag for centre of mass
Point massCentre(m.m10/m.m00, m.m01/m.m00);

In addition true flag can be used to calculate geometric center on a gray scale mask

m = moments(tmp,true);   // true flag is needed because is a gray mask
Point geometricCentreAgain(m.m10/m.m00, m.m01/m.m00);