1.统计滤波,去除离群点
#include <pcl\filters\\statistical_outlier_removal.h> #include <iostream> #include <pcl\io\pcd_io.h> #include <pcl\point_types.h> #include <pcl\visualization\cloud_viewer.h> int main() { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile<pcl::PointXYZ>("F:\\BaiduNetdiskDownload\\pcl\\desk.pcd", *cloud) == -1) { PCL_ERROR("Couldn't read file rabbit.pcd\n"); return(-1); } std::cout << "Loaded:" << cloud->width*cloud->height << "data points from test_pcd.pcd with the following fields:" << std::endl; /*for (size_t i = 0; i < cloud->points.size(); ++i) { std::cout << " " << cloud->points[i].x << " " << cloud->points[i].y << " " << cloud->points[i].z << " " << std::endl; }*/ // 统计滤波 // 创建滤波器,对每个点分析的临近点的个数设置为50 ,并将标准差的倍数设置为1 这意味着如果一 // 个点的距离超出了平均距离一个标准差以上,则该点被标记为离群点,并将它移除 pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; // 创建滤波器对象 sor.setInputCloud(cloud); // 设置待滤波的点云 sor.setMeanK(50); // 设置在进行统计时考虑查询点临近点数 sor.setStddevMulThresh(1); // 设置判断是否为离群点的阀值 sor.filter(*cloud); // 过滤 // 存储 pcl::io::savePCDFileBinary("desk-sor.pcd", *cloud); // 显示 pcl::visualization::CloudViewer viewer("cloud viewer"); viewer.showCloud(cloud); while (!viewer.wasStopped()) { } system("pause"); return 0; }
2. 平面分隔
1 #include <pcl/point_types.h> //PCL中所有点类型定义的头文件 2 #include <pcl/io/pcd_io.h> //打开关闭pcd文件的类定义的头文件 3 #include <pcl/filters/statistical_outlier_removal.h> 4 #include <pcl/filters/passthrough.h> 5 #include <pcl/segmentation/sac_segmentation.h> 6 #include <pcl/filters/extract_indices.h> 7 #include <pcl/filters/voxel_grid.h> 8 #include <pcl\visualization\cloud_viewer.h> 9 #include <pcl\point_types.h> 10 11 using namespace std; 12 13 14 typedef pcl::PointXYZ PoinT; 15 16 // 随机产生颜色 17 int *rand_rgb() { 18 int *rgb = new int[3]; 19 rgb[0] = rand() % 255; 20 rgb[1] = rand() % 255; 21 rgb[2] = rand() % 255; 22 return rgb; 23 } 24 25 int main(int argc, char** argv) 26 { 27 pcl::PointCloud<PoinT>::Ptr cloud(new pcl::PointCloud<PoinT>()); 28 // 加载pcd文件 29 pcl::io::loadPCDFile("desk-sor.pcd", *cloud); 30 std::cerr << "Cloud before filtering: " << std::endl; 31 std::cerr << *cloud << std::endl; 32 33 // 平面分隔 34 pcl::PointCloud<PoinT>::Ptr cloud_filtered(new pcl::PointCloud<PoinT>()); 35 pcl::SACSegmentation<PoinT> seg; 36 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());// 创建参数模型的的参数对象 37 pcl::PointIndices::Ptr inliers(new pcl::PointIndices());// 储存模型内点的下标索引的数组指针 38 seg.setMethodType(pcl::SAC_RANSAC); // 设置方法【聚类或随机样本一致性】 39 seg.setModelType(pcl::SACMODEL_PLANE); // 设置模型类型,检测平面 40 seg.setMaxIterations(100); 41 seg.setDistanceThreshold(0.01); 42 // Extract the inliers 43 pcl::ExtractIndices<PoinT> extract; 44 extract.setNegative(true); 45 int cloud_size = cloud->size(); 46 int i = 0; 47 // 迭代进行分割直到剩余80%的点云 48 while (cloud->size() > cloud_size * .8) 49 { 50 seg.setInputCloud(cloud); 51 seg.segment(*inliers, *coefficients);// 传入的参数未进行赋值 利用参数模型进行分割 52 extract.setInputCloud(cloud); 53 extract.setIndices(inliers); 54 extract.setNegative(false); 55 extract.filter(*cloud_filtered); 56 extract.setNegative(true); 57 extract.filter(*cloud); 58 cout << i << ":" << cloud->size() << endl; 59 stringstream ss; 60 ss << "desk-seg" << i << ".pcd"; 61 // 保存结果 62 pcl::io::savePCDFileBinary(ss.str(), *cloud_filtered); 63 i++; 64 } 65 66 67 // 显示 68 pcl::visualization::CloudViewer viewer("cloud viewer"); 69 viewer.showCloud(cloud); 70 71 while (!viewer.wasStopped()) { 72 73 } 74 75 76 77 pcl::io::savePCDFileBinary("desk-seg.pcd", *cloud); 78 return 0; 79 }