COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Fitting a circle inside a ROI

• Follow

```Hi all,

I have imaged a circular object in an MRI scanner.  I am trying to measure the 2d distortion of the image of this object.  I have loaded this image as an matrix in matlab.  I want to find out the largest possible circle that could be fit into the image of the circular object.  Then I want to work out what area of the imaged object is not covered by this circle that i have fitted.  I am going to use this as a measure of the 2d distortion of the image.
Many Thanks,
Arun
```
 0

```Arun:
Can you threshold your image to get a binary image of the circular
area?  That would be the first step.  Then find the centroid and the
three points closest to the centroid, and you're practically done.
ImageAnalyst

```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f8f2544d-6e99-4d3f-a1e0-ec0f29707186@k31g2000vbu.googlegroups.com>...
> Arun:
> Can you threshold your image to get a binary image of the circular
> area?  That would be the first step.  Then find the centroid and the
> three points closest to the centroid, and you're practically done.
> ImageAnalyst

Image Analyst:

I have have done the thresholding. I am in the process of finding out how to find the centroid of the ROI.
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f8f2544d-6e99-4d3f-a1e0-ec0f29707186@k31g2000vbu.googlegroups.com>...
> Arun:
> Can you threshold your image to get a binary image of the circular
> area?  That would be the first step.  Then find the centroid and the
> three points closest to the centroid, and you're practically done.

If the circular happens to be a perfect disc, any point in the circumference is the closest to the centroid, no? And the set of the centroids any three points located on the circle is the disc itself, no?

It seems this method is not very robust, at least for the circle.

What about find the biggest (has largest radius) circumscribed circle of all Delaunay triangles of the set of the points on the *boundary* of the object?

Bruno
```
 0

```Why don't you use a "shape factor" for this purpose? cfi:
http://en.wikipedia.org/wiki/Shape_factor_%28image_analysis_and_microscopy%29

you may want to check out the regionprops function of the image processing toolbox as well ...

Rik

"Arun Kook" <arun_kook@yahoo.co.uk> wrote in message <hto65i\$iu\$1@fred.mathworks.com>...
> Hi all,
>
> I have imaged a circular object in an MRI scanner.  I am trying to measure the 2d distortion of the image of this object.  I have loaded this image as an matrix in matlab.  I want to find out the largest possible circle that could be fit into the image of the circular object.  Then I want to work out what area of the imaged object is not covered by this circle that i have fitted.  I am going to use this as a measure of the 2d distortion of the image.
> Many Thanks,
> Arun
```
 0

```On May 28, 7:49=A0am, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <f8f2544d-6e9=
> > Arun:
> > Can you threshold your image to get a binary image of the circular
> > area? =A0That would be the first step. =A0Then find the centroid and th=
e
> > three points closest to the centroid, and you're practically done.
>
> If the circular happens to be a perfect disc, any point in the circumfere=
nce is the closest to the centroid, no? And the set of the centroids any th=
ree points located on the circle is the disc itself, no?
>
> It seems this method is not very robust, at least for the circle.
>
> What about find the biggest (has largest radius) circumscribed circle of =
all Delaunay triangles of the set of the points on the *boundary* of the ob=
ject?
>
> Bruno

---------------------------------------------------------------------------=
-----------------------------------
Bruno:
Since this is digitized on a discrete matrix, not all perimeter
coordinates of the "circle" will lie the same distance from the
centroid (even if it were a "perfect" circle but certainly not for
whatever approximately circular shape that his MRI object may have).
There may be several (but definitely not all) that are the same
distance.  Using any three points will define a circle, using the
circumscribed circle formula.

Arun said he wanted the "largest possible circle that could be fit
into the image of the circular object" - I believe the key word being
"into."  So in other words he has a bunch of perimeter points obtained
from a fairly circular object and he wants the largest circle that can
fit INTO those points.  To do this you'd first find the centroid of
the entire circular area, then simply take the three perimeter points
that are CLOSEST to the centroid and pass those points into a formula
such as this http://en.wikipedia.org/wiki/Circumscribed_circle
I believe this is accurate for fairly circular shapes but would not
work for shapes where the centroid is outside the object (such as an
object shaped like the letter "C"), or very close to the perimeter.
For those bizarre shapes, you could use morphology to compute the
"ultimate eroded set" and then do a few more steps.

What you suggested is like what you suggest but I believe you're
suggesting a method to find the SMALLEST circle that circumscribes the
OUTER perimeter points - the opposite of what I suggested.  If he
wanted to do that instead of finding the largest inner circle, he
could take the convex hull and then use the circumscribed circle
formula again (I don't see how computng the triangles is necessary).

On the other hand, if he really wanted the best FIT to ALL the
coordinates rather than the largest inside or smallest outside circle
then neither of us is right.

Arun, you can use bwlabel and regionprops to locate the centroid.
thresholdValue =3D 100;
binaryImage =3D originalImage > thresholdValue; % Bright objects will be
the chosen if you use >.
% Do a "hole fill" to get rid of any background pixels inside the
blobs.
binaryImage =3D imfill(binaryImage, 'holes');
labeledImage =3D bwlabel(binaryImage, 8);     % Label each blob so we
can make measurements of it
% Get all the blob properties.  Can only pass in originalImage in
version R2008a and later.
blobMeasurements =3D regionprops(labeledImage, originalImage, 'all');

I demonstrate how to find centroids (of circular coins) in my image
analysis demo.
http://www.mathworks.com/matlabcentral/fileexchange/25157

-ImageAnalyst

```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0e13b52b-73fe-4185-afb7-b288b7f911e0@y21g2000vba.googlegroups.com>...
> On May 28, 7:49 am, "Bruno Luong" <b.lu...@fogale.findmycountry>
> wrote:
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <f8f2544d-6e99-4d3f-a1e0-ec0f29707...@k31g2000vbu.googlegroups.com>...
> > > Arun:
> > > Can you threshold your image to get a binary image of the circular
> > > area?  That would be the first step.  Then find the centroid and the
> > > three points closest to the centroid, and you're practically done.
> >
> > If the circular happens to be a perfect disc, any point in the circumference is the closest to the centroid, no? And the set of the centroids any three points located on the circle is the disc itself, no?
> >
> > It seems this method is not very robust, at least for the circle.
> >
> > What about find the biggest (has largest radius) circumscribed circle of all Delaunay triangles of the set of the points on the *boundary* of the object?
> >
> > Bruno
>
> --------------------------------------------------------------------------------------------------------------
> Bruno:
> Since this is digitized on a discrete matrix, not all perimeter
> coordinates of the "circle" will lie the same distance from the
> centroid (even if it were a "perfect" circle but certainly not for
> whatever approximately circular shape that his MRI object may have).
> There may be several (but definitely not all) that are the same
> distance.  Using any three points will define a circle, using the
> circumscribed circle formula.
>
> Arun said he wanted the "largest possible circle that could be fit
> into the image of the circular object" - I believe the key word being
> "into."  So in other words he has a bunch of perimeter points obtained
> from a fairly circular object and he wants the largest circle that can
> fit INTO those points.  To do this you'd first find the centroid of
> the entire circular area, then simply take the three perimeter points
> that are CLOSEST to the centroid and pass those points into a formula
> such as this http://en.wikipedia.org/wiki/Circumscribed_circle
> I believe this is accurate for fairly circular shapes but would not
> work for shapes where the centroid is outside the object (such as an
> object shaped like the letter "C"), or very close to the perimeter.
> For those bizarre shapes, you could use morphology to compute the
> "ultimate eroded set" and then do a few more steps.
>
> What you suggested is like what you suggest but I believe you're
> suggesting a method to find the SMALLEST circle that circumscribes the
> OUTER perimeter points - the opposite of what I suggested.  If he
> wanted to do that instead of finding the largest inner circle, he
> could take the convex hull and then use the circumscribed circle
> formula again (I don't see how computng the triangles is necessary).
>
> On the other hand, if he really wanted the best FIT to ALL the
> coordinates rather than the largest inside or smallest outside circle
> then neither of us is right.
>
> Arun, you can use bwlabel and regionprops to locate the centroid.
> thresholdValue = 100;
> binaryImage = originalImage > thresholdValue; % Bright objects will be
> the chosen if you use >.
> % Do a "hole fill" to get rid of any background pixels inside the
> blobs.
> binaryImage = imfill(binaryImage, 'holes');
> labeledImage = bwlabel(binaryImage, 8);     % Label each blob so we
> can make measurements of it
> % Get all the blob properties.  Can only pass in originalImage in
> version R2008a and later.
> blobMeasurements = regionprops(labeledImage, originalImage, 'all');
>
> I demonstrate how to find centroids (of circular coins) in my image
> analysis demo.
> http://www.mathworks.com/matlabcentral/fileexchange/25157
>
> -ImageAnalyst
>
>

Image Analyst
Many thanks for your help.  I am nearly done with the solution using the way you described.  I am new to matlab and image processing in general, thus was trying figure out how i would find the centroid of the object.

I managed to do that using this
s = regionprops(L, 'Centroid');

Now i am trying to find the three points on the object that are the shortest distance.

Thanks again
Arun
```
 0

```On May 28, 10:36=A0am, "Arun Kook" <arun_k...@yahoo.co.uk> wrote:
> Image Analyst
> Many thanks for your help. =A0I am nearly done with the solution using th=
e way you described. =A0I am new to matlab and image processing in general,=
thus was trying figure out how i would find the centroid of the object. =
=A0
>
> I managed to do that using this
> s =3D regionprops(L, 'Centroid');
>
> Now i am trying to find the three points on the object that are the short=
est distance.
>
> Thanks again
> Arun-
--------------------------------------
You can use bwboundaries (like in the demo) and hypot() inside a
simple for loop for that if you want.  Should be very fast.  Or you
can use a vectorized method.  Both are lightning fast since you
probably only have a few thousand points.  Whatever is easiest and
most intuitive for you...
Just to make sure, you do want the largest inscribed circle, right,
not the best fit to all the boundary points, right?
-ImageAnalyst
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0e13b52b-73fe-4185-afb7-b288b7f911e0@y21g2000vba.googlegroups.com>...

> Since this is digitized on a discrete matrix, not all perimeter
> coordinates of the "circle" will lie the same distance from the
> centroid (even if it were a "perfect" circle but certainly not for
> whatever approximately circular shape that his MRI object may have).
> There may be several (but definitely not all) that are the same
> distance.  Using any three points will define a circle, using the
> circumscribed circle formula.

I can't see why "discrete" can escape to avoid the issue. Even the pixel is discrete, there is always a slight chance where the closest points are not disperse, but they fall very close in absolute distance, unless if it has been proven.
>
> What you suggested is like what you suggest but I believe you're
> suggesting a method to find the SMALLEST circle that circumscribes the
> OUTER perimeter points - the opposite of what I suggested.

No, What I suggested is infact the largest circle that does not cross any pixels other than the three in the triangles.

In the code below, OP should replace (x,y) entered by user by the list of pixels coordinates of the circumference (deleted by other means), and we can see it's the largest circle that does not contain any other pixel.

function [R cx cy] = circumscribedcircle(x, y)

x = x(:);
y = y(:);
dx = diff(x([2 3 1 2]));
dy = diff(y([2 3 1 2]));
Ta = abs(det([dx(1:2) dy(1:2)]));
l2 = dx.^2+dy.^2;
R = prod(sqrt(l2))/(2*Ta);
w = l2.*(toeplitz([-1 1 1])*l2);
w = w/sum(w);
cx = w.'*x;
cy = w.'*y;

end % circumscribedcircle

%% Script
clf;
npnt = 20;
fprintf('Use the mouse and enter %d points on the circumference\n', npnt);
[x y] = ginput(npnt);

%% Engine
tri = delaunay(x,y);
getR = @(i) circumscribedcircle(x(tri(i,:)), y(tri(i,:)));
[R cx cy] = arrayfun(getR, 1:size(tri,1));
% wee need this to avoid circle that encloses concave boundary points
in = inpolygon(cx, cy, x, y);
in = find(in);
[~, imax]=max(R(in)); % largest radius of the circle
imax = in(imax);
tx = x(tri(imax,:));
ty = y(tri(imax,:));
[Rmax cx cy] = circumscribedcircle(tx, ty);

% Graphic check
theta = [linspace(0,2*pi) 0];
plot(x([1:end 1]),y([1:end 1]),'ro-');
hold on
plot(cos(theta)*Rmax+cx,sin(theta)*Rmax+cy)
axis equal

% Bruno
```
 0

```Bruno:
Apparently I misinterpreted your comment "find the biggest (has
largest radius) circumscribed circle of all Delaunay triangles of the
set of the points on the *boundary* of the object" because when I ran
your code, it does work to find the largest inside circle.  Sorry
about that - it's nice work.

```
 0

9 Replies
520 Views

Similiar Articles:

7/22/2012 9:12:02 AM