MATLAB: How to change imellipse to include theta

image processingimellipseimrect

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.

Best Answer

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