基于matlab点云工具箱对点云进行处理一:去除地面,保留剩下的点
基于matlab点云工具箱的点云地面分割(去除地面,保留剩下的点)
步骤:
- 读取velodyne数据包pcap文件内的点云数据
- 使用pcdownsample函数对点云数据进行体素化采样,减少点云数量
- 使用find函数对点云进行筛选
- 使用pcdnoise去除点云内的噪声
- 筛选低点的点云(地面一般较低)
- 使用pcfitplane函数匹配平面(地面)
- 输出
相关文件点我下载https://download.csdn.net/download/rmrgjxeivt/59553218
基于matlab点云工具箱对点云进行处理一:去除地面,保留剩下的点https://blog.csdn.net/rmrgjxeivt/article/details/121830344
基于matlab点云工具箱对点云进行处理二:对点云进行欧式聚类,获得聚类后点云簇的外接矩形https://blog.csdn.net/rmrgjxeivt/article/details/121830919
基于matlab点云工具箱对点云进行处理三:对点云进行欧式聚类,使用三角剖分处理后获取点云簇的外接凸多边形https://blog.csdn.net/rmrgjxeivt/article/details/121831507
基于matlab点云工具箱对点云进行处理四:对点云进行欧式聚类,并获得包围点云簇的外接凹多边形https://blog.csdn.net/rmrgjxeivt/article/details/121831934
% 读取激光的PCAP文件
% 筛选感兴趣区域
% 播放筛选后的点云
veloReader = velodyneFileReader('2021-11-23-12-49-43_Velodyne-HDL-32-Data.pcap','VLP32c');
%% 设置感兴趣区域
vehPara.length = 5.5;
vehPara.width = 2.2;
vehPara.d = 2.3; % 轴距
vehPara.rearOverhang = 1; % 前悬
vehPara.rearOverhang = 1; % 后悬
vehPara.CG2Rear = 1.45; % 质心到后轴
insRegion = [-20 50 -10 10 0 2]; % 感兴趣区域[minX maxX minY maxY]
groundRegion = [-1, 0.2]; % 地面区域,z轴方向
xLimits = [insRegion(1), insRegion(2)];
yLimits = [insRegion(3), insRegion(4)];
zLimits = [insRegion(5), insRegion(6)]; % 原点在后轴中心,因此此处相对于轮芯高度
player = pcplayer(xLimits,yLimits,zLimits);
xlabel(player.Axes,'X (m)');
ylabel(player.Axes,'Y (m)');
zlabel(player.Axes,'Z (m)');
veloReader.CurrentTime = veloReader.StartTime + seconds(0.3);
disp(['frame数量',num2str(veloReader.NumberOfFrames)])
pause(2)
frameID = 2700;
while(hasFrame(veloReader) && player.isOpen() && (veloReader.CurrentTime < veloReader.EndTime))
ptCloudObj = readFrame(veloReader,frameID);
frameID
tic
lidarLo = [3.5 0 1.1 0 0 0];
% 取出XYZ
xTemp = ptCloudObj.Location(:,:,2)+lidarLo(1);
yTemp = -ptCloudObj.Location(:,:,1)+lidarLo(2);
zTemp = ptCloudObj.Location(:,:,3)+lidarLo(3);
pc = [xTemp(:) yTemp(:) zTemp(:) single(ptCloudObj.Intensity(:))];
% 对地面的点进行范围筛选
zMin = groundRegion(1);
zMax = groundRegion(2);
pcObj = pointCloud(pc(:,1:3));
pcObj.Intensity = pc(:,4);
pcOutNum = 30000; % 输出的点云数量
objPointVeh = zeros(pcOutNum,4,'single');
objPointVeh(:,1) = single(insRegion(2));
objPointVeh(:,2) = single(insRegion(4));
objPointVeh(:,3) = single(insRegion(6));
objPointVeh(:,4) = single(0);
% tic
%% 降低点云密度 coder会报错
% gridStep = 0.05;
% pcObj_downSample = pcdownsample(pcObj,'gridAverage',gridStep); % 降低点云密度
maxNumPoints = 6;
pcObj_downSample = pcdownsample(pcObj,'nonuniformGridSample',maxNumPoints);
% percentage = 0.3;
% pcObj_downSample = pcdownsample(pcObj,'random',percentage);
%% 筛选感兴趣区域(单位米),并排除车身内部的点云
xLimits = [insRegion(1), insRegion(2)];
yLimits = [insRegion(3), insRegion(4)];
zLimits = [insRegion(5), insRegion(6)]; % 原点在后轴中心,因此此处相对于轮芯高度
indices = find((pcObj_downSample.Location(:, 2) >= yLimits(1) ...
& pcObj_downSample.Location(:,2) <= yLimits(2) ...
& pcObj_downSample.Location(:,1) >= xLimits(1) ...
& pcObj_downSample.Location(:,1) <= xLimits(2) ...
& pcObj_downSample.Location(:,3) <= zLimits(2) ...
& pcObj_downSample.Location(:,3) >= zLimits(1) ...
& ~(pcObj_downSample.Location(:,1)<(vehPara.length-vehPara.rearOverhang) ...
& pcObj_downSample.Location(:,1)>(-vehPara.rearOverhang) ...
& pcObj_downSample.Location(:,2)<vehPara.width/2 ...
& pcObj_downSample.Location(:,2)>-vehPara.width/2)));% 设置感兴趣的点云区域
if ~isempty(indices)
pcObj_downSample = select(pcObj_downSample,indices);
%% 去除噪声
[pcObj_downSample,inlierIndices,~] = pcdenoise(pcObj_downSample);
pcID_noNoise = 1:1:pcObj_downSample.Count;
if ~isempty(inlierIndices)
% 分离出地面
% Set the maximum point-to-plane distance (2cm) for plane fitting.
maxDistance = 0.2;
% Set the normal vector of the plane.
referenceVector = [0,0,1];
% Set the maximum angular distance to 5 degrees.
maxAngularDistance = 10;
% 筛选出较低的点用于去除地面(地面较低)
% 只对范围内的点进行平面拟合(很低的那一部分点)
indices = find((pcObj_downSample.Location(:, 3) >= zMin ...
& pcObj_downSample.Location(:, 3) <= zMax));
if ~isempty(indices)
pcObj_lier = select(pcObj_downSample,indices);
pcID_lier = pcID_noNoise(indices);
% Detect the first plane, the table, and extract it from the point cloud.
% 输出不是地面的点
[~,inlierIndices,outlierIndices] = pcfitplane(pcObj_lier,maxDistance,referenceVector,maxAngularDistance);
pcID_outlier = pcID_lier(outlierIndices); % 不是地面的点的ID
pcID_inlier = pcID_lier(inlierIndices); % 是地面的点的ID
% 从点云中删除属于地面的点,获得过滤后的点云
pcID_out = pcID_noNoise;
pcID_out(pcID_inlier) = [];
% outlierIndices = [];
if ~isempty(outlierIndices) % 非空才输出
pcRemainObj = select(pcObj_downSample,pcID_out);
else
pcRemainObj = pcObj_downSample;
end
else
pcRemainObj = pcObj_downSample;
end
cowPCRemain = size(pcRemainObj.Location)*[1;0];
if cowPCRemain>pcOutNum
cowPCRemain = pcOutNum;
end
objPointVeh(1:cowPCRemain,:) = [pcRemainObj.Location pcRemainObj.Intensity];
% pcRemainObj = pcObj;
% cowPCRemain = size(pcRemainObj.Location)*[1;0];
% objPointVeh(1:cowPCRemain,:) = pcRemainObj.Location;
end
end
% figure(2)
% % pcshow(plane1)
% pcshow(pcPlanel)
% title('First Plane')
% cowPCRemain = length(pcObj.Location(:,1));
% pcRemain(1:cowPCRemain,:) = pcObj.Location;
% figure(3)
% % pcshow(plane1)
% pcshow(pcRemain)
% title('remainPtCloud')
objVehPoint = objPointVeh;
%%
pcObjOut = pointCloud(objVehPoint(:,1:3));
pcObjOut.Intensity = objVehPoint(:,4);
frameID = frameID+1;
toc
view(player,pcObjOut);
pause(0.02);
end