How to rotate imellipse

3 views (last 30 days)
erik
erik on 23 May 2013
Answered: Tim Jackman on 25 Sep 2018
Hello fellow Matlab users,
I'm currently using the imellipse function to draw an ellipse on an image. I would like to rotate the ellipse interactively. However I cannot get this to work. Does anyone have experience with rotating ellipses? Please share, it would help me out a lot!
With kind regards,
Erik Groot Jebbink

Accepted Answer

Image Analyst
Image Analyst on 23 May 2013
See my demo. It doesn't use imellipse() so you can't have handles to click and drag out new a size or angle. So you'd need to have a GUI with some sliders to allow the user to set new parameters for the major axis length, minor axis length, center location, and angle or orientation.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Parameterize the equation.
t = linspace(0, 360,1000);
phaseShift = 20;
xAmplitude = 2;
yAmplitude = 1;
x = xAmplitude * sind(t + phaseShift);
y = yAmplitude * cosd(t);
% Now plot the rotated ellipse.
plot(x, y, 'b-', 'LineWidth', 2);
axis equal
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Rotated Ellipses', 'FontSize', fontSize);
text(-1.75, 1.4, 'Parametric --', 'Color', 'b', 'FontSize', fontSize);
% Now plot another ellipse and multiply it by a rotation matrix.
% http://www.maa.org/joma/Volume8/Kalman/General.html
rotationAngle = 30;
transformMatrix = [cosd(rotationAngle), sind(rotationAngle);...
-sind(rotationAngle), cosd(rotationAngle)]
xAligned = xAmplitude * sind(t);
yAligned = yAmplitude * cosd(t);
xyAligned = [xAligned; yAligned]';
xyRotated = xyAligned * transformMatrix;
xRotated = xyRotated(:, 1);
yRotated = xyRotated(:, 2);
hold on;
plot(xRotated, yRotated, 'g-', 'LineWidth', 2);
% Plot a line at 30 degrees
slope = tand(30);
x1 = min(x(:));
y1 = slope * x1;
x2 = max(x(:));
y2 = slope * x2;
line([x1 x2], [y1 y2], 'Color', 'r');
text(-1.75, 1.25, 'Rotation Matrix --', 'Color', 'g', 'FontSize', fontSize);
text(-1.75, 1.1, '30 Degree Line --', 'Color', 'r', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

More Answers (3)

Tim Jackman
Tim Jackman on 25 Sep 2018
Beginning in R2018b, you can create an interactive ellipse ROI using the drawellipse function. This new ROI supports interactive rotation.

Matt J
Matt J on 23 May 2013
I believe imellipse objects are 4 DOF only :-(

erik
erik on 27 May 2013
Image Analyst, thank you for your contribution. I created a small gui with some sliders to move the ellipse around.
  3 Comments
Image Analyst
Image Analyst on 27 Oct 2013
as hz, I already pretty much gave code. All you need to do is to put it into a function called DrawEllipse which you call from the sliders. In the slider callbacks you set rotationAngle or some x and y offset/shift/translation, then call DrawEllipse passing those in.
YUKAi LIU
YUKAi LIU on 10 May 2018
Edited: Matt J on 10 May 2018
is it possible to share this code to me?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!