我在飞机上有一系列离散点,但是它们的顺序是分散的。这是一个实例:
为了用平滑的曲线连接它们,我写了一个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)< / p>
所以我想知道:
theta = linspace(0,2*pi,1000)';
boundaryPointSet= [2*sin(theta),cos(theta)];
tic;
findSmoothBoundary(boundaryPointSet)
toc;
%Elapsed time is 4.570719 seconds.
答案 0 :(得分:3)
这个答案并不完美,因为为了让它起作用,我必须做出一些假设。但是,对于绝大多数情况,它应该按预期工作。此外,根据您在评论中提供的链接,我认为这些假设至少是弱的,如果没有按照定义进行验证:
<强> 1。该点形成单个连接区域
<强> 2。点的质心位于这些点的凸包
如果尊重这些假设,您可以执行以下操作(最后提供完整代码):
第1步:计算积分的质心
Means=mean(boundaryPointSet);
第2步:更改变量以将原点设置为质心
boundaryPointSet(:,1)=boundaryPointSet(:,1)-Means(1);
boundaryPointSet(:,2)=boundaryPointSet(:,2)-Means(2);
第3步:将坐标转换为极坐标
[Angles,Radius]=cart2pol(boundaryPointSet(:,1),boundaryPointSet(:,2));
第4步:对Angle
进行排序并使用此排序对Radius
[newAngles,ids]=sort(Angles);
newRadius=Radius(ids);
Step5:返回笛卡尔坐标并重新添加质心坐标:
[X,Y]=pol2cart(newAngles,newRadius);
X=X+Means(1);
Y=Y+means(2);
%%% Find smooth boundary
fid=fopen('SmoothBoundary.txt');
A=textscan(fid,'%f %f','delimiter',',');
boundaryPointSet=cell2mat(A);
boundaryPointSet(any(isnan(boundaryPointSet),2),:)=[];
idx=randperm(size(boundaryPointSet,1));
boundaryPointSet=boundaryPointSet(idx,:);
tic
plot(boundaryPointSet(:,1),boundaryPointSet(:,2))
%% Find mean value of all parameters
Means=mean(boundaryPointSet);
%% Center values around Mean point
boundaryPointSet(:,1)=boundaryPointSet(:,1)-Means(1);
boundaryPointSet(:,2)=boundaryPointSet(:,2)-Means(2);
%% Get polar coordinates of your points
[Angles,Radius]=cart2pol(boundaryPointSet(:,1),boundaryPointSet(:,2));
[newAngles,ids]=sort(Angles);
newRadius=Radius(ids);
[X,Y]=pol2cart(newAngles,newRadius);
X=X+Means(1);
Y=Y+means(2);
toc
figure
plot(X,Y);
注意:由于您的值已经在输入文件中排序,我不得不通过排列它们来搞砸它
边界
经过的时间是0.131808秒。
消息输入:
输出