%%均匀网格法
clc
clear
close all
P = load('bun000.asc');
% figure;
x=P(1:1:end,1);
y=P(1:1:end,2);
z=P(1:1:end,3);
c=z+1;
scatter3(x,y,z,1.2,c,'filled');
colorbar
view(2)
title('原始数据')
X = P(1,:);
Y = P(2,:);
Z = P(3,:);
step=0.002; % 包围盒尺寸,控制精简率的唯一参数
% std_th = 100;
tic;
Xmin=min(X);Xmax=max(X);
Ymin=min(Y);Ymax=max(Y);
Zmin=min(Z);Zmax=max(Z);
jingjian = zeros(size(P));% 预定义
ite = 0;
for ix=Xmin:step:Xmax
xindex=find((X>=ix&X<(ix+step)));
if isempty(xindex)==1
continue
end
Xselect=X(xindex);
Yselect=Y(xindex);
Zselect=Z(xindex);
for iy=Ymin:step:Ymax
yindex=find(Yselect>=iy&Yselect<(iy+step));
if isempty(yindex)==1
continue
end
Xslt=Xselect(yindex);
Yslt=Yselect(yindex);
Zslt=Zselect(yindex);
for iz=Zmin:step:Zmax
zindex=find(Zslt>=iz&Zslt<(iz+step));
if isempty(zindex)==1
continue
end
Xsel=Xslt(zindex);
Ysel=Yslt(zindex);
Zsel=Zslt(zindex);
if length(zindex)==1
ite = ite + 1;
jingjian(ite,:) = [Xsel;Ysel;Zsel];
continue
end
Distance=sqrt((Xsel-mean(Xsel)).^2+(Ysel-mean(Ysel)).^2+(Zsel-mean(Zsel)).^2);
index=find(Distance==min(Distance));
index = index(1);
ite = ite + 1;
jingjian(ite,:) = [Xsel(index);Ysel(index);Zsel(index)];
end
end
end
jingjian(:,ite+1:end) = [];% 删除矩阵jingjian中的空位置
figure;
x=jingjian(:,1);
y=jingjian(:,2);
z=jingjian(:,3);
% x=jingjian(1,:);
% y=jingjian(2,:);
% z=jingjian(3,:);
c=z+1;
scatter3(x,y,z,1.2,c,'filled');
colorbar
view(2)
title('精简后的点云');
toc;
disp(['精简率为:',num2str((size(P,2)-size(jingjian,2))/size(P,2)*100),'%']);
评论2