目录
PCL体素网格法VoxelGrid使用方法和点云效果显示
资源下载链接和参考教程在文章最后;
程序中实现最基本的滤波功能和对一个点云多次滤波的实现;
参数0.01f是1cm,调整后可以看效果;
/*20201213 by 手口一斤*/
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/visualization/cloud_viewer.h>
int
main(int argc, char** argv)
{
pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2());
pcl::PCLPointCloud2::Ptr cloud_filtered(new pcl::PCLPointCloud2());
pcl::PCDReader reader;
reader.read("table_scene_lms400.pcd", *cloud);
std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height
<< " data points (" << pcl::getFieldsList(*cloud) << ")." << std::endl;
// Create the filtering object
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud(cloud);
sor.setLeafSize(0.01f, 0.01f, 0.01f);
sor.filter(*cloud_filtered);
sor.setInputCloud(cloud_filtered);
sor.setLeafSize(0.1f, 0.1f, 0.1f);
sor.filter(*cloud_filtered);
std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height
<< " data points (" << pcl::getFieldsList(*cloud_filtered) << ")." << std::endl;
pcl::PCDWriter writer;
writer.write("table_scene_lms400_downsampled.pcd", *cloud_filtered,
Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_show(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(*cloud, *cloud_show);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered_show(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(*cloud_filtered, *cloud_filtered_show);
pcl::visualization::CloudViewer viewer("Cloud Viewer");
viewer.showCloud(cloud_filtered_show,"cloud filtered");
//viewer.showCloud(cloud_show, "cloud");
while (!viewer.wasStopped())
{
//viewer.spin(100);
}
return (0);
}
运行结果:
体素网格法VoxelGrid原理说明
VoxelGrid类在输入点云数据上创建一个三维体素网格(将体素网格看作空间中一组微小的三维长方体)。然后,在每个体素(即,3D框)中,所有存在的点都将用它们的质心近似。这种方法比用体素的中心近似它们慢一些,但它获得结果表示更精确;
适用场景和使用心得
密集点云下采样,并能保持原有的点云形状;
密度不同的点云下采样,均衡点云数据的分布密度;
下采样效率高,适合大规模点云;
作为配准、重建、形状识别、法向量求取等算法的预处理步骤。
参考和资源下载:https://pcl.readthedocs.io/projects/tutorials/en/latest/voxel_grid.html#voxelgrid