【发布时间】:2016-09-21 02:15:04
【问题描述】:
我有一个平面上的一系列离散点,但是,它们的顺序是分散的。这是一个例子:
为了用平滑的曲线将它们连接起来,我写了一个findSmoothBoundary()来实现平滑的边界。
代码
function findSmoothBoundary(boundaryPointSet)
%initialize the current point
currentP = boundaryPointSet(1,:);
%Create a space smoothPointsSet to store the point
smoothPointsSet = NaN*ones(length(boundaryPointSet),2);
%delete the current point from the boundaryPointSet
boundaryPointSet(1,:) = [];
ptsNum = 1; %record the number of smoothPointsSet
smoothPointsSet(ptsNum,:) = currentP;
while ~isempty(boundaryPointSet)
%ultilize the built-in knnsearch() to
%achieve the nearest point of current point
nearestPidx = knnsearch(boundaryPointSet,currentP);
currentP = boundaryPointSet(nearestPidx,:);
ptsNum = ptsNum + 1;
smoothPointsSet(ptsNum,:) = currentP;
%delete the nearest point from boundaryPointSet
boundaryPointSet(nearestPidx,:) = [];
end
%visualize the smooth boundary
plot(smoothPointsSet(:,1),smoothPointsSet(:,2))
axis equal
end
虽然findSmoothBoundary()可以正确找到平滑边界,但是效率却低(关于数据,请看here)
所以我想知道:
- 如何高效求离散点阶?
数据
theta = linspace(0,2*pi,1000)';
boundaryPointSet= [2*sin(theta),cos(theta)];
tic;
findSmoothBoundary(boundaryPointSet)
toc;
%Elapsed time is 4.570719 seconds.
【问题讨论】:
-
你的身材能有多奇怪?我可以假设质心包含在其中吗? (如果是,我有一个计算时间约为 0.2 秒的答案)
-
@BillBokeey 这个边界来自一系列部分。喜欢this
标签: performance matlab nearest-neighbor boundary