当前位置:   article > 正文

点云分割segmentation

点云分割

        点云分割是根据空间、几何和纹理等特征对点云进行划分,使得同一划分区域内的点云拥有相似的特征 。点云的有效分割往往是许多应用的前提。例如,在逆向工程CAD/CAM 领域,对零件的不同扫描表面进行分割,然后才能更好地进行孔洞修复、曲面重建、特征描述和提取,进而进行基于3D内容的检索、组合重用等。在激光遥感领域,同样需要对地面、物体首先进行分类处理,然后才能进行后期地物的识别、重建 。

        总之,分割采用分而治之的思想,在点云处理中和滤波一样属于重要的基础操作,在PCL 中目前实现了进行分割的基础架构,为后期更多的扩展奠定了基础,现有实现的分割算法是鲁棒性比较好的Cluster聚类分割和RANSAC基于随机采样一致性的分割。

        PCL分割库包含多种算法,这些算法用于将点云分割为不同簇。适合处理由多个隔离区域空间组成的点云。将点云分解成其组成部分,然后可以对其进行独立处理。 可以在集群提取教程中找到理论入门,以解释集群方法的工作原理。这两个图说明了平面模型分割(左)和圆柱模型分割(右)的结果。

平面模型分割

代码实现

  1. #include <iostream>
  2. #include <pcl/ModelCoefficients.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/point_types.h>
  5. #include <pcl/sample_consensus/method_types.h>
  6. #include <pcl/sample_consensus/model_types.h>
  7. #include <pcl/segmentation/sac_segmentation.h>
  8. #include <pcl/visualization/pcl_visualizer.h>
  9. int main(int argc, char** argv)
  10. {
  11. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  12. /***
  13. * 生成20个无序点云,x,y为随机数,z为1.0
  14. * 将points中0、3、6、9、12、15索引位置的z值进行修改,将之作为离群值
  15. */
  16. cloud->width = 20;
  17. cloud->height = 1;
  18. cloud->points.resize(cloud->width * cloud->height);
  19. for (std::size_t i = 0; i < cloud->points.size(); ++i) {
  20. cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
  21. cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
  22. cloud->points[i].z = 1.0;
  23. }
  24. // 设置离群点
  25. cloud->points[0].z = 2.0;
  26. cloud->points[3].z = -2.0;
  27. cloud->points[6].z = 4.0;
  28. cloud->points[9].z = 3.0;
  29. cloud->points[12].z = -3.0;
  30. cloud->points[15].z = -4.0;
  31. std::cerr << "Point cloud data: " << cloud->points.size() << " points" << std::endl;
  32. for (std::size_t i = 0; i < cloud->points.size(); ++i)
  33. std::cerr << " " << cloud->points[i].x << " "
  34. << cloud->points[i].y << " "
  35. << cloud->points[i].z << std::endl;
  36. /**
  37. * 创建分割时所需要的模型系数对象 coefficients 及存储内点的点索引集合对象 inliers .
  38. * 这也是我们指定“阈值距离DistanceThreshold”的地方,该距离阈值确定点必须与模型有多远才能被视为离群点。
  39. * 这里距离阔值是 0.01m ,即只要点到 z=1 平面距离小于该阈值的点都作为内部点看待,而大于该阈值的则看做离群点。
  40. * 我们将使用RANSAC方法(`pcl::SAC_RANSAC`)作为可靠的估计器。因为RANSAC比较简单(其他强大的估算工具也以此为基础,并添加了其他更复杂的概念)。
  41. */
  42. pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
  43. pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
  44. // 创建分割对象
  45. pcl::SACSegmentation<pcl::PointXYZ> seg;
  46. // 可选配置:是否优化模型系数
  47. seg.setOptimizeCoefficients(true);
  48. // 必选配置:设置分割的模型类型、分割算法、距离阈值、输入点云
  49. seg.setModelType(pcl::SACMODEL_PLANE);
  50. seg.setMethodType(pcl::SAC_RANSAC);
  51. seg.setDistanceThreshold(0.01);
  52. seg.setInputCloud(cloud);
  53. // 执行分割操作,并存储分割结果保存到点集合 inliers 及存储平面模型系数 coefficients
  54. seg.segment(*inliers, *coefficients);
  55. if (inliers->indices.size() == 0) {
  56. PCL_ERROR("Could not estimate a planar model for the given dataset.");
  57. return (-1);
  58. }
  59. // 此段代码用来打印出估算的平面模型的参数(以 ax+by+ca+d=0 形式),详见RANSAC采样一致性算法的SACMODEL_PLANE平面模型
  60. std::cerr << "Model coefficients: " << coefficients->values[0] << " "
  61. << coefficients->values[1] << " "
  62. << coefficients->values[2] << " "
  63. << coefficients->values[3] << std::endl;
  64. std::cerr << "Model inliers: " << inliers->indices.size() << std::endl;
  65. for (std::size_t i = 0; i < inliers->indices.size(); ++i)
  66. std::cerr << inliers->indices[i] << " " << cloud->points[inliers->indices[i]].x << " "
  67. << cloud->points[inliers->indices[i]].y << " "
  68. << cloud->points[inliers->indices[i]].z << std::endl;
  69. return 0;
  70. }

输出结果

  1. Point cloud data: 20 points
  2. 1.28125 577.094 2
  3. 197.938 828.125 1
  4. 599.031 491.375 1
  5. 358.688 917.438 -2
  6. 842.562 764.5 1
  7. 178.281 879.531 1
  8. 727.531 525.844 4
  9. 311.281 15.3438 1
  10. 93.5938 373.188 1
  11. 150.844 169.875 3
  12. 1012.22 456.375 1
  13. 121.938 4.78125 1
  14. 9.125 386.938 -3
  15. 544.406 584.875 1
  16. 616.188 621.719 1
  17. 170.219 678.938 -4
  18. 461.594 360.562 1
  19. 58.4062 622.25 1
  20. 802.094 821.844 1
  21. 532.344 309.188 1
  22. Model coefficients: 0 0 1 -1
  23. Model inliers: 14
  24. 1 197.938 828.125 1
  25. 2 599.031 491.375 1
  26. 4 842.562 764.5 1
  27. 5 178.281 879.531 1
  28. 7 311.281 15.3438 1
  29. 8 93.5938 373.188 1
  30. 10 1012.22 456.375 1
  31. 11 121.938 4.78125 1
  32. 13 544.406 584.875 1
  33. 14 616.188 621.719 1
  34. 16 461.594 360.562 1
  35. 17 58.4062 622.25 1
  36. 18 802.094 821.844 1
  37. 19 532.344 309.188 1

圆柱体模型分割

        本节举例说明了如何采用随机采样一致性估计从带有噪声的点云中提取一个圆柱体模型,整个程序处理流程如下 :

  1. 过滤掉远于 1. 5 m 的数据点
  2. 估计每个点的表面法线
  3. 分割出平面模型 (数据集中的桌面)并保存到磁盘中。
  4. 分割圆出柱体模型(数据集中的杯子)并保存到磁盘中。

注:由于数据中包含噪声,圆柱体模型并不十分严格 。

 代码实现

  1. #include <pcl/ModelCoefficients.h>
  2. #include <pcl/io/pcd_io.h>
  3. #include <pcl/point_types.h>
  4. #include <pcl/filters/extract_indices.h>
  5. #include <pcl/filters/passthrough.h>
  6. #include <pcl/features/normal_3d.h>
  7. #include <pcl/sample_consensus/method_types.h>
  8. #include <pcl/sample_consensus/model_types.h>
  9. #include <pcl/segmentation/sac_segmentation.h>
  10. typedef pcl::PointXYZ PointT;
  11. int main(int argc, char** argv) {
  12. // 需要的所有对象
  13. pcl::PCDReader reader; // PCD文件读取对象
  14. pcl::PassThrough<PointT> pass; // 直通滤波器
  15. pcl::NormalEstimation<PointT, pcl::Normal> ne; // 法线估算对象
  16. pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg; // 分割器
  17. pcl::PCDWriter writer; // PCD文件写出对象
  18. pcl::ExtractIndices<PointT> extract; // 点提取对象
  19. pcl::ExtractIndices<pcl::Normal> extract_normals; // 法线提取对象
  20. pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>());
  21. // 数据
  22. pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
  23. pcl::PointCloud<PointT>::Ptr cloud_filtered(new pcl::PointCloud<PointT>);
  24. pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
  25. pcl::PointCloud<PointT>::Ptr cloud_filtered2(new pcl::PointCloud<PointT>);
  26. pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2(new pcl::PointCloud<pcl::Normal>);
  27. pcl::ModelCoefficients::Ptr coefficients_plane(new pcl::ModelCoefficients), coefficients_cylinder(new pcl::ModelCoefficients);
  28. pcl::PointIndices::Ptr inliers_plane(new pcl::PointIndices), inliers_cylinder(new pcl::PointIndices);
  29. // 读取点云数据
  30. reader.read("G:/vsdata/PCLlearn/PCDdata/table_scene_mug_stereo_textured.pcd", *cloud);
  31. std::cerr << "PointCloud has: " << cloud->points.size() << " data points." << std::endl;
  32. // 构建一个直通过滤器以删除虚假的NaNs
  33. pass.setInputCloud(cloud);
  34. pass.setFilterFieldName("z");
  35. pass.setFilterLimits(0, 1.5);
  36. pass.filter(*cloud_filtered);
  37. std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points." << std::endl;
  38. // 估计点法线
  39. ne.setSearchMethod(tree);
  40. ne.setInputCloud(cloud_filtered);
  41. ne.setKSearch(50);
  42. ne.compute(*cloud_normals);
  43. // 为平面模型创建分割对象并设置所有参数
  44. seg.setOptimizeCoefficients(true);
  45. seg.setModelType(pcl::SACMODEL_NORMAL_PLANE);
  46. seg.setNormalDistanceWeight(0.1);
  47. seg.setMethodType(pcl::SAC_RANSAC);
  48. seg.setMaxIterations(100);
  49. seg.setDistanceThreshold(0.03);
  50. seg.setInputCloud(cloud_filtered);
  51. seg.setInputNormals(cloud_normals);
  52. // 获得平面内点和系数
  53. seg.segment(*inliers_plane, *coefficients_plane);
  54. std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
  55. // 从输入云中提取平面内联
  56. extract.setInputCloud(cloud_filtered);
  57. extract.setIndices(inliers_plane);
  58. extract.setNegative(false);
  59. // 将平面点云保存
  60. pcl::PointCloud<PointT>::Ptr cloud_plane(new pcl::PointCloud<PointT>());
  61. extract.filter(*cloud_plane);
  62. std::cerr << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points."
  63. << std::endl;
  64. writer.write("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);
  65. // 移除平面inliers, 提取其余部分
  66. extract.setNegative(true);
  67. extract.filter(*cloud_filtered2);
  68. extract_normals.setNegative(true);
  69. extract_normals.setInputCloud(cloud_normals);
  70. extract_normals.setIndices(inliers_plane);
  71. extract_normals.filter(*cloud_normals2);
  72. // 设置圆柱体分割对象参数
  73. seg.setOptimizeCoefficients(true);
  74. seg.setModelType(pcl::SACMODEL_CYLINDER); // 设置分割模型为圆柱体
  75. seg.setMethodType(pcl::SAC_RANSAC); // 设置采用RANSAC算法进行参数估计
  76. seg.setNormalDistanceWeight(0.1); // 设置表面法线权重系数
  77. seg.setMaxIterations(10000); // 设置最大迭代次数10000
  78. seg.setDistanceThreshold(0.05); // 设置内点到模型的最大距离 0.05m
  79. seg.setRadiusLimits(0, 0.1); // 设置圆柱体的半径范围0 -> 0.1m
  80. seg.setInputCloud(cloud_filtered2);
  81. seg.setInputNormals(cloud_normals2);
  82. // 获取cylinder inliers
  83. seg.segment(*inliers_cylinder, *coefficients_cylinder);
  84. std::cerr << "Cylinder coefficients: " << *coefficients_cylinder << std::endl;
  85. // 将cylinder inliers保存
  86. extract.setInputCloud(cloud_filtered2);
  87. extract.setIndices(inliers_cylinder);
  88. extract.setNegative(false);
  89. pcl::PointCloud<PointT>::Ptr cloud_cylinder(new pcl::PointCloud<PointT>());
  90. extract.filter(*cloud_cylinder);
  91. if (cloud_cylinder->points.empty())
  92. std::cerr << "Can't find the cylindrical component." << std::endl;
  93. else {
  94. std::cerr << "PointCloud representing the cylindrical component: " << cloud_cylinder->points.size()
  95. << " data points." << std::endl;
  96. writer.write("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
  97. }
  98. return (0);
  99. }

输出结果

  1. PointCloud has: 307200 data points.
  2. PointCloud after filtering has: 139897 data points.
  3. Plane coefficients: header:
  4. seq: 0 stamp: 0 frame_id:
  5. values[]
  6. values[0]: 0.016223
  7. values[1]: -0.83761
  8. values[2]: -0.546028
  9. values[3]: 0.528923
  10. PointCloud representing the planar component: 116054 data points.
  11. Cylinder coefficients: header:
  12. seq: 0 stamp: 0 frame_id:
  13. values[]
  14. values[0]: 0.0519707
  15. values[1]: 0.172656
  16. values[2]: 0.834861
  17. values[3]: 0.0229013
  18. values[4]: -0.836744
  19. values[5]: -0.547116
  20. values[6]: 0.0387646
  21. PointCloud representing the cylindrical component: 11479 data points.

欧式聚类提取

        基于欧式距离的分割和基于区域生长的分割本质上都是用区分邻里关系远近来完成的。由于点云数据提供了更高维度的数据,故有很多信息可以提取获得。欧几里得算法使用邻居之间距离作为判定标准,而区域生长算法则利用了法线,曲率,颜色等信息来判断点云是否应该聚成一类。

欧几里德算法

具体的实现方法大致是:

  1. 找到空间中某点p10,有kdTree找到离他最近的n个点,判断这n个点到p的距离。将距离小于阈值r的点p12,p13,p14....放在类Q里
  2. 在 Q\p10 里找到一点p12,重复1
  3. 在 Q\p10,p12 找到一点,重复1,找到p22,p23,p24....全部放进Q里
  4. 当 Q 再也不能有新点加入了,则完成搜索了

        因为点云总是连成片的,很少有什么东西会浮在空中来区分。但是如果结合此算法可以应用很多东西。比如

  1. 半径滤波删除离群点
  2. 采样一致找到桌面或者除去滤波

        当然,一旦桌面被剔除,桌上的物体就自然成了一个个的浮空点云团。就能够直接用欧几里德算法进行分割了,这样就可以提取出我们想要识别的东西。

 代码实现

  1. #include <pcl/ModelCoefficients.h>
  2. #include <pcl/point_types.h>
  3. #include <pcl/ModelCoefficients.h>
  4. #include <pcl/point_types.h>
  5. #include <pcl/io/pcd_io.h>
  6. #include <pcl/filters/extract_indices.h>
  7. #include <pcl/filters/voxel_grid.h>
  8. #include <pcl/features/normal_3d.h>
  9. #include <pcl/kdtree/kdtree.h>
  10. #include <pcl/sample_consensus/method_types.h>
  11. #include <pcl/sample_consensus/model_types.h>
  12. #include <pcl/segmentation/sac_segmentation.h>
  13. #include <pcl/segmentation/extract_clusters.h>
  14. #include <pcl/visualization/pcl_visualizer.h>
  15. int main(int argc, char** argv)
  16. {
  17. pcl::PCDReader reader;
  18. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>), cloud_f(
  19. new pcl::PointCloud<pcl::PointXYZ>);
  20. reader.read("G:/vsdata/PCLlearn/PCDdata/table_scene_lms400.pcd", *cloud);
  21. std::cout << "PointCloud before filtering has: " << cloud->points.size() << " data points." << std::endl; //*
  22. // 执行降采样滤波,叶子大小1cm
  23. pcl::VoxelGrid<pcl::PointXYZ> vg;
  24. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
  25. vg.setInputCloud(cloud);
  26. vg.setLeafSize(0.01f, 0.01f, 0.01f);
  27. vg.filter(*cloud_filtered);
  28. std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size() << " data points."
  29. << std::endl; //*
  30. // 创建平面模型分割器并初始化参数
  31. pcl::SACSegmentation<pcl::PointXYZ> seg;
  32. pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
  33. pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
  34. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane(new pcl::PointCloud<pcl::PointXYZ>());
  35. pcl::PCDWriter writer;
  36. seg.setOptimizeCoefficients(true);
  37. seg.setModelType(pcl::SACMODEL_PLANE);
  38. seg.setMethodType(pcl::SAC_RANSAC);
  39. seg.setMaxIterations(100);
  40. seg.setDistanceThreshold(0.02);
  41. int i = 0, nr_points = (int)cloud_filtered->points.size();
  42. while (cloud_filtered->points.size() > 0.3 * nr_points) {
  43. // 移除剩余点云中最大的平面
  44. seg.setInputCloud(cloud_filtered);
  45. // 执行分割,将分割出来的平面点云索引保存到inliers中
  46. seg.segment(*inliers, *coefficients);
  47. if (inliers->indices.size() == 0) {
  48. std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
  49. break;
  50. }
  51. // 从输入点云中取出平面内点
  52. pcl::ExtractIndices<pcl::PointXYZ> extract;
  53. extract.setInputCloud(cloud_filtered);
  54. extract.setIndices(inliers);
  55. extract.setNegative(false);
  56. // 得到与平面相关的点cloud_plane
  57. extract.filter(*cloud_plane);
  58. std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size() << " data points."
  59. << std::endl;
  60. // 从点云中剔除这些平面内点,提取出剩下的点保存到cloud_f中,并重新赋值给cloud_filtered。
  61. extract.setNegative(true);
  62. extract.filter(*cloud_f);
  63. *cloud_filtered = *cloud_f;
  64. }
  65. // 为提取算法的搜索方法创建一个KdTree对象
  66. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
  67. tree->setInputCloud(cloud_filtered);
  68. /**
  69. * 在这里,我们创建一个PointIndices的vector,该vector在vector <int>中包含实际的索引信息。
  70. * 每个检测到的簇的索引都保存在这里-请注意,cluster_indices是一个vector,包含多个检测到的簇的PointIndices的实例。
  71. * 因此,cluster_indices[0]包含我们点云中第一个 cluster(簇)的所有索引。
  72. *
  73. * 从点云中提取簇(集群),并将点云索引保存在 cluster_indices 中。
  74. */
  75. std::vector<pcl::PointIndices> cluster_indices;
  76. pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;//因为点云是PointXYZ类型的,所以这里用PointXYZ创建一个欧氏聚类对象,并设置提取的参数和变量
  77. ec.setClusterTolerance(0.02); // 设置一个合适的聚类搜索半径 ClusterTolerance,如果搜索半径取一个非常小的值,那么一个实际独立的对象就会被分割为多个聚类;如果将值设置得太高,那么多个对象就会被分割为一个聚类,所以需要进行测试找出最合适的ClusterTolerance
  78. ec.setMinClusterSize(100); // 每个簇(集群)的最小大小
  79. ec.setMaxClusterSize(25000); // 每个簇(集群)的最大大小
  80. ec.setSearchMethod(tree); // 设置点云搜索算法
  81. ec.setInputCloud(cloud_filtered); // 设置输入点云
  82. ec.extract(cluster_indices); // 设置提取到的簇,将每个簇以索引的形式保存到cluster_indices;
  83. pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
  84. // 为了从点云索引向量中分割出每个簇,必须迭代访问点云索引,
  85. int j = 0;
  86. for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin();
  87. it != cluster_indices.end(); ++it) {
  88. // 每次创建一个新的点云数据集,并且将所有当前簇的点写入到点云数据集中。
  89. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
  90. const std::vector<int>& indices = it->indices;
  91. for (std::vector<int>::const_iterator pit = indices.begin(); pit != indices.end(); ++pit)
  92. cloud_cluster->points.push_back(cloud_filtered->points[*pit]);
  93. cloud_cluster->width = cloud_cluster->points.size();
  94. cloud_cluster->height = 1;
  95. cloud_cluster->is_dense = true;
  96. std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size() << " data points."
  97. << std::endl;
  98. /*
  99. std::stringstream ss;
  100. ss << "cloud_cluster_" << j << ".pcd";
  101. writer.write<pcl::PointXYZ>(ss.str(), *cloud_cluster, false); //
  102. */
  103. std::stringstream ss;
  104. ss << "cloud_cluster_" << j;
  105. // Generate a random (bright) color
  106. pcl::visualization::PointCloudColorHandlerRandom<pcl::PointXYZ> single_color(cloud_cluster);
  107. viewer->addPointCloud<pcl::PointXYZ>(cloud_cluster, single_color, ss.str());
  108. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, ss.str());
  109. j++;
  110. }
  111. std::cout << "cloud size: " << cluster_indices.size() << std::endl;
  112. viewer->addCoordinateSystem(0.5);
  113. while (!viewer->wasStopped()) {
  114. viewer->spinOnce();
  115. }
  116. return (0);
  117. }

输出结果

  1. PointCloud before filtering has: 460400 data points.
  2. PointCloud after filtering has: 41049 data points.
  3. PointCloud representing the planar component: 20536 data points.
  4. PointCloud representing the planar component: 12442 data points.
  5. PointCloud representing the Cluster: 4857 data points.
  6. PointCloud representing the Cluster: 1386 data points.
  7. PointCloud representing the Cluster: 321 data points.
  8. PointCloud representing the Cluster: 291 data points.
  9. PointCloud representing the Cluster: 123 data points.
  10. cloud size: 5

实现效果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/630039
推荐阅读
相关标签
  

闽ICP备14008679号