【问题标题】:Matlab - Trace contour line between two different pointsMatlab - 跟踪两个不同点之间的轮廓线
【发布时间】:2010-04-17 12:50:23
【问题描述】:

我有一组点,表示为 2 行 x n 列矩阵。 这些点构成连接的边界或边。我需要一个从起点 P1 跟踪此轮廓并在终点 P2 停止的函数。它还需要能够以顺时针或逆时针方向跟踪轮廓。我想知道这是否可以通过使用 Matlab 的一些函数来实现。

我曾尝试编写自己的函数,但这充满了错误,我也尝试使用bwtraceboundary 和索引,但这会产生问题,因为矩阵中的点不是按照创建轮廓的顺序。

提前感谢您的帮助。

顺便说一句,我已经包含了指向一组点的图的链接。它是手轮廓的一半。

该函数将理想地跟踪从红色星形到绿色三角形的轮廓。按遍历顺序返回点。

编辑:这可能是解决我要解决的更大问题的方法,但是是否可以测试蓝色边界边缘上的点是否连接到红色星星或绿色三角形之间的轮廓点。

即对于蓝色边界上的点,如果您要手动从左侧红色星号到绿色三角形跟踪轮廓,如果该点位于两点之间的连接边界上,则函数将返回 true,否则返回 false。

alt text http://img717.imageshack.us/img717/9814/hand1.png

【问题讨论】:

  • 为什么这是一个社区维基,顺便说一句?
  • 抱歉,我必须是自动驾驶仪

标签: matlab trace line traceroute contour


【解决方案1】:

如果这些点非常靠近,您应该能够通过始终查找列表中的下一个最近点来进行跟踪。

如果该点相距较远,则问题将无法解决 - 想象五个点,其中四个是角,一个在中心:追踪线的“正确”方式是什么?

%%# create some points
npts = 100;
x = linspace(-1,1,100)'; %'
y = 1 - x.^2;
pts = [x,y];

%# shuffle the points
newOrder = randperm(npts);
pts = pts(newOrder,:);

%# find index of start, end point
startIdx = find(newOrder == 1);
endIdx = find(newOrder == npts);

%# this brings us to where you are - pts as a nx2 array
%# startIdx indicates the star, and endIdx indicates the triangle.

%# pre-assign output - traceIdx, which contains the ordered indices of the point on the trace
traceIdx = NaN(npts,1);

%# create distance matrix
distances = squareform(pdist(pts));

%# eliminate zero-distance along the diagonal, b/c we don't want points linking to themselves
distances(logical(eye(npts))) = NaN;

%# starting from startIdx: always find the closest next point, store in traceIdx,
%# check whether we've arrived at the end, and repeat if we haven't
done = false;
traceCt = 1;
traceIdx(1) = startIdx;

while ~done
    %# find the index of the next, closest point
    [dummy,newIdx] = min(distances(traceIdx(traceCt),:));

    %# store new index and up the counter
    traceCt = traceCt + 1;
    traceIdx(traceCt) = newIdx;

    %# check whether we're done
    if newIdx == endIdx
        done = true;
    else
        %# mask the backward distance so that there's no turning back
        distances(newIdx,traceIdx(traceCt-1)) = NaN;
    end %# if
end %# while ~done

%# remove NaNs
traceIdx(~isfinite(traceIdx)) = [];

%# plot result with a line connecting the dots to demonstrate that everything went well.
figure,
plot(pts(traceIdx,1),pts(traceIdx,2),'-o')
hold on,
plot(pts(startIdx,1),pts(startIdx,2),'*r')
plot(pts(endIdx,1),pts(endIdx,2),'>g')

【讨论】:

  • 谢谢乔纳斯 我没有意识到这实际上可能无法解决!
  • 我不会说它无法解决,而是难以解决。
  • @Jonas - 感谢您提供您的代码 sn-p。我确实遇到了一个涉及不可回头部分的问题,而不是仅将最后一个点标记为 NaN,而是将所有先前选择的点都选择为 NaN。而不是:距离(newIdx,traceIdx(traceCt-1))= NaN;距离(:,traceIdx(1:traceCt))= NaN; (他想到了自己的头顶)顺便说一句 Jonas,你的 matlab 技能给我留下了深刻的印象!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 2014-04-09
  • 2021-06-16
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多