Matlab - 跟踪两个不同点之间的轮廓线

时间:2010-04-17 12:50:24

标签: matlab trace line traceroute contour

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

我已经尝试编写自己的函数但是这里充满了bug并且我也尝试使用bwtraceboundary并编制索引但是这会产生问题,因为矩阵中的点不是创建轮廓的顺序

提前感谢您的帮助。

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

理想情况下,该功能可以跟踪从红色星形到红色星形到绿色三角形的轮廓。按遍历顺序返回点数。

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

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

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

1 个答案:

答案 0 :(得分:2)

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

如果这一点相距甚远,问题将无法解决 - 想象一下五个角落,其中四个是角落,一个是中心:跟踪线路的“正确”方式是什么?

%%# 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')