Jos wrote:
>
>
> Abo Bshire wrote:
>>
>>
>> I am looking for a code to calculate the centroid (the center
of
>> mass) of a binary image.
>>
>> any help is appreciated.
>>
>> thanks,
>> Abo Bshire
>
> (since any help is appreciated,) what about:
> % A is your binary (0,1) image
> [x,y] = find (A) ;
> CenterOfMassXY = [mean(x) mean(y)] ;
>
> Jos
Thanks Jos for your help.
I am interseted in the centroid of the whole image not just a region
or an object.
I have the following code to calculate the centroid:
im = imread(imfile);
[rows,cols] = size(im);
x = ones(rows,1)*[1:cols]; area = sum(sum(im));
meanx = sum(sum(double(im).*x))/area;
meany = sum(sum(double(im).*y))/area;
amazingly, your code gives the same result:
[x,y] = find (A) ;
CenterOfMassXY = [mean(x) mean(y)] ;
Now, I used ImageJ software to verify the results of my/your code.
But, it gave me a different result.
For example, my/your code gives: 364.6110 259.3841
ImageJ gives: 363.286 256.779
what is the problem here? which one is wrong?
thanks,
Abo Bshire
