【问题标题】:Draw Rotated Ellipses on Matlab在 Matlab 上绘制旋转椭圆
【发布时间】:2015-05-08 06:14:39
【问题描述】:

我正在尝试在 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;

我可以私发图片,好像他们不让我上传。但是我在正确的位置获得了圆圈而不是椭圆。 我的方程式对吗? 最好的 多里安

【问题讨论】:

  • 您遇到了什么具体问题?

标签: matlab ellipse drawellipse


【解决方案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;

输出:

耶!

编辑:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多