Zaid Hassan
Zaid Hassan

Reputation: 1

Drawing an ellipse into a matrix image using Matlab

I to Draw an ellipse into a matrix image to build a shepp logan phantom,It is a collection of elliopsides,just i want to draw one ellipse and convert it to get matrix image

Upvotes: 0

Views: 6567

Answers (2)

Ludecan
Ludecan

Reputation: 331

Hey I think there's a problem with the solution that's been given. The rotation is being applied after the translation is done which causes some wierd results (if you take a look at it, the drawn ellipse's center is not (10, 20)) since the rotation is defined with center at 0.

I believe the right answer would be:

theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;

% the ellipse in x and y coordinates centered at 0
ellipse_x_r = a*cos( theta_grid );
ellipse_y_r = b*sin( theta_grid );

% Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];
n = length(ellipse_x_r);

% let's rotate the ellipse to some angle phii and then translate it to (X0, Y0)
r_ellipse = R * [ellipse_x_r; ellipse_y_r] + repmat([X0; Y0], [1, n]);

plot(r_ellipse(1,:),r_ellipse(2,:),'-x')

Thanks for the solution though I couldn't find how to do this anywhere and this was the most helpful post I found.

Cheers!
Pablo

Edit: heh, there's a bug with SO's code formatter. A ' inside a comment is not a string =)

Upvotes: 1

bla
bla

Reputation: 26069

If you have the PDE toolbox you can use pdeellip . Otherwise you can just write:

% input ellipse parameters
theta_grid = linspace(0,2*pi);
phi = 45*180/pi;
X0=10;
Y0=20;
a=40;
b=15;

% the ellipse in x and y coordinates 
ellipse_x_r  = X0 + a*cos( theta_grid );
ellipse_y_r  = Y0 + b*sin( theta_grid );

%Define a rotation matrix
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ];

%let's rotate the ellipse to some angle phii
r_ellipse = R * [ellipse_x_r;ellipse_y_r];

plot(r_ellipse(1,:),r_ellipse(2,:),'-x')

enter image description here

Here's another option that instead of x-y coordinates, "stamps" an ellipse into an array:

a=20;
b=9;
phi=45;
[x y] = meshgrid(-50:50,-50:50);
el=((x-X0)/a).^2+((y-Y0)/b).^2<=1;
imagesc(imrotate(el,phi)); colormap(bone) 

enter image description here

Upvotes: 4

Related Questions