登录论坛

查看完整版本 : Matlab-在两个不同点之间绘制轮廓线


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

我试图编写自己的函数,但此过程充满了错误,并且还尝试了使用bwtraceboundary和索引,但是由于矩阵中的点不按创建轮廓的顺序,因此结果很成问题。

预先感谢您的任何帮助。

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

理想情况下,该函数将轮廓描绘为从红色星到绿色三角形。按遍历顺序返回点。

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

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

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



回答:

如果这些点之间的距离非常近,那么您应该可以通过始终在列表中寻找下一个最近的点来进行跟踪。

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

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

更多&回答... (https://stackoverflow.com/questions/2658390)