• pcl--第九节 点云分割


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

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

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

    聚类分割算法

    在聚类方法中每个点都与一个特征向量相关联,特征向量又包含了若干个几何或者辐射度量值。然后,在特征空间中通过聚类的方法(如K-mean法最大似然方法和模糊聚类法)分割点云数据。聚类分割的基本原理为:考察m 个数据点,在m维空间内定义点与点之间某种性质的亲疏聚类,设 m个数据点组成类,然后将具有最小距离的两类合为一类,并重新计算类与类之间的距离,迭代直到任意两类之间的距离大于指定的闽值,或者类的个数小于指定的数目,完成分割。

    基于随机采样一致性(RANSAC)的分割

    基于随机采样一致性算法的分割是通过随机取样剔除局外点,构建一个仅由局内点数据组成的基本子集的过程。其基本思想为:在进行参数估计时,不是不加区分地对待所有可能的输人数据,而是首先针对具体问题设计出一个判断准则模型,利用此判断准则迭代地剔除那些与所估计的参数不一致的输入数据,然后通过正确的输入数据来估计模型参数。基于随机采样一致性的点云分割的过程为:首先从输入的点云数据集中随机选择一些点并计算用户给定模型的参数,对数据集中的所有点设置距离阙值,如果点到模型的距离在距离闽值范围内,则将该点归为局内点,否则为局外点,然后统计所有局内点的个数,判断是否大于设定的阙值,如果是,则用内点重新估计模型,作为模型输出,存储所有内点作为分割结果,如果不是,则与当前最大的局内点个数对比,如果大于则取代当前最大局内点个数,并存储当前的模型系数,然后进行迭代计算,直到分割出用户满意的模型

    分割平面

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. int
    10. main ()
    11. {
    12. pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    13. pcl::PointCloud::Ptr inliers_points(new pcl::PointCloud);
    14. // Fill in the cloud data
    15. cloud->width = 15;
    16. cloud->height = 1;
    17. cloud->points.resize (cloud->width * cloud->height);
    18. // Generate the data
    19. for (auto& point: *cloud)
    20. {
    21. point.x = rand () / (RAND_MAX + 1.0f);
    22. point.y = rand () / (RAND_MAX + 1.0f);
    23. point.z = 1.0;
    24. }
    25. // Set a few outliers
    26. (*cloud)[0].z = 2.0;
    27. (*cloud)[3].z = -2.0;
    28. (*cloud)[6].z = 4.0;
    29. std::cerr << "Point cloud data: " << cloud->size () << " points" << std::endl;
    30. for (const auto& point: *cloud)
    31. std::cerr << " " << point.x << " "
    32. << point.y << " "
    33. << point.z << std::endl;
    34. // 创建模型系数
    35. pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    36. pcl::PointIndices::Ptr inliers (new pcl::PointIndices); // 创建索引
    37. // Create the segmentation object
    38. pcl::SACSegmentation seg; // 创建分割对象并设置参数
    39. // Optional
    40. seg.setOptimizeCoefficients (true);
    41. // Mandatory
    42. seg.setModelType (pcl::SACMODEL_PLANE);
    43. seg.setMethodType (pcl::SAC_RANSAC);
    44. seg.setDistanceThreshold (0.01);
    45. seg.setInputCloud (cloud);
    46. seg.segment (*inliers, *coefficients); //获取点云索引和对应的系数
    47. if (inliers->indices.size () == 0)
    48. {
    49. PCL_ERROR ("Could not estimate a planar model for the given dataset.\n");
    50. return (-1);
    51. }
    52. std::cerr << "Model coefficients: " << coefficients->values[0] << " "
    53. << coefficients->values[1] << " "
    54. << coefficients->values[2] << " "
    55. << coefficients->values[3] << std::endl;
    56. std::cerr << "Model inliers: " << inliers->indices.size () << std::endl;
    57. for (const auto& idx : inliers->indices)
    58. {
    59. std::cerr << idx << " " << cloud->points[idx].x << " "
    60. << cloud->points[idx].y << " "
    61. << cloud->points[idx].z << std::endl;
    62. auto& point = cloud->points[idx];
    63. inliers_points->emplace_back(point);
    64. }
    65. // 创建 PCL 可视化对象
    66. pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");
    67. // 设置显示点云的颜色为红色
    68. pcl::visualization::PointCloudColorHandlerCustom color1(cloud, 255, 0, 0);
    69. pcl::visualization::PointCloudColorHandlerCustom color2(inliers_points, 0, 255, 0);
    70. // 添加点云数据到可视化对象并设置颜色
    71. viewer.addPointCloud(cloud, color1, "cloud");
    72. viewer.addPointCloud(inliers_points, color2, "inliers_points");
    73. // 设置点云数据的显示参数
    74. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "cloud");
    75. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "inliers_points");
    76. // 添加坐标轴到可视化对象
    77. viewer.addCoordinateSystem(2.0); // 默认1.0为坐标轴的尺寸
    78. // 显示点云数据
    79. while (!viewer.wasStopped())
    80. {
    81. viewer.spinOnce();
    82. }
    83. return (0);
    84. }

     

    在PCL中如何实现圆柱体模型分割

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

    1. 过滤掉远于1.5m的数据点。
    2. 估计每个点的表面法线。
    3. 分割出平面模型(在我们的演示数据集中表示桌面)并保存到磁盘中
    4. 分出体模型(在我们的示数中表示)保到磁中注意:由于数据中噪声的出现,圆柱体模型并不十分严格。 
    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. typedef pcl::PointXYZ PointT;
    11. int
    12. main ()
    13. {
    14. // All the objects needed
    15. pcl::PCDReader reader; // pcd读对象
    16. pcl::PassThrough pass; // 直通滤波对象
    17. pcl::NormalEstimation ne; // 法向量估计对象
    18. pcl::SACSegmentationFromNormals seg; // 分割对象
    19. pcl::PCDWriter writer; // pcd写对象
    20. pcl::ExtractIndices extract; // 点云索引对象
    21. pcl::ExtractIndices extract_normals; // 法向量索引对象
    22. pcl::search::KdTree::Ptr tree (new pcl::search::KdTree ()); // kd树对象
    23. // Datasets
    24. pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
    25. pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud);
    26. pcl::PointCloud::Ptr cloud_normals (new pcl::PointCloud);
    27. pcl::PointCloud::Ptr cloud_filtered2 (new pcl::PointCloud);
    28. pcl::PointCloud::Ptr cloud_normals2 (new pcl::PointCloud);
    29. pcl::ModelCoefficients::Ptr coefficients_plane (new pcl::ModelCoefficients), coefficients_cylinder (new pcl::ModelCoefficients);
    30. pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices), inliers_cylinder (new pcl::PointIndices);
    31. // 读取点云数据
    32. reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);
    33. std::cerr << "PointCloud has: " << cloud->size () << " data points." << std::endl;
    34. // Build a passthrough filter to remove spurious NaNs and scene background
    35. // 直通滤波出想要的点云,保留z轴0到1.5m的点云数据到cloud_filtered对象中,其他都删除
    36. pass.setInputCloud (cloud);
    37. pass.setFilterFieldName ("z");
    38. pass.setFilterLimits (0, 1.5);
    39. pass.filter (*cloud_filtered);
    40. std::cerr << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl;
    41. // Estimate point normals
    42. // 估计cloud_filtered的法向量
    43. ne.setSearchMethod (tree);
    44. ne.setInputCloud (cloud_filtered);
    45. ne.setKSearch (50);
    46. ne.compute (*cloud_normals);
    47. // Create the segmentation object for the planar model and set all the parameters
    48. // 为平面模型创建分割对象并设置所有参数
    49. seg.setOptimizeCoefficients (true);
    50. seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
    51. seg.setNormalDistanceWeight (0.1);
    52. seg.setMethodType (pcl::SAC_RANSAC);
    53. seg.setMaxIterations (100);
    54. seg.setDistanceThreshold (0.03);
    55. seg.setInputCloud (cloud_filtered);
    56. seg.setInputNormals (cloud_normals);
    57. // Obtain the plane inliers and coefficients
    58. // 获取平面离群值和系数
    59. seg.segment (*inliers_plane, *coefficients_plane);
    60. std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
    61. // Extract the planar inliers from the input cloud
    62. // 提取平面内的点云
    63. extract.setInputCloud (cloud_filtered);
    64. extract.setIndices (inliers_plane);
    65. extract.setNegative (false);
    66. // Write the planar inliers to disk
    67. // 把平面点云保存到磁盘
    68. pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud ());
    69. extract.filter (*cloud_plane);
    70. std::cerr << "PointCloud representing the planar component: " << cloud_plane->size () << " data points." << std::endl;
    71. writer.write ("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);
    72. // Remove the planar inliers, extract the rest
    73. // 删除平面点云,提取剩余的点云到cloud_filtered2
    74. extract.setNegative (true);
    75. extract.filter (*cloud_filtered2);
    76. // 提取剩余的法向量
    77. extract_normals.setNegative (true);
    78. extract_normals.setInputCloud (cloud_normals);
    79. extract_normals.setIndices (inliers_plane);
    80. extract_normals.filter (*cloud_normals2);
    81. // Create the segmentation object for cylinder segmentation and set all the parameters
    82. // 创建分割圆柱体的对象,并设置参数
    83. seg.setOptimizeCoefficients (true);
    84. seg.setModelType (pcl::SACMODEL_CYLINDER);
    85. seg.setMethodType (pcl::SAC_RANSAC);
    86. seg.setNormalDistanceWeight (0.1);
    87. seg.setMaxIterations (10000);
    88. seg.setDistanceThreshold (0.05);
    89. seg.setRadiusLimits (0, 0.1);
    90. seg.setInputCloud (cloud_filtered2);
    91. seg.setInputNormals (cloud_normals2);
    92. // Obtain the cylinder inliers and coefficients
    93. seg.segment (*inliers_cylinder, *coefficients_cylinder);
    94. std::cerr << "Cylinder coefficients: " << *coefficients_cylinder << std::endl;
    95. // Write the cylinder inliers to disk
    96. extract.setInputCloud (cloud_filtered2);
    97. extract.setIndices (inliers_cylinder);
    98. extract.setNegative (false);
    99. pcl::PointCloud::Ptr cloud_cylinder (new pcl::PointCloud ());
    100. extract.filter (*cloud_cylinder);
    101. if (cloud_cylinder->points.empty ())
    102. std::cerr << "Can't find the cylindrical component." << std::endl;
    103. else
    104. {
    105. std::cerr << "PointCloud representing the cylindrical component: " << cloud_cylinder->size () << " data points." << std::endl;
    106. writer.write ("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
    107. }
    108. return (0);
    109. }

    欧式聚类提取

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. int
    13. main ()
    14. {
    15. // Read in the cloud data
    16. pcl::PCDReader reader;
    17. pcl::PointCloud::Ptr cloud (new pcl::PointCloud), cloud_f (new pcl::PointCloud);
    18. reader.read ("table_scene_lms400.pcd", *cloud);
    19. std::cout << "PointCloud before filtering has: " << cloud->size () << " data points." << std::endl; //*
    20. // Create the filtering object: downsample the dataset using a leaf size of 1cm
    21. pcl::VoxelGrid vg;
    22. pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud);
    23. vg.setInputCloud (cloud);
    24. vg.setLeafSize (0.01f, 0.01f, 0.01f);
    25. vg.filter (*cloud_filtered);
    26. std::cout << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl; //*
    27. // Create the segmentation object for the planar model and set all the parameters
    28. pcl::SACSegmentation seg;
    29. pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    30. pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    31. pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud ());
    32. pcl::PCDWriter writer;
    33. seg.setOptimizeCoefficients (true);
    34. seg.setModelType (pcl::SACMODEL_PLANE);
    35. seg.setMethodType (pcl::SAC_RANSAC);
    36. seg.setMaxIterations (100);
    37. seg.setDistanceThreshold (0.02);
    38. int nr_points = (int) cloud_filtered->size ();
    39. while (cloud_filtered->size () > 0.3 * nr_points)
    40. {
    41. // Segment the largest planar component from the remaining cloud
    42. seg.setInputCloud (cloud_filtered);
    43. seg.segment (*inliers, *coefficients);
    44. if (inliers->indices.size () == 0)
    45. {
    46. std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
    47. break;
    48. }
    49. // Extract the planar inliers from the input cloud
    50. pcl::ExtractIndices extract;
    51. extract.setInputCloud (cloud_filtered);
    52. extract.setIndices (inliers);
    53. extract.setNegative (false);
    54. // Get the points associated with the planar surface
    55. extract.filter (*cloud_plane);
    56. std::cout << "PointCloud representing the planar component: " << cloud_plane->size () << " data points." << std::endl;
    57. // Remove the planar inliers, extract the rest
    58. extract.setNegative (true);
    59. extract.filter (*cloud_f);
    60. *cloud_filtered = *cloud_f;
    61. }
    62. // Creating the KdTree object for the search method of the extraction
    63. pcl::search::KdTree::Ptr tree (new pcl::search::KdTree);
    64. tree->setInputCloud (cloud_filtered);
    65. std::vector cluster_indices;
    66. pcl::EuclideanClusterExtraction ec;
    67. ec.setClusterTolerance (0.02); // 2cm
    68. ec.setMinClusterSize (100);
    69. ec.setMaxClusterSize (25000);
    70. ec.setSearchMethod (tree);
    71. ec.setInputCloud (cloud_filtered);
    72. ec.extract (cluster_indices);
    73. int j = 0;
    74. for (std::vector::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
    75. {
    76. pcl::PointCloud::Ptr cloud_cluster (new pcl::PointCloud);
    77. for (const auto& idx : it->indices)
    78. cloud_cluster->push_back ((*cloud_filtered)[idx]); //*
    79. cloud_cluster->width = cloud_cluster->size ();
    80. cloud_cluster->height = 1;
    81. cloud_cluster->is_dense = true;
    82. std::cout << "PointCloud representing the Cluster: " << cloud_cluster->size () << " data points." << std::endl;
    83. std::stringstream ss;
    84. ss << "cloud_cluster_" << j << ".pcd";
    85. writer.write (ss.str (), *cloud_cluster, false); //*
    86. j++;
    87. }
    88. return (0);
    89. }

     

    实现详解

    pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    

    因为点云是PointXYZ类型的,所以这里用PointXYZ创建一个欧氏聚类对象,并设置提取的参数和变量。

    ec.setClusterTolerance(0.02); // 设置临近搜索的搜索半径(搜索容差)为2cm
    

    设置一个合适的聚类搜索半径 ClusterTolerance,如果搜索半径取一个非常小的值,那么一个实际独立的对象就会被分割为多个聚类;如果将值设置得太高,那么多个对象就会被分割为一个聚类,所以需要进行测试找出最合适的ClusterTolerance.

    1. ec.setMinClusterSize(100); // 每个簇(集群)的最小大小
    2. ec.setMaxClusterSize(25000); // 每个簇(集群)的最大大小

    本例用两个参数来限制找到的聚类 :

    1. 用 set­MinClusterSize( )来限制一个聚类最少需要的点数目
    2. 用 setMaXClusterSize( )来限制一个聚类最多需要的点数目

     

     

  • 相关阅读:
    关于数据中心的设计方案,数据中心网络规划设计
    怒刷LeetCode的第19天(Java版)
    ArcGIS软件制作双变量等值区域地图(Bivariate Choropleth Maps)
    2022年深圳市龙岗区企业培育专项扶持细则
    Spring Event
    在优化微信、支付宝小程序用户体验时有哪些关键指标
    服务器正文22:linux内核网络模块笔记:理解TCP连接建立过程、一条TCP连接多大内存、一台机器最多支持多少条TCP连接、网络优化建议(下)
    发动机连杆加工工艺及镗孔夹具设计
    Qt --- Day02
    免杀Bdfproxy
  • 原文地址:https://blog.csdn.net/weixin_42398658/article/details/133129454