如何在matlab中找到轮廓轴的方向?

How to find Orientation of axis of contour in matlab?

我想找到用以下代码绘制的等高线的 OrientationMajorAxisLengthMinorAxisLength

clear
[x1 , x2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [1,3];
sigm = [2,0;0,2];
xx_size = length(mu);
tem_matrix = ones(size(x1));

x_mesh= cell(1,xx_size);
for i = 1 : xx_size
    x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {x1,x2};
temp_mesh = [];
for i = 1 : xx_size
    temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(x1));
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');

% regionprops(z_plat,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');

在我看来,我可能不得不使用 regionprops 命令,但我不知道该怎么做。我想找到轮廓轴的方向并绘制类似这样的东西

我该如何完成这个任务?非常感谢您的帮助

与其尝试处理 contour 的图形输出,我建议使用 contourc to compute the ContourMatrix and then use the x/y points to estimate the major and minor axes lengths as well as the orientation (for this I used this 文件交换提交)

这看起来像下面这样。请注意,我已将输入修改为 contourc,因为前两个输入应该是向量形式,而不是 meshgrid.

的输出
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);

% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;

% Now loop through each contour
while start < size(contourmatrix, 2)
    value = contourmatrix(1, start);
    nPoints = contourmatrix(2, start);

    contour_points = contourmatrix(:, start + (1:nPoints));

    % Now fit an ellipse using the file exchange
    ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));

    % Increment the start pointer
    start = start + nPoints + 1;
    count = count + 1;
end

orientations = [ellipsedata.phi];
%   0   0   0   

major_length = [ellipsedata.long_axis];
%   4.7175    3.3380    2.1539

minor_length = [ellipsedata.short_axis];
%   4.7172    3.3378    2.1532

如您所见,轮廓实际上基本上是圆形,因此方向为零,长轴和短轴长度几乎相等。它们在您的 post 中看起来像椭圆的原因是因为您的 x 轴和 y 轴的缩放比例不同。要解决此问题,您可以调用 axis equal

figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
axis equal

谢谢@Suever。它帮助我实现我的想法。 我在代码中添加了一些行:

clear
[X1 , X2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [-1,0];
a = [3,2;1,4];
a = a * a';
sigm = a;
xx_size = length(mu);
tem_matrix = ones(size(X1));

x_mesh= cell(1,xx_size);
for i = 1 : xx_size
    x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {X1,X2};
temp_mesh = [];
for i = 1 : xx_size
    temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(X1));
figure;contour(X1, X2, z_plat,3, 'LineWidth', 2,'color','m');
hold on;

% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
    value = contourmatrix(1, start);
    nPoints = contourmatrix(2, start);
    contour_points = contourmatrix(:, start + (1:nPoints));
    % Now fit an ellipse using the file exchange
    ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
    % Increment the start pointer
    start = start + nPoints + 1;
    count = count + 1;
end

orientations = [ellipsedata.phi];  
major_length = [ellipsedata.long_axis];
minor_length = [ellipsedata.short_axis];
tet = orientations(1);
x1 = mu(1);
y1 = mu(2);
a = sin(tet) * sqrt(major_length(1));
b = cos(tet) * sqrt(major_length(1));
x2 = x1 + a;
y2 = y1 + b;

line([x1, x2], [y1, y2],'linewidth',2);

tet = ( pi/2 + orientations(1) );
a = sin(tet) * sqrt(minor_length(1));
b = cos(tet) * sqrt(minor_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);