Thread Subject: fit an ellipse over an object...

Subject: fit an ellipse over an object...

From: Clare

Date: 24 Nov, 2009 07:35:17

Message: 1 of 9

Hi all, this post is related to my earlier post on using "imshow, plot and quiver".

So here is my problem...

I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:

%draw ellipse
    x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
    y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);

where "center" is the centroid coordinate of a given cell
"phi" is the angle between major axis of the cell and the x-axis of the image.
"a and b" are lengths of the major and minor axes respectively.
"t" is a parameter varying between [0, 2*pi].
and "x, y" are computed Cartesian coordinates of the ellipse

The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:

imshow(Image); hold on; plot(x,y); hold off;

Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.

I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.

Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!

Subject: fit an ellipse over an object...

From: Jan Simon

Date: 24 Nov, 2009 08:38:02

Message: 2 of 9

Dear Clare!

> imshow(Image); hold on; plot(x,y); hold off;
>
> Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.

This is not strange.
1. Images imported by IMREAD are defined with the origin in the top left corner. If they are displayed in a AXES object, the YDir must be 'reverse' to show them correctly.
2. Plot objects (LINE, PLOT, PATCH, SURFACE, ...) are defined with the origin in the bottom left corner. Therefore the AXES object must have 'normal' YDir.

=> If you want to combine two such objects, you have to decide for one of the two YDir definitions and flip one of the objects upsidedown.
E.g.:
  imshow(Image(:, end:-1:1, :))

Kind regards, Jan

Subject: fit an ellipse over an object...

From: Clare

Date: 24 Nov, 2009 08:43:03

Message: 3 of 9

Great Jan! Thanks so much! It really makes sense now.

Subject: fit an ellipse over an object...

From: Clare

Date: 1 Dec, 2009 11:04:03

Message: 4 of 9

In the end I found that the easiest way was just to invert the angle.

phi = - phi basically solved the problem beautifully. Thanks again.

"Clare " <flyingclare@gmail.com> wrote in message <heg6an$2f$1@fred.mathworks.com>...
> Great Jan! Thanks so much! It really makes sense now.

Subject: fit an ellipse over an object...

From: Alex Taylor

Date: 1 Dec, 2009 22:07:03

Message: 5 of 9

"Clare " <flyingclare@gmail.com> wrote in message <heg2bl$qno$1@fred.mathworks.com>...
> Hi all, this post is related to my earlier post on using "imshow, plot and quiver".
>
> So here is my problem...
>
> I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:
>
> %draw ellipse
> x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
> y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);
>
> where "center" is the centroid coordinate of a given cell
> "phi" is the angle between major axis of the cell and the x-axis of the image.
> "a and b" are lengths of the major and minor axes respectively.
> "t" is a parameter varying between [0, 2*pi].
> and "x, y" are computed Cartesian coordinates of the ellipse
>
> The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:
>
> imshow(Image); hold on; plot(x,y); hold off;
>
> Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.
>
> I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.
>
> Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!

If you want to draw an ellipse over an image, you could also use the function imellipse that is provided in the Image Processing Toolbox. It provides an API that allows you to do lots of different things with an ellipse over an image. For more info:

doc imellipse

Subject: fit an ellipse over an object...

From: Kenneth Galea

Date: 28 Dec, 2009 14:15:23

Message: 6 of 9

"Clare " <flyingclare@gmail.com> wrote in message <heg2bl$qno$1@fred.mathworks.com>...
> Hi all, this post is related to my earlier post on using "imshow, plot and quiver".
>
> So here is my problem...
>
> I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:
>
> %draw ellipse
> x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
> y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);
>
> where "center" is the centroid coordinate of a given cell
> "phi" is the angle between major axis of the cell and the x-axis of the image.
> "a and b" are lengths of the major and minor axes respectively.
> "t" is a parameter varying between [0, 2*pi].
> and "x, y" are computed Cartesian coordinates of the ellipse
>
> The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:
>
> imshow(Image); hold on; plot(x,y); hold off;
>
> Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.
>
> I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.
>
> Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!

Hi, I'm just doing the same thing.....i.e. fitting an ellipse on different objects by using regionprops. May I ask you if your ellipses are becoming much larger than the object itself and even not fitting in the image??
Here's my code (similar to yours for the ellipse drawing):
o = regionprops (L, 'orientation');
orientations = cat(1, o.Orientation);
phi_1 = orientations(1)* (pi/180) %convert to radians

a = regionprops (L, 'majorAxisLength');
majorAxisLengths = cat(1, a.MajorAxisLength);
a_1 = majorAxisLengths(1)

b = regionprops (L, 'minorAxisLength');
minorAxisLengths = cat(1, b.MinorAxisLength);
b_1 = minorAxisLengths(1)

t= (0 : 0.01 : 2*pi);

% Draw Ellipse
    x_1 = center_1(1)+a_1*cos(phi_1)*cos(t)-b_1*sin(phi_1)*sin(t)
    y_1 = center_1(2)+a_1*sin(phi_1)*cos(t)+b_1*cos(phi_1)*sin(t)

figure
 plot(x_1,y_1);

figure
   imshow (RGB); %RGB Labelled matrix containing different labelled objects
    hold on;
    plot(x_1,y_1);
    hold off;

This resulted in the following... Check the following link:
http://drop.io/ellipse_plot

As you can see the image (640x480) contains only a portion of the ellipse since its values (x,y) are becoming much larger!! Any help is greatly appreciated!!!
Thanks
Kenneth

Subject: fit an ellipse over an object...

From: Kenneth Galea

Date: 28 Dec, 2009 14:15:23

Message: 7 of 9

"Clare " <flyingclare@gmail.com> wrote in message <heg2bl$qno$1@fred.mathworks.com>...
> Hi all, this post is related to my earlier post on using "imshow, plot and quiver".
>
> So here is my problem...
>
> I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:
>
> %draw ellipse
> x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
> y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);
>
> where "center" is the centroid coordinate of a given cell
> "phi" is the angle between major axis of the cell and the x-axis of the image.
> "a and b" are lengths of the major and minor axes respectively.
> "t" is a parameter varying between [0, 2*pi].
> and "x, y" are computed Cartesian coordinates of the ellipse
>
> The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:
>
> imshow(Image); hold on; plot(x,y); hold off;
>
> Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.
>
> I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.
>
> Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!

Hi, I'm just doing the same thing.....i.e. fitting an ellipse on different objects by using regionprops. May I ask you if your ellipses are becoming much larger than the object itself and even not fitting in the image??
Here's my code (similar to yours for the ellipse drawing):
o = regionprops (L, 'orientation');
orientations = cat(1, o.Orientation);
phi_1 = orientations(1)* (pi/180) %convert to radians

a = regionprops (L, 'majorAxisLength');
majorAxisLengths = cat(1, a.MajorAxisLength);
a_1 = majorAxisLengths(1)

b = regionprops (L, 'minorAxisLength');
minorAxisLengths = cat(1, b.MinorAxisLength);
b_1 = minorAxisLengths(1)

t= (0 : 0.01 : 2*pi);

% Draw Ellipse
    x_1 = center_1(1)+a_1*cos(phi_1)*cos(t)-b_1*sin(phi_1)*sin(t)
    y_1 = center_1(2)+a_1*sin(phi_1)*cos(t)+b_1*cos(phi_1)*sin(t)

figure
 plot(x_1,y_1);

figure
   imshow (RGB); %RGB Labelled matrix containing different labelled objects
    hold on;
    plot(x_1,y_1);
    hold off;

This resulted in the following... Check the following link:
http://drop.io/ellipse_plot

As you can see the image (640x480) contains only a portion of the ellipse since its values (x,y) are becoming much larger!! Any help is greatly appreciated!!!
Thanks
Kenneth

Subject: fit an ellipse over an object...

From: Monica Hasegan

Date: 12 Jul, 2010 19:33:05

Message: 8 of 9

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <hhaehr$bvl$1@fred.mathworks.com>...
> "Clare " <flyingclare@gmail.com> wrote in message <heg2bl$qno$1@fred.mathworks.com>...
> > Hi all, this post is related to my earlier post on using "imshow, plot and quiver".
> >
> > So here is my problem...
> >
> > I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:
> >
> > %draw ellipse
> > x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
> > y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);
> >
> > where "center" is the centroid coordinate of a given cell
> > "phi" is the angle between major axis of the cell and the x-axis of the image.
> > "a and b" are lengths of the major and minor axes respectively.
> > "t" is a parameter varying between [0, 2*pi].
> > and "x, y" are computed Cartesian coordinates of the ellipse
> >
> > The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:
> >
> > imshow(Image); hold on; plot(x,y); hold off;
> >
> > Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.
> >
> > I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.
> >
> > Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!
>
> Hi, I'm just doing the same thing.....i.e. fitting an ellipse on different objects by using regionprops. May I ask you if your ellipses are becoming much larger than the object itself and even not fitting in the image??
> Here's my code (similar to yours for the ellipse drawing):
> o = regionprops (L, 'orientation');
> orientations = cat(1, o.Orientation);
> phi_1 = orientations(1)* (pi/180) %convert to radians
>
> a = regionprops (L, 'majorAxisLength');
> majorAxisLengths = cat(1, a.MajorAxisLength);
> a_1 = majorAxisLengths(1)
>
> b = regionprops (L, 'minorAxisLength');
> minorAxisLengths = cat(1, b.MinorAxisLength);
> b_1 = minorAxisLengths(1)
>
> t= (0 : 0.01 : 2*pi);
>
> % Draw Ellipse
> x_1 = center_1(1)+a_1*cos(phi_1)*cos(t)-b_1*sin(phi_1)*sin(t)
> y_1 = center_1(2)+a_1*sin(phi_1)*cos(t)+b_1*cos(phi_1)*sin(t)
>
> figure
> plot(x_1,y_1);
>
> figure
> imshow (RGB); %RGB Labelled matrix containing different labelled objects
> hold on;
> plot(x_1,y_1);
> hold off;
>
> This resulted in the following... Check the following link:
> http://drop.io/ellipse_plot
>
> As you can see the image (640x480) contains only a portion of the ellipse since its values (x,y) are becoming much larger!! Any help is greatly appreciated!!!
> Thanks
> Kenneth
Hi,
I hope you solved you problem by now, but just for the sake of having it addressed here, here it's my finding: the a_1 and b_1 should be equal to *half* of majorAxisLength, and minorAxisLength respectively. So a_1 = majorAxisLength/2 and similar for b_1.
Cheers,
Monica

Subject: fit an ellipse over an object...

From: Dan

Date: 13 Apr, 2011 21:06:05

Message: 9 of 9

"Monica Hasegan" wrote in message <i1fqlh$l4o$1@fred.mathworks.com>...
> "Kenneth Galea" <k.galea@hotmail.com> wrote in message <hhaehr$bvl$1@fred.mathworks.com>...
> > "Clare " <flyingclare@gmail.com> wrote in message <heg2bl$qno$1@fred.mathworks.com>...
> > > Hi all, this post is related to my earlier post on using "imshow, plot and quiver".
> > >
> > > So here is my problem...
> > >
> > > I have these elongated shapes in an image (they are biological cells) and I want to fit an ellipse to each of them - basically draw an ellipse around each cell. The image has already been converted to binary. And I did the usual: component labeling and finding "region props". From "regionprops" command I have the following information: centroid of each cell and the orientation of the major axes of each cell (angle between major axis and the image x-axis). Then I plugged these values to the following ellipse-generating equation:
> > >
> > > %draw ellipse
> > > x = center(1)+a*cos(phi)*cos(t)-b*sin(phi)*sin(t);
> > > y = center(2)+a*sin(phi)*cos(t)+b*cos(phi)*sin(t);
> > >
> > > where "center" is the centroid coordinate of a given cell
> > > "phi" is the angle between major axis of the cell and the x-axis of the image.
> > > "a and b" are lengths of the major and minor axes respectively.
> > > "t" is a parameter varying between [0, 2*pi].
> > > and "x, y" are computed Cartesian coordinates of the ellipse
> > >
> > > The ellipse-generating functions are correct. When I did "plot(x,y)", the ellipse was plotted correctly. However, when I tried to plot the ellipse on top of the original cell (image). Simply when I did:
> > >
> > > imshow(Image); hold on; plot(x,y); hold off;
> > >
> > > Something strange happened! The ellipse was flipped upside down. For example its major axis is supposed to be 30 degrees from the horizontal direction, but instead it was displayed at -30 degrees from the horizontal direction.
> > >
> > > I noticed that when I did "imshow", the YDir (Y-axis direction) under Plot Axis Properties is set to reverse, hence I think it explained why my ellipse is displayed in such a way. When I revert the YDir back to "normal" setting, it did both for the image and the ellipse - now even though my ellipse is displayed correctly, the actual image is flipped upside down.
> > >
> > > Does anyone know a fix to this problem? Well, besides manually change the "phi" value. Perhaps "plot" and "imshow" commands use different Axes settings?? Any suggestions/insights will help!!!!!!!
> >
> > Hi, I'm just doing the same thing.....i.e. fitting an ellipse on different objects by using regionprops. May I ask you if your ellipses are becoming much larger than the object itself and even not fitting in the image??
> > Here's my code (similar to yours for the ellipse drawing):
> > o = regionprops (L, 'orientation');
> > orientations = cat(1, o.Orientation);
> > phi_1 = orientations(1)* (pi/180) %convert to radians
> >
> > a = regionprops (L, 'majorAxisLength');
> > majorAxisLengths = cat(1, a.MajorAxisLength);
> > a_1 = majorAxisLengths(1)
> >
> > b = regionprops (L, 'minorAxisLength');
> > minorAxisLengths = cat(1, b.MinorAxisLength);
> > b_1 = minorAxisLengths(1)
> >
> > t= (0 : 0.01 : 2*pi);
> >
> > % Draw Ellipse
> > x_1 = center_1(1)+a_1*cos(phi_1)*cos(t)-b_1*sin(phi_1)*sin(t)
> > y_1 = center_1(2)+a_1*sin(phi_1)*cos(t)+b_1*cos(phi_1)*sin(t)
> >
> > figure
> > plot(x_1,y_1);
> >
> > figure
> > imshow (RGB); %RGB Labelled matrix containing different labelled objects
> > hold on;
> > plot(x_1,y_1);
> > hold off;
> >
> > This resulted in the following... Check the following link:
> > http://drop.io/ellipse_plot
> >
> > As you can see the image (640x480) contains only a portion of the ellipse since its values (x,y) are becoming much larger!! Any help is greatly appreciated!!!
> > Thanks
> > Kenneth
> Hi,
> I hope you solved you problem by now, but just for the sake of having it addressed here, here it's my finding: the a_1 and b_1 should be equal to *half* of majorAxisLength, and minorAxisLength respectively. So a_1 = majorAxisLength/2 and similar for b_1.
> Cheers,
> Monica

I think you are right!
In addition... I'm pretty sure that "a" & "b" are mislabeled. (That is to say that "a" is really the minor axis length)... although... given the confusion over the sign of phi, I'm not sure.

Dan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
best fitting el... Kenneth Galea 28 Dec, 2009 09:19:10
hoe to find the... SHANMUGAM 12 Dec, 2009 03:47:50
dear friends SHANMUGAM 12 Dec, 2009 03:47:48
imellipse Alex Taylor 1 Dec, 2009 17:09:08
imshow Claire 24 Nov, 2009 02:39:04
plot Claire 24 Nov, 2009 02:39:04
image processing Claire 24 Nov, 2009 02:39:03
rssFeed for this Thread

Contact us at files@mathworks.com