0
点赞
收藏
分享

微信扫一扫

点云边界提取及可视化

小安子啊 2022-02-01 阅读 75
#include <iostream>
#include <pcl/console/parse.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/features/boundary.h>
#include <math.h>
#include <boost/make_shared.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/pcd_io.h>
#include<pcl/io/io.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/features/normal_3d.h>

//#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/covariance_sampling.h>
#include <pcl/filters/normal_space.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/boundary.h>
#include <pcl/io/ply_io.h>


int estimateBorders(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, float re, float reforn)
{

    pcl::PointCloud<pcl::Boundary> boundaries;//边界类型
    pcl::BoundaryEstimation<pcl::PointXYZ, pcl::Normal, pcl::Boundary> boundEst;//边界法线
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normEst;//估计法线
    pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);//法线
    //计算法向量
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_boundary(new pcl::PointCloud<pcl::PointXYZ>);//边界点云
    normEst.setInputCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr(cloud));
    normEst.setRadiusSearch(reforn);
    normEst.compute(*normals);
    //计算边界
    boundEst.setInputCloud(cloud);
    boundEst.setInputNormals(normals);//设置边界估计的法线,因为边界估计依赖于法线
    boundEst.setRadiusSearch(re);//设置边界估计所需要的半径,//这里的Threadshold为一个浮点值,可取点云模型密度的10倍
    boundEst.setAngleThreshold(M_PI / 4);//边界估计时的角度阈值M_PI / 4  并计算k邻域点的法线夹角,若大于阈值则为边界特征点
    boundEst.setSearchMethod(pcl::search::KdTree<pcl::PointXYZ>::Ptr(new pcl::search::KdTree<pcl::PointXYZ>));
    boundEst.compute(boundaries);

    for (int i = 0; i < cloud->points.size(); i++)
    {

        if (boundaries[i].boundary_point > 0)
        {
            cloud_boundary->push_back(cloud->points[i]);
        }
    }

    /*boost::shared_ptr<pcl::visualization::PCLVisualizer> MView(new pcl::visualization::PCLVisualizer("点云库PCL从入门到精通案例"));

    int v1(0);
    MView->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
    MView->setBackgroundColor(0.3, 0.3, 0.3, v1);
    MView->addText("Raw point clouds", 10, 10, "v1_text", v1);
    int v2(0);
    MView->createViewPort(0.5, 0.0, 1, 1.0, v2);
    MView->setBackgroundColor(0.5, 0.5, 0.5, v2);
    MView->addText("Boudary point clouds", 10, 10, "v2_text", v2);

    MView->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud", v1);
    MView->addPointCloud<pcl::PointXYZ>(cloud_boundary, "cloud_boundary", v2);
    MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "sample cloud", v1);
    MView->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "cloud_boundary", v2);
    MView->addCoordinateSystem(1.0);
    MView->initCameraParameters();

    MView->spin();*/
    pcl::io::savePCDFileBinary("1.pcd", *cloud_boundary);
    return 0;
}
int
main(int argc, char** argv)
{
    srand(time(NULL));

    float re, reforn;
    re = std::atof(argv[2]);//边界半径
    reforn = std::atof(argv[3]);//法线半径
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_src(new pcl::PointCloud<pcl::PointXYZ>);



    //Laden der PCD-Files 
    pcl::io::loadPCDFile(argv[1], *cloud_src);

    estimateBorders(cloud_src, re, reforn);

    return 0;
}

点云边界提取的两个重要参数设置

boundEst.setRadiusSearch(re);//设置边界估计所需要的半径,//这里的Threadshold为一个浮点值,可取点云模型密度的10倍
    boundEst.setAngleThreshold(M_PI / 4);//边界估计时的角度阈值M_PI / 4  并计算k邻域点的法线夹角,若大于阈值则为边界特征点
举报

相关推荐

0 条评论