相机扫描的点云存在问题,点云会不在一个平面上,以此使用点云平滑算法,将点云变得平滑
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
pcl::VoxelGrid<pcl::PointXYZ> sor2;
sor2.setInputCloud(cloud);
sor2.setLeafSize(downsample, downsample, downsample); //大小为2*2*2
sor2.filter(*cloud1);
pcl::PointCloud<pcl::PointNormal>::Ptr smoothedCloud(new pcl::PointCloud<pcl::PointNormal>);
// Smoothing object (we choose what point types we want as input and output).
pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> filter;
pcl::search::KdTree<pcl::PointXYZ>::Ptr kdtree;
filter.setInputCloud(cloud1);
//filter.setUpsamplingMethod(filter.SAMPLE_LOCAL_PLANE); // 增加密度较小区域的密度对于holes的填补却无能为力,具体方法要结合参数使用
filter.setSearchRadius(2);// 用于拟合的K近邻半径。在这个半径里进行表面映射和曲面拟合。半径越小拟合后曲面的失真度越小,反之有可能出现过拟合的现象。
filter.setPolynomialFit(true); // 对于法线的估计是有多项式还是仅仅依靠切线。true为加多项式;false不加,速度较快
filter.setPolynomialFit(3); // 拟合曲线的阶数
filter.setComputeNormals(true); // 是否存储点云的法向量,true 为存储,false 不存储
filter.setSearchMethod(kdtree);
filter.process(*smoothedCloud); // 输出