4

Matlab の等高線関数 (および imcontour) は、行列のさまざまなレベルの等値線をプロットします。知りたいのですが、この関数の出力を操作して、各輪郭のすべての (x,y) 座標とレベルを取得するにはどうすればよいですか? 前述のタスクを達成するために、出力 [C,h] = Contour(...) をどのように使用できますか? また、連続関数である基になるグリッドの操作には興味がなく、プロットに表示される関連ピクセルのみを抽出します

4

1 に答える 1

5

この機能を使用できます。関数の出力を受け取り、contour構造体配列を出力として返します。配列内の各構造体は、1 つの等高線を表します。構造体にはフィールドがあります

  • v、等高線の値
  • x、等高線上の点の x 座標
  • y、等高線上の点の y 座標

    関数 s = getcontourlines(c)

    sz = size(c,2);     % Size of the contour matrix c
    ii = 1;             % Index to keep track of current location
    jj = 1;             % Counter to keep track of # of contour lines
    
    while ii < sz       % While we haven't exhausted the array
        n = c(2,ii);    % How many points in this contour?
        s(jj).v = c(1,ii);        % Value of the contour
        s(jj).x = c(1,ii+1:ii+n); % X coordinates
        s(jj).y = c(2,ii+1:ii+n); % Y coordinates
        ii = ii + n + 1;          % Skip ahead to next contour line
        jj = jj + 1;              % Increment number of contours
    end
    

    終わり

次のように使用できます。

>> [x,y] = ndgrid(linspace(-3,3,10));
>> z = exp(-x.^2 -y.^2);
>> c = contour(z);
>> s = getcontourlines(c);
>> plot(s(1).x, s(1).y, 'b', s(4).x, s(4).y, 'r', s(9).x, s(9).y, 'g')

これにより、次のプロットが得られます。

ここに画像の説明を入力

于 2013-03-08T19:36:29.153 に答える