You are talking about mask 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 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
//inRange(difference,Scalar(50),Scalar(255),mask);
//mask is now logical image
Moments m = moments(mask); // true flag is not needed because is a mask
Point geometricCentre(m.m10/m.m00, m.m01/m.m00);
// GET CENTRE OF MASS
//---------------------
Mat tmp(difference.size(),difference.type());
tmp.setTo(0);
difference.copyTo(tmp,mask); // copy only wanted pixels
//matrix multiplication might be used if mask is 0/1
//Mat tmp = difference.mul(mask);
// tmp is a gray 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);