使用體素化網格方法實現下采樣,即減少點的數量,減少點雲數據,並同時保持點雲的形狀特征,在提高配准、曲面重建、形狀識別等算法速度中非常實用。
PCL實現的VoxelGrid類通過輸入的點雲數據創建一個三維體素柵格(可把體素柵格想象為微小的空間三維立方體的集合),然后在每個體素(即,三維立方體)內,用體素中所有點的重心來近似顯示體素中其他點,這樣該體素就內所有點就用一個重心點最終表示,對於所有體素處理后得到過濾后的點雲。這種方法比用體素中心來逼近的方法更慢,但它對於采樣點對應曲面的表示更為准確。
所以該類常用於對大數據量的下采樣處理,特別是在配准、曲面重建等工作之前作為預處理,可以很好的提高程序的速度。
代碼:
#include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/filters/voxel_grid.h> #include <pcl/io/io.h> #include <pcl/visualization/cloud_viewer.h> int main(int argc, char** argv) { pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>); pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZI>); // 填入點雲數據 pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud); // 創建濾波器對象 pcl::VoxelGrid<pcl::PointXYZI> sor;//濾波處理對象 sor.setInputCloud(cloud); sor.setLeafSize(0.01f, 0.01f, 0.01f);//設置濾波器處理時采用的體素大小的參數 sor.filter(*cloud_filtered); std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points (" << pcl::getFieldsList(*cloud_filtered) << ")."; pcl::visualization::CloudViewer viewer("Cloud Viewer"); //showCloud函數是同步的,在此處等待直到渲染顯示為止 viewer.showCloud(cloud_filtered); while (!viewer.wasStopped()) { //在此處可以添加其他處理 } return (0); }
雖然處理后數據量大大減少,但很明顯其所含有的形狀特征與空間結構信息與原始點雲差不多