Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to find the centroid of a binary image?

Subject: how to find the centroid of a binary image?

From: Abo Bshire

Date: 11 Oct, 2005 10:05:46

Message: 1 of 5

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

Subject: how to find the centroid of a binary image?

From: Brett Shoelson

Date: 11 Oct, 2005 10:16:17

Message: 2 of 5



"Abo Bshire" <moh054@yahoo.com> wrote in message
news:ef17d25.-1@webx.raydaftYaTP...
>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

Hi Abo,
I assume that you mean that you are looking for the centroid of an
object/region in a binary image. Suggest you read the help docs for
REGIONPROPS. With BWLABEL and regionprops, centroids are a breeze.
Brett
--
char(cumsum(...
[115 -11 7 -10 7 7 -4 -1 -46 40 -3 7 -3 15 -74 64 -5 -1 -58 57 8 7]))

Subject: how to find the centroid of a binary image?

From: Jos

Date: 11 Oct, 2005 10:24:42

Message: 3 of 5

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

Subject: how to find the centroid of a binary image?

From: Abo Bshire

Date: 11 Oct, 2005 11:09:55

Message: 4 of 5

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

Subject: how to find the centroid of a binary image?

From: Jos

Date: 11 Oct, 2005 17:08:17

Message: 5 of 5

Abo Bshire wrote:
>
>
> <SNIP Find Centroid code
>
> 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?

No idea. I don't know which algorithm ImageJ uses.

Can't you make a test image?
a = zeros(10,10) ; a(5,5)=1 ; a(5,6)=1
should give (5,5.5)

Personnally, I trust our MatLab codes ;-)
However, I am not sure if this really defines the center of mass.

hth
Jos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us