I am trying to plot a series of ellipses on Matlab. Basically I have a microscopic picture and I process it with ImageJ to obtain a series of data (area, center, major and minor axes) for each ellipse. I am trying to replot those ellipses on matlab in order to after add a gradient color to map the picture so I can tell from the ellipse in which direction the fibre is. This is my code
clearvars -except data colheaders %Clear everything but the original data
data(:,9)=data(:,9)*pi/180; %Transform my 9th colomn (rotation angle) in rad
data(:,6)=1196-data(:,6); %Recalibrate the y axis (different coordinate system)
for i=1:29 %29 ellipses to plot
theta = 0 : 0.01 : 2*pi;
x = data(i,7)/2 * cos(theta) * cos(data(i,9)) - data(i,8)/2 * sin(theta) * sin(data(i,9)) + data(i,5);
y = data(i,7)/2 * sin(theta) * cos(data(i,9)) + data(i,8)/2 * cos(theta) * sin(data(i,9)) + data(i,6);
plot(x, y, 'LineWidth', 1);
hold on
end
% Columns (5,6) are the centre (x,y) of the ellipse
% Columns (7,8) are the major and minor axes (a,b)
% Column 9 is the rotation angle with the x axis
axis equal; % Keep axis same size as sample picture
xlim([0 1592]);
ylim([0 1196]);
grid on;
I can send a picture on private, seems they don't let me upload one. But i obtain circles at the right location instead of ellipses. Are my equations right ? Best Dorian
You're actually pretty close; you made a small mistake in the definition of the x value and simply need to swap data(i,8)
with data(i,7)
.
i.e. change that line:
x = data(i,7)/2 * cos(theta) * cos(data(i,9)) - data(i,8)/2 * sin(theta) * sin(data(i,9)) + data(i,5);
for that one:
x = data(i,8)/2 * cos(theta) * cos(data(i,9)) - data(i,7)/2 * sin(theta) * sin(data(i,9)) + data(i,5);
Here is a test with dummy data:
clear
clc
%// Define dummy data
data = zeros(4,9);
data(:,5) = [1 ;2 ;3; 4];
data(:,6) = [1; 2 ;3; 4];
data(:,7) = [6 ;7;8;9];
data(:,8) = [2;3;4;5];
data(:,9) = [10;30;45;90];
data(:,9)=data(:,9)*pi/180;
%// You can put that outside the loop.
theta = 0 : 0.01 : 2*pi;
hold all
for i=1:size(data,1)
x = data(i,8)/2 * cos(theta) * cos(data(i,9)) - data(i,7)/2 * sin(theta) * sin(data(i,9)) + data(i,5);
y = data(i,7)/2 * sin(theta) * cos(data(i,9)) + data(i,8)/2 * cos(theta) * sin(data(i,9)) + data(i,6);
plot(x, y, 'LineWidth', 1);
%// NEW to add text
text(data(i,5),data(i,6),['Ellipse' num2str(i)],'Color',rand(1,3))
end
axis equal;
grid on;
Output:
yay!
EDIT:
I added a line of code to label ellipses with text. I use random colors and place the text at the center of the ellipse but you can change it as you like to match the color with that of each ellipse.