• [点云分割] 圆柱体分割


    平面分割类似,关于理论的东西不多阐释了,直接上代码:

    1. #include
    2. #include
    3. #include // 模型系数的定义
    4. #include
    5. #include // 各种点云数据类型
    6. #include // 包含用于采样一致性算法的不同方法的定义,如RANSAC、MSAC等
    7. #include // 包含用于采样一致性算法的不同模型的定义,如平面、球体、圆柱体
    8. #include // 包含用于分割点云的采样一致性算法(SACSegmentation)的定义,用于识别点云的几何模型
    9. #include // 包含用于从点云中提取特定索引的函数和类,用于根据索引提取点云中的子集
    10. #include // 包含了用于可视化点云的函数和类,用于在3D视窗中现实点云数据
    11. #include // 估计法线
    12. #include // 直通滤波
    13. typedef pcl::PointXYZ PointT;
    14. int main(){
    15. // All the objectss needed
    16. pcl::PCDReader reader;
    17. pcl::PassThrough pass;
    18. pcl::NormalEstimation ne;
    19. pcl::SACSegmentationFromNormals seg;
    20. pcl::PCDWriter writer;
    21. pcl::ExtractIndices extract;
    22. pcl::ExtractIndices extract_normals;
    23. pcl::search::KdTree::Ptr tree (new pcl::search::KdTree());
    24. // Datasets
    25. pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
    26. pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud);
    27. pcl::PointCloud::Ptr cloud_filtered2 (new pcl::PointCloud);
    28. pcl::PointCloud::Ptr cloud_normals (new pcl::PointCloud);
    29. pcl::PointCloud::Ptr cloud_normals2 (new pcl::PointCloud);
    30. pcl::ModelCoefficients::Ptr coefficients_plane (new pcl::ModelCoefficients), coefficients_cylinder (new pcl::ModelCoefficients);
    31. pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices), inliers_cylinder (new pcl::PointIndices);
    32. // Read in the cloud data
    33. reader.read("/home/lrj/work/pointCloudData/table_scene_mug_stereo_textured.pcd", *cloud);
    34. std::cerr << "PointCloud has: " << cloud->size() << " data points." << std::endl;
    35. // Build a passthrough filter to remove spurious NaNs and scene background
    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.\n";
    41. // Estimate point normals
    42. ne.setSearchMethod(tree);
    43. ne.setInputCloud(cloud_filtered);
    44. ne.setKSearch(50);
    45. ne.compute(*cloud_normals);
    46. // Create the segmentation object for the plannar model and set all the parameters
    47. seg.setOptimizeCoefficients(true);
    48. seg.setModelType(pcl::SACMODEL_NORMAL_PLANE);
    49. seg.setNormalDistanceWeight(0.1);
    50. seg.setMethodType(pcl::SAC_RANSAC);
    51. seg.setMaxIterations(100);
    52. seg.setDistanceThreshold(0.03);
    53. seg.setInputCloud(cloud_filtered);
    54. seg.setInputNormals(cloud_normals);
    55. // Obtain the plane inliers anda coefficients
    56. seg.segment(*inliers_plane, *coefficients_plane);
    57. std::cerr << "Plane coefficients: " << *coefficients_plane << std::endl;
    58. // Extract the planar inliers from the input cloud
    59. extract.setInputCloud(cloud_filtered);
    60. extract.setIndices(inliers_plane);
    61. extract.setNegative(false);
    62. // Wirte the planar inliers to disk
    63. pcl::PointCloud::Ptr cloud_plane(new pcl::PointCloud());
    64. extract.filter(*cloud_plane);
    65. std::cerr << "PointCloud representing the planar component: " << cloud_plane->size() << " data points." << std::endl;
    66. writer.write("/home/lrj/work/pointCloudData/table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);
    67. // Remove the planar inliers, extract the rest
    68. extract.setNegative(true);
    69. extract.filter(*cloud_filtered2);
    70. extract_normals.setNegative(true);
    71. extract_normals.setIndices(inliers_plane);
    72. extract_normals.filter(*cloud_normals2);
    73. // Create the segmentation object for cylinder segmentation and set all the parameters
    74. seg.setOptimizeCoefficients(true);
    75. seg.setMethodType(pcl::SACMODEL_CYLINDER);
    76. seg.setMethodType(pcl::SAC_RANSAC);
    77. seg.setNormalDistanceWeight(0.1);
    78. seg.setMaxIterations(10000);
    79. seg.setDistanceThreshold(0.05);
    80. seg.setRadiusLimits(0, 0.01);
    81. seg.setInputCloud(cloud_filtered2);
    82. seg.setInputNormals(cloud_normals2);
    83. // Obtain the cylinder inliers and coefficients
    84. seg.segment(*inliers_cylinder, *coefficients_cylinder);
    85. std::cerr << "Cylinder cofficients: " << *coefficients_cylinder << std::endl;
    86. // Write the cylinder inliers to disk
    87. extract.setInputCloud(cloud_filtered2);
    88. extract.setIndices(inliers_cylinder);
    89. extract.setNegative(false);
    90. pcl::PointCloud::Ptr cloud_cylinder (new pcl::PointCloud ());
    91. extract.filter(*cloud_cylinder);
    92. if(cloud_cylinder->points.empty())
    93. std::cerr << "Can't find the cylindrical component." << std::endl;
    94. else
    95. {
    96. std::cerr << "PointCloud representing the cylindrical component: " << cloud_cylinder->size() << " data points.\n";
    97. writer.write("/home/lrj/work/pointCloudData/table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
    98. }
    99. return(0);
    100. }

  • 相关阅读:
    Python每日一练(牛客新题库)——第15天:字典、函数练习
    SQLite利用事务实现批量插入(提升效率)
    企业运维之服务管理 -- promethues
    C#基础知识
    面试用-常用注解
    Java Thread.interrupt()方法具有什么功能呢?
    Docker 操作镜像
    JDK JVM JRE和Java API的关系
    MySQL数据库高级查询语句及案例
    java基于微信小程序的驾校报名预约考试 uniapp小程序
  • 原文地址:https://blog.csdn.net/weixin_45824067/article/details/134553392