【问题标题】:How to find Orientation of axis of contour in matlab?如何在matlab中找到轮廓轴的方向?
【发布时间】:2017-01-09 22:16:39
【问题描述】:

我想找到下面代码绘制的轮廓的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 的输出(轮廓矩阵本身)来确定这些东西?
  • @Suever 我可以从 sigma 中找到分布等高线的方向并用这个方向绘制 2 线吗? (对于我的任务,行的长度并不重要)

标签: matlab contour


【解决方案1】:

与其尝试处理contour 的图形输出,不如建议使用contourc 来计算ContourMatrix,然后使用x/y 点来估计长轴和短轴长度以及方向(为此我使用了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

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

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

【讨论】:

    【解决方案2】:

    谢谢@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);
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      相关资源
      最近更新 更多