轮廓对比matlab,matlab-追踪两个不同点之间的轮廓线

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

如果点离得更远,问题就无法解决了——想象五个点,四个角和一个角在中间:什么是追踪直线的“正确”方法?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值