Downsampling 3D Point Clouds with a Voxelized Grid¶
In this tutorial we will learn how to down sample / reduce the number of points in a 3D point cloud, using a voxelized grid approach.
Note
This tutorial can be run both inside and outside a Docker* image. We assume that the pcl-oneapi-tutorial Deb package has been installed, and the user has copied the tutorial directory from /opt/intel/pcl/oneapi/tutorials/ to a user-writable directory.
Prepare the environment:
oneapi_voxel_grid.cpp
should be in the directory with following content:1#include <pcl/oneapi/filters/voxel_grid.h> 2#include <pcl/io/pcd_io.h> 3#include <pcl/point_types.h> 4#include <pcl/point_cloud.h> 5 6 7int main (int argc, char** argv) 8{ 9 std::cout << "Running on device: " << dpct::get_default_queue().get_device().get_info<sycl::info::device::name>() << "\n"; 10 11 // Read Point Cloud 12 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_( new pcl::PointCloud<pcl::PointXYZ>() ); 13 int result = pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud_); 14 if (result != 0) 15 { 16 pcl::console::print_info ("Load pcd file failed.\n"); 17 return result; 18 } 19 20 // Prepare Point Cloud Memory (output) 21 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered_( new pcl::PointCloud<pcl::PointXYZ>() ); 22 23 // GPU calculate 24 pcl::oneapi::VoxelGrid<pcl::PointXYZ> vg_oneapi; 25 vg_oneapi.setInputCloud(cloud_); 26 float leafsize= 0.005f; 27 vg_oneapi.setLeafSize (leafsize, leafsize, leafsize); 28 vg_oneapi.filter(*cloud_filtered_); 29 30 // print log 31 std::cout << "[oneapi voxel grid] PointCloud before filtering: " << cloud_->size() << std::endl; 32 std::cout << "[oneapi voxel grid] PointCloud after filtering: " << cloud_filtered_->size() << std::endl; 33}
Source the Intel® oneAPI Base Toolkit environment:
(Optional) Setup proxy setting to download test data:
Build the code:
Run the binary:
Expected results example:
Code Explanation¶
Now, let’s explain the code in detail.
We first load the example PCD into a PointCloud<PointXYZ>.
Then prepare output buffer for filtered result.
Next starts to compute the result.
Result(output log).