Use VoxelGrid filter to achieve point cloud sparse

VoxelGrid filter

Use the voxel grid method to achieve downsampling, that is, reduce the number of points, reduce the point cloud data, and save the shape features of the point cloud at the same time. It is very practical in improving the speed of algorithms such as registration, surface reconstruction, and shape recognition. PCL is the realization The VoxelGrid class creates a three-dimensional voxel grid through the input point cloud data, and uses the center of gravity of all points in each voxel to approximate other points in the voxel, so that all points in the voxel use a The center of gravity points finally represent, for all voxels processed to obtain a filtered point cloud, this method is slower than the method of approximating with voxel center, but it is more accurate for the representation of the surface corresponding to the sample points.
The VoxelGrid filter uses a voxel grid method to achieve downsampling and maintains the shape characteristics of the point cloud;

  1. Specifically: The voxelGrid class creates a three-dimensional voxel grid in the point cloud data, and then uses the center of gravity of each voxel to approximate other points in the voxel.
  2. Evaluation: This method is slower than the method of approximating the voxel center, but it represents the surface corresponding to the sampling point more accurately;

Code

/*
 * @Description: 2VoxelGrid滤波器对点云进行下采样    https://www.cnblogs.com/li-yao7758258/p/6445831.html
 * @Author: HCQ
 * @Company(School): UCAS
 * @Email: [email protected]
 * @Date: 2020-10-20 10:50:23
 * @LastEditTime: 2020-10-20 11:00:48
 * @FilePath: /pcl-learning/09filters滤波/2VoxelGrid滤波器对点云进行下采样/voxel_grid.cpp
 */
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.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);    //读取点云到cloud中

  std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height 
       << " data points (" << pcl::getFieldsList (*cloud) << ").";

  /******************************************************************************
  创建一个voxel叶大小为1cm的pcl::VoxelGrid滤波器,
**********************************************************************************/
  pcl::VoxelGrid<pcl::PCLPointCloud2> sor;  //创建滤波对象
  sor.setInputCloud (cloud);            //设置需要过滤的点云给滤波对象
  sor.setLeafSize (0.01f, 0.01f, 0.01f);  //设置滤波时创建的体素体积为1cm的立方体
  sor.filter (*cloud_filtered);           //执行滤波处理,存储输出

  std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height 
       << " data points (" << pcl::getFieldsList (*cloud_filtered) << ").";

  pcl::PCDWriter writer;
  writer.write ("../table_scene_lms400_downsampled.pcd", *cloud_filtered, 
         Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), false);

  return (0);
}

Effect

The visualization results of the original point cloud and the filtered point cloud can be clearly seen that the density and order of the points are different. Although the amount of data after processing is greatly reduced, it is obvious that the shape features and spatial structure information contained are different from those of the original point cloud. The original point cloud is about the same.
The red point is the point cloud data after downsampling
insert image description here

reference

https://www.yuque.com/huangzhongqing/pcl/ai96k5#Pj8Fd

おすすめ

転載: blog.csdn.net/weixin_42990464/article/details/131239643