在Matlab上绘制旋转椭圆

时间:2015-03-06 15:37:26

标签: matlab ellipse drawellipse

我试图在Matlab上绘制一系列省略号。基本上我有一个显微图片,我用ImageJ处理它,以获得每个椭圆的一系列数据(区域,中心,长轴和短轴)。我试图在matlab上重新绘制这些椭圆,以便在添加渐变颜色后映射图片,这样我就可以从椭圆中看出光纤的方向。这是我的代码

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;

我可以私下发送图片,好像他们不让我上传一张。但我在正确的位置而不是椭圆获得圆圈。 我的方程式对吗? 最好 道连

1 个答案:

答案 0 :(得分:1)

你实际上非常接近;你在x值的定义中犯了一个小错误,只需要将data(i,8)data(i,7)交换。

即。改变这一行:

x = data(i,7)/2 * cos(theta) * cos(data(i,9)) - data(i,8)/2 * sin(theta) * sin(data(i,9)) + data(i,5);

对于那个:

x = data(i,8)/2 * cos(theta) * cos(data(i,9)) - data(i,7)/2 * sin(theta) * sin(data(i,9)) + data(i,5);

这是一个虚拟数据的测试:

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;

输出:

enter image description here

耶!

编辑:

我添加了一行代码来标记带有文本的省略号。我使用随机颜色并将文本放在椭圆的中心,但您可以根据需要更改它以使颜色与每个椭圆的颜色相匹配。

相关问题