点云降采样--VoxelGrid点云降采样

1.版本要求

版本: >PCL1.0

2.简介

VoxelGrid体素降采样是PCL开源库中非常有效的点云降采样手段。VoxelGrid对点云进行体素化,创建一个三维体素栅格。在每个体素里面,求取该立方体内的所有点云重心点来代表这个立方体,从而达到降采样的目的。

3.数据

本例中使用的点云数据(test.pcd)请见百度网盘分享。
链接:https://pan.baidu.com/s/1fNgXCpRwIcYSPxL1vGvt-g
提取码:dwkc

4.代码

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/voxel_grid.h>
#include <string>
#include <sstream>

int main(int argc, char**argv)
{
     
	//读入测试点云test.pcd
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_voxel_downsample(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::io::loadPCDFile("test.pcd", *cloud);

	//创建VexelGrid体素降采样对象
	pcl::VoxelGrid<pcl::PointXYZ> vg;
	vg.setInputCloud(cloud);
	vg.setLeafSize(0.15f, 0.15f, 0.15f);  //设置体素大小为0.15m x 0.15m x 0.15m
	vg.filter(*cloud_voxel_downsample);

	//输出降采样前的点云数量和降采样后的点云数量
	std::cout << "points quantity before voxel downsample:" << cloud->size()<<std::endl
				<<"points quantity after voxel downsample:" << cloud_voxel_downsample->size() << std::endl;

	//对比显示降采样效果
	pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
	int v1(0);  //创建左窗口显示原始点云
	viewer.createViewPort(0, 0, 0.5, 1.0, v1);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v1);
	viewer.addText("Before Downsample", 2, 2, "Before Downsample", v1);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb1(cloud, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud, rgb1, "cloud before downsample", v1);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud before downsample", v1);
	viewer.addCoordinateSystem(1.0, "Before Voxel Downsample", v1);
	int v2(1);  //创建右窗口显示降采样后的点云
	viewer.createViewPort(0.5, 0, 1.0, 1.0, v2);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v2);
	viewer.addText("After Downsample", 2, 2, "After Downsample", v2);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb2(cloud_voxel_downsample, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud_voxel_downsample, rgb2, "cloud after downsample", v2);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud after downsample", v2);
	viewer.addCoordinateSystem(1.0, "After Voxel Downsample", v2);

	viewer.spin();

	return 0;
}

5.效果

左图为降采样前的点云显示,右图为降采样后的点云显示。
点云降采样--VoxelGrid点云降采样_第1张图片

你可能感兴趣的:(PCL-点云处理,自动驾驶,算法)