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:
Circular region within a matrix

Subject: Circular region within a matrix

From: Philipp Steffen

Date: 7 Sep, 2010 19:17:07

Message: 1 of 11

Hallo,

I am pretty new is using MATLAB and currently encounter the following problem:
I have a matrix of 512x76 values. I need to calculate the average of a circular region within that matrix. You could think of the matrix as an image where I would like to measure the average intensity of a circular region of let say 20 pixels radius in the center of that image.
So far I figured out that most likely a logical mask would help me to set all values outside of the circular region to zero but how can I define a logical mask that represents a circular region?
If anybody could give me some advice how to achieve this without the Image Processing toolbox I would be very happy.
Thank you in advance,

Philipp

Subject: Circular region within a matrix

From: Sean

Date: 7 Sep, 2010 19:58:10

Message: 2 of 11

"Philipp Steffen" <philipp.steffen@imba.oeaw.ac.at> wrote in message <i6633j$6gp$1@fred.mathworks.com>...
> Hallo,
>
> I am pretty new is using MATLAB and currently encounter the following problem:
> I have a matrix of 512x76 values. I need to calculate the average of a circular region within that matrix. You could think of the matrix as an image where I would like to measure the average intensity of a circular region of let say 20 pixels radius in the center of that image.
> So far I figured out that most likely a logical mask would help me to set all values outside of the circular region to zero but how can I define a logical mask that represents a circular region?
> If anybody could give me some advice how to achieve this without the Image Processing toolbox I would be very happy.
> Thank you in advance,
>
> Philipp

Just use the formula for a circle:

%Circle with radius centered at 40,40 in an 80x80image
[rr cc] = meshgrid(1:80);
C = sqrt((rr-40).^2+(cc-40).^2)<=20;
imshow(C)

Subject: Circular region within a matrix

From: Image Analyst

Date: 7 Sep, 2010 20:50:19

Message: 3 of 11

Then you could take Sean's C and use it as logical indexing in your image to get the mean of the image in that disc location:
meanGL = mean(grayImage(C))

Subject: Circular region within a matrix

From: Philipp Steffen

Date: 7 Sep, 2010 21:15:27

Message: 4 of 11

Thank you both! That is a really elegant solution and much simpler then I expected.
I really appreciate your fast help.
Philipp

Subject: Circular region within a matrix - follow up question

From: Naama

Date: 17 Jan, 2011 10:21:05

Message: 5 of 11

Hi,

I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient. Can anyone think of a good way to define several circlular masks on one image?

Thanks!

Subject: Circular region within a matrix - follow up question

From: Matt J

Date: 17 Jan, 2011 11:03:04

Message: 6 of 11

"Naama" wrote in message <ih156h$fng$1@fred.mathworks.com>...
> Hi,
>
> I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient.
=======

why does this seem inefficient?

Subject: Circular region within a matrix - follow up question

From: Matt J

Date: 17 Jan, 2011 11:09:04

Message: 7 of 11

"Naama" wrote in message <ih156h$fng$1@fred.mathworks.com>...
> Hi,
>
> I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient.
============

It might be slightly more efficient to do logical addition(via '|') rather than via '+'
For example, for two circles:


[xx yy] = meshgrid(1:80);
C = sqrt(xx-cx1).^2+(yy-cy1).^2)<=R1 | sqrt(xx-cx2).^2+(yy-cy2).^2)<=R2 ;

Subject: Circular region within a matrix - follow up question

From: Matt J

Date: 17 Jan, 2011 11:26:05

Message: 8 of 11

"Matt J" wrote in message <ih180g$a46$1@fred.mathworks.com>...
> "Naama" wrote in message <ih156h$fng$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient.
> ============
>
> It might be slightly more efficient to do logical addition(via '|') rather than via '+'
> For example, for two circles:
>
>
> [xx yy] = meshgrid(1:80);
> C = sqrt(xx-cx1).^2+(yy-cy1).^2)<=R1 | sqrt(xx-cx2).^2+(yy-cy2).^2)<=R2 ;
==========

You could also improve the efficiency of this by using BSXFUN instead of MESHGRID. That way, memory for a full grid of data will only be allocated to store the final result:


cc=1:80;
rr=cc.';

cx1=40; cy1=40; R1=20;
cx2=10; cy2=10; R2=10;


f=@(xx,yy) (xx-cx1).^2+(yy-cy1).^2 <=R1^2 | (xx-cx2).^2+(yy-cy2).^2 <=R2^2 ;
 

C=bsxfun(f,rr,cc); %Logical map of 2 circles
figure; imshow(C)

Subject: Circular region within a matrix - follow up question

From: Naama

Date: 17 Jan, 2011 11:35:06

Message: 9 of 11

"Matt J" wrote in message <ih17l8$ib3$1@fred.mathworks.com>...
> "Naama" wrote in message <ih156h$fng$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient.
> =======
>
> why does this seem inefficient?

because that would require a loop... and I might need a large number of itteration (could be up to 100)

Subject: Circular region within a matrix - follow up question

From: Naama

Date: 17 Jan, 2011 11:37:04

Message: 10 of 11

"Matt J" wrote in message <ih190d$c16$1@fred.mathworks.com>...
> "Matt J" wrote in message <ih180g$a46$1@fred.mathworks.com>...
> > "Naama" wrote in message <ih156h$fng$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I am trying to do the same thing, except I would like to define several circles in the image and not just one. Of course I could do it separately for each circle and then add them up, but it seems inefficient.
> > ============
> >
> > It might be slightly more efficient to do logical addition(via '|') rather than via '+'
> > For example, for two circles:
> >
> >
> > [xx yy] = meshgrid(1:80);
> > C = sqrt(xx-cx1).^2+(yy-cy1).^2)<=R1 | sqrt(xx-cx2).^2+(yy-cy2).^2)<=R2 ;
> ==========
>
> You could also improve the efficiency of this by using BSXFUN instead of MESHGRID. That way, memory for a full grid of data will only be allocated to store the final result:
>
>
> cc=1:80;
> rr=cc.';
>
> cx1=40; cy1=40; R1=20;
> cx2=10; cy2=10; R2=10;
>
>
> f=@(xx,yy) (xx-cx1).^2+(yy-cy1).^2 <=R1^2 | (xx-cx2).^2+(yy-cy2).^2 <=R2^2 ;
>
>
> C=bsxfun(f,rr,cc); %Logical map of 2 circles
> figure; imshow(C)

Will try that, thanks!

Subject: Circular region within a matrix - follow up question

From: ImageAnalyst

Date: 17 Jan, 2011 14:49:25

Message: 11 of 11

Naama:
First of all, 100 is not a large number. Even 100 thousand is not a
large number. That code would happen faster than a blink of an eye.

An alternative way is to use image processing. Just threshold, label,
and call regionprops. That would get you the measurements for every
circle in your image. In fact I do exactly that in my BlobsDemo. I
even use circles (coins) like you want. See my demo if you want an
example:
http://www.mathworks.com/matlabcentral/fileexchange/authors/31862

Tags for 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