PCL迭代提取点云的索引,并用向量存储提取的PCL点云

        利用pcl进行点云索引提取时,经常需要迭代分割提取多块子点云,为了方便存储管理,通常使用向量来进行。这时有两种思路存储分割后的子点云:一种是将子点云的索引存入向量,另一种是将点云存入向量。

      需要的注意的是,如果将子点云的索引存入向量,容易出现问题:循环迭代的输入点云发生了变化,所以得到的子点云索引是新输入点云中的索引,并非最初的输入点云的索引。所以,最可靠的方法是将子点云存入向量。这时关于点云向量的定义如下:

std::vector<pcl::PointCloud<pcl::PointXYZ>, Eigen::aligned_allocator<pcl::PointXYZ> > clouds_vector;

分割提取多个子点云,并存入点云向量的代码如下:

int i = 0, nr_points = (int)cloud2->points.size();
while (cloud2->points.size()>0.3*nr_points){
    pcl::PointCloud<PointT> cloud;
    // 从余下的点云中分割最大平面组成部分
    seg.setInputCloud(cloud2);
    seg.segment(*inliers, *coefficients);
    if (inliers->indices.size()==0)
    {
        cout << "Could not estimate the model for the given dataset." << endl;
        break;
    }
    //分离内存
    extract.setInputCloud(cloud2);
    extract.setIndices(inliers);  //设置分割后的内点为需要提取的点集
    extract.setNegative(false);   //设置提取内点而非外点
    extract.filter(cloud);      //提取输出存储到 cloud
    cout << "PointCloud representing the model component: " << cloud.width * cloud.height << " data points." <<endl;
    clouds_vector.push_back(cloud);  //将点云存入点云向量。
    // 创建滤波器对象, 剔除已经判断的模型点
    extract.setNegative(true);
    extract.filter(*cloud_f);
    cloud2->swap(*cloud_f);  //j交换两个点云
    i++;
}
Enjoy!





  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
计算法向量可以使用 pcl::NormalEstimation 类,可视化可以使用 pcl::visualization::PCLVisualizer 类。 以下是一个简单的示例代码: ```cpp #include <iostream> #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/io/pcd_io.h> #include <pcl/visualization/pcl_visualizer.h> int main(int argc, char** argv) { // Load point cloud data from file pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("cloud.pcd", *cloud); // Estimate normals pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud(cloud); pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>()); ne.setSearchMethod(tree); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); ne.setRadiusSearch(0.03); // set the radius of the sphere for normal estimation ne.compute(*cloud_normals); // Visualize the point cloud and its normals pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer"); viewer.setBackgroundColor(0.0, 0.0, 0.0); viewer.addPointCloud<pcl::PointXYZ>(cloud, "cloud"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud"); viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, cloud_normals, 10, 0.05, "normals"); viewer.spin(); return 0; } ``` 在这个例子中,我们首先从文件中加载点云数据,然后使用 pcl::NormalEstimation 类估计点云的法向量。接着,我们使用 pcl::visualization::PCLVisualizer 类将点云和法向量可视化。在可视化中,我们使用 addPointCloud() 函数将点云添加到可视化窗口中,并使用 setPointCloudRenderingProperties() 函数设置点云的大小。然后,我们使用 addPointCloudNormals() 函数将法向量添加到点云中,并设置法向量的长度和大小。最后,我们使用 spin() 函数显示可视化窗口并等待用户交互。 注意,在使用 PCL 可视化库时,可能需要在编译时链接对应的库文件。例如,在 Ubuntu 系统中,可以使用以下命令编译上述代码: ``` g++ -o main main.cpp -I/usr/include/pcl-1.8 -lpcl_visualization -lboost_system ``` 其中 -I 指定了 PCL 库的头文件路径,-l 指定了需要链接的库文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值