From: Monica Hasegan on
"Kenneth Galea" <k.galea(a)hotmail.com> wrote in message <hhaehr$bvl$1(a)fred.mathworks.com>...
> "Clare " <flyingclare(a)gmail.com> wrote in message <heg2bl$qno$1(a)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
 | 
Pages: 1
Prev: perturbation+for loop
Next: uicontol callback