How to rotate imellipse

조회 수: 6 (최근 30일)
erik
erik 2013년 5월 23일
답변: Tim Jackman 2018년 9월 25일
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

채택된 답변

Image Analyst
Image Analyst 2013년 5월 23일
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]);

추가 답변 (3개)

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

Matt J
Matt J 2013년 5월 23일
I believe imellipse objects are 4 DOF only :-(

erik
erik 2013년 5월 27일
Image Analyst, thank you for your contribution. I created a small gui with some sliders to move the ellipse around.
  댓글 수: 3
Image Analyst
Image Analyst 2013년 10월 27일
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 2018년 5월 10일
편집: Matt J 2018년 5월 10일
is it possible to share this code to me?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by