PCL点云曲面重采样三种方法:上采样,下采样,均匀采样
(1)下采樣? Downsampling
一般下采樣是通過構(gòu)造一個三維體素柵格,然后在每個體素內(nèi)用體素內(nèi)的所有點的重心近似顯示體素中的其他點,這樣體素內(nèi)所有點就用一個重心點來表示,進行下采樣的來達到濾波的效果,這樣就大大的減少了數(shù)據(jù)量,特別是在配準,曲面重建等工作之前作為預處理,可以很好的提高程序的運行速度,
#include <pcl/io/pcd_io.h> #include <pcl/filters/voxel_grid.h>int main(int argc, char** argv) {// 創(chuàng)建點云對象pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZ>);// 讀取PCD文件if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0){return -1;}// 創(chuàng)建濾波對象pcl::VoxelGrid<pcl::PointXYZ> filter;filter.setInputCloud(cloud);// 設置體素柵格的大小為 1x1x1cmfilter.setLeafSize(0.01f, 0.01f, 0.01f);filter.filter(*filteredCloud); }實驗結(jié)果(略)
(2)
均勻采樣:這個類基本上是相同的,但它輸出的點云索引是選擇的關鍵點在計算描述子的常見方式。
#include <pcl/io/pcd_io.h> #include <pcl/keypoints/uniform_sampling.h>int main(int argc, char** argv) {pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);pcl::PointCloud<pcl::PointXYZ>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZ>);if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0){return -1;}// Uniform sampling object.pcl::UniformSampling<pcl::PointXYZ> filter;filter.setInputCloud(cloud);filter.setRadiusSearch(0.01f);// We need an additional object to store the indices of surviving points.pcl::PointCloud<int> keypointIndices;filter.compute(keypointIndices);pcl::copyPointCloud(*cloud, keypointIndices.points, *filteredCloud); }(3)增采樣 :增采樣是一種表面重建方法,當你有比你想象的要少的點云數(shù)據(jù)時,增采樣可以幫你恢復原有的表面(S),通過內(nèi)插你目前擁有的點云數(shù)據(jù),這是一個復雜的猜想假設的過程。所以構(gòu)建的結(jié)果不會百分之一百準確,但有時它是一種可選擇的方案。所以,在你的點云云進行下采樣時,一定要保存一份原始數(shù)據(jù)!
#include <pcl/io/pcd_io.h> #include <pcl/surface/mls.h>int main(int argc,char** argv) { // 新建點云存儲對象 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointXYZ>::Ptr filteredCloud(new pcl::PointCloud<pcl::PointXYZ>);// 讀取文件if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0){return -1;}// 濾波對象pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointXYZ> filter;filter.setInputCloud(cloud);//建立搜索對象pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree;filter.setSearchMethod(kdtree);//設置搜索鄰域的半徑為3cmfilter.setSearchRadius(0.03);// Upsampling 采樣的方法有 DISTINCT_CLOUD, RANDOM_UNIFORM_DENSITYfilter.setUpsamplingMethod(pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointXYZ>::SAMPLE_LOCAL_PLANE);// 采樣的半徑是filter.setUpsamplingRadius(0.03);// 采樣步數(shù)的大小filter.setUpsamplingStepSize(0.02);filter.process(*filteredCloud); }實驗的結(jié)果
原始圖像可視化:
?
(4)表面重建
深度傳感器的測量是不準確的,和由此產(chǎn)生的點云也是存在的測量誤差,比如離群點,孔等表面,可以用一個算法重建表面,遍歷所有的點云和插值數(shù)據(jù),試圖重建原來的表面。比如增采樣,PCL使用MLS算法和類。執(zhí)行這一步是很重要的,因為由此產(chǎn)生的點云的法線將更準確。
#include <pcl/io/pcd_io.h> #include <pcl/surface/mls.h> #include <pcl/visualization/pcl_visualizer.h> #include <pcl/visualization/cloud_viewer.h> #include <boost/thread/thread.hpp> int main(int argc, char** argv) {pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointNormal>::Ptr smoothedCloud(new pcl::PointCloud<pcl::PointNormal>);if (pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud) != 0){return -1;}// Smoothing object (we choose what point types we want as input and output).pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> filter;filter.setInputCloud(cloud);// Use all neighbors in a radius of 3cm.filter.setSearchRadius(0.03);// If true, the surface and normal are approximated using a polynomial estimation// (if false, only a tangent one).filter.setPolynomialFit(true);// We can tell the algorithm to also compute smoothed normals (optional).filter.setComputeNormals(true);// kd-tree object for performing searches.pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree;filter.setSearchMethod(kdtree);filter.process(*smoothedCloud);boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("smooth")); viewer->addPointCloud<pcl::PointNormal>(smoothedCloud,"smoothed");while(!viewer->wasStopped()){viewer->spinOnce(100); boost::this_thread::sleep(boost::posix_time::microseconds(1000000));} }運行即可查看結(jié)果
?????????????????????????????????????????????????????????????? 原始圖像(加了顏色)
???????????????????????????????????????????? 增采樣平滑后(沒有顏色信息)
?
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的PCL点云曲面重采样三种方法:上采样,下采样,均匀采样的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab读取点云数据显示
- 下一篇: C++11判断inf, nan