3D视觉学习之路——基于pcl从CAD模型中获取单视角点云

CAD模型单视角点云提取
本文介绍了一种从CAD模型中提取单视角点云的方法,利用PCL库中的renderViewTesselatedSphere函数实现,并提供了完整的代码示例。

学习PCL也有将近两个月了,现在想开下博客记录一些自己的学习心得。
由于项目需要,现在需要从CAD模型中提取单视角点云。pcl提取单视角点云的函数名是renderViewTesselatedSphere,属于pcl::visualization::PCLVisualizer中的成员函数。

下面是关键代码:

/*+++++++++++++++++++++++++单视角点云获取+++++++++++++++++++++++++++++++*/
    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkSTLReader> readerQuery = vtkSmartPointer<vtkSTLReader>::New();
    //读取CAD模型
    readerQuery->SetFileName("aa.STL");
    readerQuery->Update();
    polydata = readerQuery->GetOutput();
    polydata->GetNumberOfPoints());

    //单视角点云获取
    float resx = 512;
    float resy = resx;
    std::vector<pcl::PointCloud<pcl::PointXYZ>, Eigen::aligned_allocator<pcl::PointCloud<pcl::PointXYZ> > > views_xyz;
    std::vector<Eigen::Matrix4f, Eigen::aligned_allocator<Eigen::Matrix4f> > poses;
    std::vector<float> entropies;
    pcl::visualization::PCLVisualizer vis;
    vis.addModelFromPolyData(polydata, "mesh", 0);
    vis.setRepresentationToSurfaceForAllActors();
    vis.renderViewTesselatedSphere(resx, resy, views_xyz, poses, entropies, 1, 90, 1, FALSE);
    for (int i = 0; i < views_xyz.size(); i++)
    {
        pcl::PointCloud<pcl::PointXYZ> views_cloud;
        pcl::transformPointCloud<pcl::PointXYZ>(views_xyz[i], views_cloud, poses[i]);

        std::stringstream ss;
        ss << "cloud_view_" << i << ".ply";
        pcl::io::savePLYFile(ss.str(), views_cloud);
    }



    while (!vis.wasStopped())
    {
    }

该代码实现的是从一个cad模型中获取80个视角的点云。

虽然给定引用中未直接提及使用PCL从三维模型获取视角点云的方法,但可以基于PCL的一般功能和常见做法进行推测。 通常,使用PCL从三维模型获取视角点云可能需要以下步骤: 1. **加载三维模型**:使用PCL的文件读取功能将三维模型文件(如PLY、STL等)加载到PCL点云数据结构中。 ```cpp #include <pcl/io/ply_io.h> #include <pcl/point_cloud.h> #include <pcl/point_types.h> pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPLYFile<pcl::PointXYZ> ("your_model.ply", *cloud) == -1) { PCL_ERROR ("Couldn't read the model file\n"); return (-1); } ``` 2. **定义视角**:确定获取点云视角,包括视点的位置、方向等信息。 ```cpp Eigen::Vector3f view_point(0.0, 0.0, 0.0); // 视点位置 Eigen::Vector3f view_direction(0.0, 0.0, 1.0); // 视点方向 ``` 3. **模拟投影**:根据定义的视角,模拟从该视角对三维模型进行投影,只保留可见的点。这可能涉及到使用射线追踪等算法来确定哪些点是可见的。 ```cpp // 这里需要实现射线追踪算法,伪代码如下 pcl::PointCloud<pcl::PointXYZ>::Ptr single_view_cloud (new pcl::PointCloud<pcl::PointXYZ>); for (const auto& point : cloud->points) { // 检查点是否可见 if (is_visible(point, view_point, view_direction)) { single_view_cloud->points.push_back(point); } } single_view_cloud->width = single_view_cloud->points.size(); single_view_cloud->height = 1; ``` 4. **保存视角点云**:将获取到的视角点云保存到文件中。 ```cpp pcl::io::savePLYFileASCII ("single_view_cloud.ply", *single_view_cloud); ```
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值