How to change imellipse to include theta?
2 views (last 30 days)
Show older comments
Is there a way to hack imellipse such that I can draw an ellipse and then change the angle so that major and minor axes do not just lie on the x and y axes?
I'm currently "brute forcing" it but I was wondering if anyone knows of an easier way.
0 Comments
Accepted Answer
Image Analyst
on 7 Jan 2012
Anathea: Try this demo and see if it does what you want:
% Demo to rotate an ellipse over an image.
% By ImageAnalyst
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
axis on;
hEllipse = imellipse(gca,[70 60 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
disp(hEllipse);
xy = hEllipse.getVertices()
% Clear lines from axes
axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
hold on;
x = xy(:,1);
y = xy(:,2);
xCenter = mean(x);
yCenter = mean(y);
plot(x, y);
x = x - xCenter;
y = y - yCenter;
xy = [x y]
for theta = 0:22.5:180
rotationArray = [cosd(theta) sind(theta); -sind(theta) cosd(theta)];
rotated_xy = xy * rotationArray;
x = rotated_xy(:,1) + xCenter;
y = rotated_xy(:,2) + yCenter;
plot(x, y);
promptMessage = sprintf('Rotated by %.1f degrees.\nDo you want to Continue processing,\nor Cancel to abort processing?', theta);
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
end
0 Comments
More Answers (1)
Walter Roberson
on 6 Jan 2012
No, you cannot do that, or at least not without re-parenting against imroi instead of imrect and re-doing a number of functions.
The current implementation mirrors the rectangle() function, which can create ellipses that are aligned with the axes but not tilted ones.
You could possibly parent the imellipse against an hgtransform and set the transformation matrix to do the rotation, but I think that might encounter some difficulties in (e.g.) resizing the ellipse.
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!