How to change imellipse to include theta?

1 view (last 30 days)
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.

Accepted Answer

Image Analyst
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

More Answers (1)

Walter Roberson
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.
  1 Comment
Anathea Pepperl
Anathea Pepperl on 7 Jan 2012
Thank you for your input! That is what I thought, and I am currently re-writing a imellipse class that uses imroi as the parent class (this is what I meant by "brute forcing" it). I am having problems with functions in the image/imuitools/private folder being recognized. I am guessing this is because that folder does not exist in my MATLAB path, but there exists some kind of header file so that these functions can be called from imroi and such. Is there a better work-around than just putting the new imellipse class in the toolbox/image/imuitools folder?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!