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 Center および File ExchangeLive Scripts and Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by