In this tutorial we will learn how to downsample -- that is, reduce the number of points -- a point cloud dataset, using a voxelized grid approach.
The VoxelGrid
class that we're about to present creates a 3D voxel grid (think about a voxel grid as a set of tiny 3D boxes in space) over the input point cloud data. Then, in each voxel (i.e., 3D box), all the points present will be approximated (i.e., downsampled) with their centroid. This approach is a bit slower than approximating them with the center of the voxel, but it represents the underlying surface more accurately.
First, download the dataset table_scene_lms400.pcd and save it somewhere to disk.
Then, create a file, let's say, voxel_grid.cpp
in your favorite editor, and place the following inside it:
sources/voxel_grid/voxel_grid.cpp
Now, let's break down the code piece by piece.
The following lines of code will read the point cloud data from disk.
sources/voxel_grid/voxel_grid.cpp
Then, a pcl::VoxelGrid filter is created with a leaf size of 1cm, the input data is passed, and the output is computed and stored in cloud_filtered.
sources/voxel_grid/voxel_grid.cpp
Finally, the data is written to disk for later inspection.
sources/voxel_grid/voxel_grid.cpp
Add the following lines to your CMakeLists.txt file:
sources/voxel_grid/CMakeLists.txt
After you have made the executable, you can run it. Simply do:
$ ./voxel_grid
You will see something similar to:
PointCloud before filtering: 460400 data points (x y z intensity distance sid).
PointCloud after filtering: 41049 data points (x y z intensity distance sid).