点云降采样--VoxelGrid和ApproximateVoxelGrid降采样区别

1.版本要求

版本: >PCL1.0

2.简介

VoxelGrid体素降采样和ApproximateVoxelGrid体素降采样是PCL开源库中非常有效的点云降采样手段。
两者的区别在于:
VoxelGrid以体素栅格重心来代表这个立方体,下采样速度相比ApproximateVoxelGrid慢一些,但更好地保留了原始点云的局部形态特征。
ApproximateVoxelGrid以体素栅格中心点来代表这个立方体,下采样速度相比VoxelGrid快一些,但也损失了更多原始点云的局部形态特征。
ApproximateVoxelGrid降采样后点云数量比VoxelGrid降采样后点云数量多,造成这种结果的原因是ApproximateVoxelGrid是依据每一个体素的中心点来获取点云的,并不是依赖每个体素里面是否存在点云。

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 <pcl/filters/approximate_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_VoxelGrid_downsample(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ApproximateVoxelGrid_downsample(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::io::loadPCDFile("test.pcd", *cloud);

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

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

	//对比显示两种体素降采样效果
	pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
	int v1(0);  //创建左窗口显示VoxelGrid降采样结果
	viewer.createViewPort(0, 0, 0.5, 1.0, v1);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v1);
	viewer.addText("VoxelGrid Downsample", 2, 2, "VoxelGrid Downsample", v1);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb1(cloud_VoxelGrid_downsample, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud_VoxelGrid_downsample, rgb1, "VoxelGrid downsample", v1);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "VoxelGrid downsample", v1);
	viewer.addCoordinateSystem(1.0, "VoxelGrid downsample", v1);
	int v2(1);  //创建右窗口显示ApproximateVoxelGrid降采样结果
	viewer.createViewPort(0.5, 0, 1.0, 1.0, v2);  //左右窗口大小划分,1:1
	viewer.setBackgroundColor(0, 0, 0, v2);
	viewer.addText("ApproximateVoxelGrid Downsample", 2, 2, "ApproximateVoxelGrid Downsample", v2);
	pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> rgb2(cloud_ApproximateVoxelGrid_downsample, "z");
	viewer.addPointCloud<pcl::PointXYZ>(cloud_ApproximateVoxelGrid_downsample, rgb2, "ApproximateVoxelGrid downsample", v2);
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "ApproximateVoxelGrid downsample", v2);
	viewer.addCoordinateSystem(1.0, "ApproximateVoxelGrid downsample", v2);

	viewer.spin();

	return 0;
}

5.效果

左图为VoxelGrid降采样结果,右图为ApproximateVoxelGrid降采结果。
点云降采样--VoxelGrid和ApproximateVoxelGrid降采样区别_第1张图片
原始点云数量,ApproximateVoxelGrid降采样后点云数量,VoxelGrid降采样后点云数量。可以看到ApproximateVoxelGrid降采样后点云数量比VoxelGrid降采样后点云数量多。
点云降采样--VoxelGrid和ApproximateVoxelGrid降采样区别_第2张图片

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