1

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

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

预先感谢您的任何帮助。

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

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

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

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

替代文字 http://img717.imageshack.us/img717/9814/hand1.png

4

1 回答 1

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')
于 2010-04-17T13:40:41.227 回答