• pcl--第十节 点云曲面重建


    曲面重建技术在逆向工程、数据可视化、机器视觉、虚拟现实、医疗技术等领域中得到了广泛的应用 。 例如,在汽车、航空等工业领域中,复杂外形产品的设计仍需要根据手工模型,采用逆向工程的手段建立产品的数字化模型,根据测量数据建立人体以及骨骼和器官的计算机模型,在医学、定制生产等方面都有重要意义 。

    除了上述传统的行业,随着新兴的廉价 RGBD 获取设备在数字娱乐行业的病毒式扩展,使得更多人开始使用点云来处理对象并进行工程应用 。 根据重建曲面和数据点云之间的关系,可将曲面重建分为两大类:插值法和逼近法。前者得到的重建曲面完全通过原始数据点,而后者则是用分片线性曲面或其他形式的曲面来逼近原始数据点,从而使得得到的重建曲面是原始点集的一个逼近曲面。

    关联知识:

    Search、KdTree、Octree

    基于多项式重构的平滑和法线估计

    本教程说明如何使用移动最小二乘(MLS)曲面重构方法来平滑和重采样噪声数据。

    使用统计分析很难消除某些数据不规则性(由较小的距离测量误差引起)。要创建完整的模型,必须考虑光滑的表面以及数据中的遮挡。在无法获取其他扫描的情况下,一种解决方案是使用重采样算法,该算法尝试通过周围数据点之间的高阶多项式插值来重新创建表面的缺失部分。通过执行重采样,可以纠正这些小的错误,并且可以将多个扫描记录在一起执行平滑操作合并成同一个点云。

    _images/resampling_1.jpg

    在上图的左侧,我们在包含两个配准点云的数据集中看到了配准后的效果及表面法线估计。由于对齐错误,所产生的法线有噪声。在右侧,使用移动最小二乘法对表面法线估计进行平滑处理后,在同一数据集中看到了该效果。绘制每个点的曲率,作为重新采样前后特征值关系的度量,我们得到:

    _images/resampling_2.jpg

     

    1. #include
    2. #include
    3. //#include
    4. #include
    5. #include
    6. int
    7. main ()
    8. {
    9. // Load input file into a PointCloud with an appropriate type
    10. pcl::PointCloud::Ptr cloud (new pcl::PointCloud ());
    11. // Load bun0.pcd -- should be available with the PCL archive in test
    12. pcl::io::loadPCDFile ("table_scene_lms400_downsampled.pcd", *cloud);
    13. // Create a KD-Tree
    14. //pcl::search::KdTree::Ptr tree (new pcl::search::KdTree);
    15. pcl::search::KdTree::Ptr tree(new pcl::search::KdTree()); // kd树对象
    16. // Output has the PointNormal type in order to store the normals calculated by MLS
    17. pcl::PointCloud mls_points;
    18. // Init object (second point type is for the normals, even if unused)
    19. pcl::MovingLeastSquares mls;
    20. mls.setComputeNormals (true);
    21. // Set parameters
    22. mls.setInputCloud (cloud);
    23. mls.setPolynomialOrder (2);
    24. mls.setSearchMethod (tree);
    25. mls.setSearchRadius (0.03);
    26. // Reconstruct
    27. mls.process (mls_points);
    28. // Save output
    29. pcl::io::savePCDFile ("table_scene_lms400_downsampled-mls.pcd", mls_points);
    30. }

    右边是重建后,会发现比左边更平整

    在平面模型上提取凸(凹)多边形

    学习如何为平面模型上的点集提取其对应的凹多边形的例子,该例首先从点云中提取平面模型,再通过该估计的平面模型系数从滤波后的点云投影一组点集形成点云,最后为投影后的点云计算其对应的二维凸(凹)多边形。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. int
    11. main ()
    12. {
    13. pcl::PointCloud::Ptr cloud (new pcl::PointCloud), cloud_filtered (new pcl::PointCloud), cloud_projected (new pcl::PointCloud);
    14. pcl::PCDReader reader;
    15. reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);
    16. // Build a filter to remove spurious NaNs and scene background
    17. pcl::PassThrough pass;
    18. pass.setInputCloud (cloud);
    19. pass.setFilterFieldName ("z");
    20. pass.setFilterLimits (0, 1.1);
    21. pass.filter (*cloud_filtered);
    22. std::cerr << "PointCloud after filtering has: " << cloud_filtered->size () << " data points." << std::endl;
    23. pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    24. pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    25. // Create the segmentation object
    26. pcl::SACSegmentation seg;
    27. // Optional
    28. seg.setOptimizeCoefficients (true);
    29. // Mandatory
    30. seg.setModelType (pcl::SACMODEL_PLANE);
    31. seg.setMethodType (pcl::SAC_RANSAC);
    32. seg.setDistanceThreshold (0.01);
    33. seg.setInputCloud (cloud_filtered);
    34. seg.segment (*inliers, *coefficients);
    35. // Project the model inliers
    36. pcl::ProjectInliers proj;
    37. proj.setModelType (pcl::SACMODEL_PLANE);
    38. proj.setInputCloud (cloud_filtered);
    39. proj.setIndices (inliers);
    40. proj.setModelCoefficients (coefficients);
    41. proj.filter (*cloud_projected);
    42. // Create a Convex Hull representation of the projected inliers
    43. pcl::PointCloud::Ptr cloud_hull (new pcl::PointCloud);
    44. pcl::ConvexHull chull;
    45. chull.setInputCloud (cloud_projected);
    46. chull.reconstruct (*cloud_hull);
    47. std::cerr << "Convex hull has: " << cloud_hull->size () << " data points." << std::endl;
    48. pcl::PCDWriter writer;
    49. writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false);
    50. return (0);
    51. }

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. int
    11. main ()
    12. {
    13. pcl::PointCloud::Ptr cloud (new pcl::PointCloud),
    14. cloud_filtered (new pcl::PointCloud),
    15. cloud_projected (new pcl::PointCloud);
    16. pcl::PCDReader reader;
    17. reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);
    18. // Build a filter to remove spurious NaNs and scene background
    19. pcl::PassThrough pass;
    20. pass.setInputCloud (cloud);
    21. pass.setFilterFieldName ("z");
    22. pass.setFilterLimits (0, 1.1);
    23. pass.filter (*cloud_filtered);
    24. std::cerr << "PointCloud after filtering has: "
    25. << cloud_filtered->size () << " data points." << std::endl;
    26. pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
    27. pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
    28. // Create the segmentation object
    29. pcl::SACSegmentation seg;
    30. // Optional
    31. seg.setOptimizeCoefficients (true);
    32. // Mandatory
    33. seg.setModelType (pcl::SACMODEL_PLANE);
    34. seg.setMethodType (pcl::SAC_RANSAC);
    35. seg.setDistanceThreshold (0.01);
    36. seg.setInputCloud (cloud_filtered);
    37. seg.segment (*inliers, *coefficients);
    38. std::cerr << "PointCloud after segmentation has: "
    39. << inliers->indices.size () << " inliers." << std::endl;
    40. // Project the model inliers
    41. pcl::ProjectInliers proj;
    42. proj.setModelType (pcl::SACMODEL_PLANE);
    43. // proj.setIndices (inliers);
    44. proj.setInputCloud (cloud_filtered);
    45. proj.setModelCoefficients (coefficients);
    46. proj.filter (*cloud_projected);
    47. std::cerr << "PointCloud after projection has: "
    48. << cloud_projected->size () << " data points." << std::endl;
    49. // Create a Concave Hull representation of the projected inliers
    50. pcl::PointCloud::Ptr cloud_hull (new pcl::PointCloud);
    51. pcl::ConcaveHull chull;
    52. chull.setInputCloud (cloud_projected);
    53. chull.setAlpha (0.1);
    54. chull.reconstruct (*cloud_hull);
    55. std::cerr << "Concave hull has: " << cloud_hull->size ()
    56. << " data points." << std::endl;
    57. pcl::PCDWriter writer;
    58. writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false);
    59. return (0);
    60. }

     无序点云的快速三角化

    本小节介绍了怎样使用贪婪投影三角化算法对有向点云进行三角化,具体方法是先将有向点云投影到某一局部二维坐标平面内,再在坐标平面内进行平面内的三角化,再根据平面内三位点的拓扑连接关系获得一个三角网格曲面模型。贪婪投影三角化算法原理是处理一系列可以使网格“生长扩大”的点(边缘点)延伸这些点直到所有符合几何正确性和拓扑正确性的点都被连上。该算法的优点是可以处理来自一个或者多个扫描仪扫描得到并且有多个连接处的散乱点云。但该算法也有一定的局限性,它更适用于采样点云来自于表面连续光滑的曲面且点云密度
    变化比较均匀的情况。该算法的三角化过程是局部进行的,首先沿着一点的法线将该点投影到局部二维坐标平面内并连接其他悬空点,然后再进行下一点。所以这里设置如下参数:

    1. 函数 SetMaximumNearestNeighbors(unsigned)和 SetMu(double),这两个函数的作用是控制搜索邻域大小。前者定义了可搜索的邻域个数,后者规定了被样本点搜索其邻近点的最远距离(是为了适应点云密度的变化),特征值一般是 50~100和2.5~3(或者15每栅格)。
    2. 函数SetSearchRadius(double),该函数设置了三角化后得到的每个三角形的最大可能边长。
    3. 函数SetMinimumAngle(double)和 SetMaximumAngle(double),这两个函数是三角化后每个三角形的最大角和最小角。两者至少要符合一个,典型值分别是10和120°(弧度)。
    4. 函数 SetMaximumSurfaceAgle(double)和 SetNormalConsistency(bool),这两个函数是为了处理边缘或者角很尖锐以及一个表面的两边非常靠近的情况。为了处理这些特殊情况,函数SetMaximumSurfaceAgle(double)规定如果某点法线方向的偏离超过指定角度(注:大多数表面法线估计方法可以估计出连续变化的表面法线方向,即使在尖锐的边缘条件下),该点就不连接到样本点上。该角度是通过计算法向线段(忽略法线方向)之间的角度。函数SetNormalConsistency(bool)保证法线朝向,如果法线方向一致性标识没有设定,就不能保证估计出的法线都可以始终朝向一致。第一个函数特征值为45(弧度)第二个函数默认值为false。
    1. #include
    2. #include
    3. #include // for KdTree
    4. #include
    5. #include
    6. #include
    7. #include
    8. int
    9. main ()
    10. {
    11. // Load input file into a PointCloud with an appropriate type
    12. pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
    13. pcl::PCLPointCloud2 cloud_blob;
    14. pcl::io::loadPCDFile ("bunny.pcd", cloud_blob);
    15. pcl::fromPCLPointCloud2 (cloud_blob, *cloud);
    16. //* the data should be available in cloud
    17. // Normal estimation*
    18. pcl::NormalEstimation n;
    19. pcl::PointCloud::Ptr normals (new pcl::PointCloud);
    20. pcl::search::KdTree::Ptr tree (new pcl::search::KdTree);
    21. tree->setInputCloud (cloud);
    22. n.setInputCloud (cloud);
    23. n.setSearchMethod (tree);
    24. n.setKSearch (20);
    25. n.compute (*normals);
    26. //* normals should not contain the point normals + surface curvatures
    27. // Concatenate the XYZ and normal fields*
    28. pcl::PointCloud::Ptr cloud_with_normals (new pcl::PointCloud);
    29. pcl::concatenateFields (*cloud, *normals, *cloud_with_normals);
    30. //* cloud_with_normals = cloud + normals
    31. // Create search tree*
    32. pcl::search::KdTree::Ptr tree2 (new pcl::search::KdTree);
    33. tree2->setInputCloud (cloud_with_normals);
    34. // Initialize objects
    35. pcl::GreedyProjectionTriangulation gp3;
    36. pcl::PolygonMesh triangles;
    37. // Set the maximum distance between connected points (maximum edge length)
    38. gp3.setSearchRadius (0.02);
    39. // Set typical values for the parameters
    40. gp3.setMu (2.5);
    41. gp3.setMaximumNearestNeighbors (100);
    42. gp3.setMaximumSurfaceAngle(M_PI/4); // 45 degrees
    43. gp3.setMinimumAngle(M_PI/18); // 10 degrees
    44. gp3.setMaximumAngle(2*M_PI/3); // 120 degrees
    45. gp3.setNormalConsistency(false);
    46. // Get result
    47. gp3.setInputCloud (cloud_with_normals);
    48. gp3.setSearchMethod (tree2);
    49. gp3.reconstruct (triangles);
    50. // Additional vertex information
    51. std::vector<int> parts = gp3.getPartIDs();
    52. std::vector<int> states = gp3.getPointStates();
    53. // Finish
    54. pcl::visualization::PCLVisualizer viewer("Triangulation");
    55. viewer.setBackgroundColor(0.0, 0.0, 0.0);
    56. viewer.addPointCloud(cloud, "input_cloud");
    57. viewer.addPolygonMesh(triangles, "triangles", 0);
    58. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "input_cloud");
    59. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 1.0, 0.0, "triangles");
    60. viewer.spin();
    61. return (0);
    62. }

  • 相关阅读:
    SpringBoot 全局事务配置
    Functional equation (L-function)
    医学大模型开源项目: 医学大模型的局限性 + 改进思路
    Java ~ Reference ~ FinalReference/Finalizer
    使用扩散模型从文本生成图像
    Golang编译生成可执行程序的三种方法
    git如何手动解决冲突文件
    uos桌面专业版下载多架构软件安装包
    基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的稻田虫害检测系统详解(深度学习+Python代码+UI界面+训练数据集)
    vscode 上传项目到gitlab
  • 原文地址:https://blog.csdn.net/weixin_42398658/article/details/133135633