Thanks to Pau Santos who requested a Physics 71 review two weeks ago, I was able to relearn the art of locating the center of mass.
Say we have a binarized image of an object, assuming that it has uniform density and symmetrical along the axis perpendicular to the plane of the image, we can find its center of mass with this simple bit of code in Scilab:
First we find the pixels representing the object
x = Img; xcoord = linspace(1,size(Img,2),size(Img,2)); for i = 1:size(Img,1) x(i,:) = x(i,:).*xcoord; end
Finally, computing for the center of mass
xcom = sum(x)/sum(Img);
I guess with ndgrid() we can make the code shorter by eliminating the for() loop.