从Matlab轮廓函数中选择等值线 [英] Choosing isolines from Matlab contour function

查看:75
本文介绍了从Matlab轮廓函数中选择等值线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab轮廓函数(和等高线)绘制了矩阵不同级别的等值线. 我想知道:如何接收该轮廓的所有(x,y)坐标以及水平,如何操纵此函数的输出? 如何使用输出[C,h] = Silhouette(...)来完成上述任务?另外,我对操作底层网格(这是一个连续函数)不感兴趣,仅提取我在绘图上看到的相关像素.

The Matlab contour function (and imcontour) plots isolines of different levels of a matrix. I would like to know: How can I manipulate the output of this function in order to receive all the (x,y) coordinates of each contour, along with the level? How can I use the output [C,h] = contour(...) to achieve the aforementioned task? Also, I am not interested in manipulating the underlying grid, which is a continuous function, only extracting the relevant pixels which I see on the plot.

推荐答案

您可以使用此功能.它获取contour函数的输出,并返回一个struct数组作为输出.数组中的每个结构代表一条轮廓线.结构包含字段

You can use this function. It takes the output of the contour function, and returns a struct array as output. Each struct in the array represents one contour line. The struct has fields

  • v,等高线的值
  • x,等高线上的点的x坐标
  • y,等高线上的点的y坐标

  • v, the value of the contour line
  • x, the x coordinates of the points on the contour line
  • y, the y coordinates of the points on the contour line

函数s = getcontourlines(c)

function 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')

将给出以下情节:

这篇关于从Matlab轮廓函数中选择等值线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆