【问题标题】:Finding 2D area defined by contour lines in Matlab在Matlab中查找由轮廓线定义的二维区域
【发布时间】:2013-12-01 04:22:59
【问题描述】:

我在计算 Matlab 中的核密度估计 (KDE) 生成的二维轮廓区域时遇到了困难。我有三个变量:

X 和 Y = 计算变量“密度”的网格网格 (256x256) 密度 = 从 KDE 计算的密度 (256x256)

我运行代码

轮廓(X,Y,密度,10)

这会产生附加的情节。对于 10 个轮廓级别中的每一个,我想计算面积。我已经在 R 等其他一些平台上做到了这一点,但在 Matlab 中找出正确的方法/语法时遇到了麻烦。

C =轮廓c(密度)

我相信上面的行会存储轮廓的所有值,让我可以计算面积,但我不完全了解这些值是如何存储的,也不知道如何正确获取它们。

任何帮助或建议将不胜感激!非常感谢。

【问题讨论】:

    标签: matlab area contour kde


    【解决方案1】:

    这个小脚本会帮助你。一般为contour。可能也为contour3 和contourf 工作,当然要进行调整。

    [X,Y,Z] = peaks;   %example data
    % specify certain levels
    clevels = [1 2 3];
    C = contour(X,Y,Z,clevels);
    xdata = C(1,:);   %not really useful, in most cases delimters are not clear
    ydata = C(2,:);   %therefore further steps to determine the actual curves:
    
    %find curves
    n(1) = 1;         %n: indices where the certain curves start
    d(1) = ydata(1);  %d: distance to the next index
    ii = 1;
    while true
    
           n(ii+1) = n(ii)+d(ii)+1;    %calculate index of next startpoint
    
           if n(ii+1) > numel(xdata)   %breaking condition
               n(end) = [];            %delete breaking point
               break
           end
    
           d(ii+1) = ydata(n(ii+1));   %get next distance
           ii = ii+1; 
    end
    
    %which contourlevel to calculate?
    value = 2;             %must be member of clevels
    sel = find(ismember(xdata(n),value));
    idx = n(sel);          %indices belonging to choice
    L = ydata( n(sel) );   %length of curve array
    
    % calculate area and plot all contours of the same level
    for ii = 1:numel(idx)
        x{ii} = xdata(idx(ii)+1:idx(ii)+L(ii));
        y{ii} = ydata(idx(ii)+1:idx(ii)+L(ii));
    
        figure(ii)
        patch(x{ii},y{ii},'red');            %just for displaying purposes
        %partial areas of all contours of the same plot
        areas(ii) = polyarea(x{ii},y{ii});   
    
    end
    
    % calculate total area of all contours of same level
    totalarea = sum(areas)
    

    示例: 峰(通过 Matlab)

    Level value=2 是绿色轮廓,第一个循环获取所有轮廓线,第二个循环计算所有绿色多边形的面积。最后总结一下。


    如果您想获得所有级别的所有总面积,我宁愿编写一些小函数,而不是使用另一个循环。您还可以考虑,仅绘制每次计算所需的级别。这样contourmatrix 会更容易,您可以简化流程。如果您没有多个形状,我只需使用标量指定级别并使用contour 获取仅此级别的C,删除xdata 和ydata 的第一个值并直接计算带有polyarea的区域

    【讨论】:

      【解决方案2】:

      这是我发布的关于 usage of Matlab contour(...) function 的类似问题。

      主要思想是正确操作返回变量。在你的例子中

      c = contour(X,Y,density,10)

      变量 c 可以返回并用于对等值线的任何计算,包括面积。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多