pcl点云索引与应用

点云索引其实就是将点云中不同点加上标签,方便后面的分类提取。有了点云的索引值可以方便的对点云进行不同操作:

以下举例说明:(代码仅显示主要部分,忽略模型设置部分)

(1)
保存一点云中某些特定的点

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//输入点云
pcl::io::loadPCDFile<pcl::PointXYZ>("~/xxx.pcd", *cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudOut(new pcl::PointCloud<pcl::PointXYZ>);//输出点云
std::vector<int > indexs = { 1, 2, 5 };//声明索引值
pcl::copyPointCloud(*cloud, indexs, *cloudOut);//将对应索引的点存储

如indexs代表点云中某些特定的点,如排序为第1,2,5三个点,这些顺序通常是有kdtree得到的。
如果没有这个参数,就是将cloud中所有的点全部填入到cloudout。


(2)
通过索引迭代输出每个聚类对应的点云

pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    ec.extract (cluster_indices);
    std::cerr << cluster_indices.size() << std::endl; //输出聚类的数目
    int j = 0;//通过两次迭代,总共产生j个聚类
    for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)//迭代j次将j个聚类中对应索引的点存到j个pcd中
    {

        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
        for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)//每个it =cluster_indices.begin ()下面对应的it->indices.begin ()代表单个聚类包含的索引,indices[i]代表单个聚类中的第i个点
        cloud_cluster->points.push_back (cloud_filtered->points[*pit]); //将
        cloud_cluster->width = cloud_cluster->points.size ();
        cloud_cluster->height = 1;
        cloud_cluster->is_dense = true;
        std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
        std::stringstream ss;
        ss << "/home/exbot/文档/pp/pcd/cloud_cluster_" << j << ".pcd";
        writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
        j++;
            /* Dynamic naming
            std::cout << "Cluster " << currentClusterNum << " has " << cluster->points.size() << " points." << std::endl;
            std::string fileName = clusterType+"_cluster" + boost::to_string(currentClusterNum) + ".pcd";
            pcl::io::savePCDFileASCII(fileName, *cluster);
            currentClusterNum++;
            */
    }
    return (0);

(3)
保存模型(平面,圆柱等等)分割中的内点
pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices);//声明指向PointIndices的共享指针
pcl::ModelCoefficients::Ptr coefficients_plane;
pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg; 
seg.setInputCloud (cloud_filtered);//输入点云
seg.setInputNormals (cloud_normals);//输入法线
seg.segment (*inliers_plane, *coefficients_plane);//计算模型内点索引
extract.setInputCloud (cloud_filtered);//输入点云
extract.setIndices (inliers_plane);//输入模型内点索引
//分割模型内点存到文件    
pcl::PointCloud<PointT>::Ptr cloud_plane (new pcl::PointCloud<PointT> ());
extract.filter (*cloud_plane);//根据输入点云和模型内点索引分割出内点对应点云
writer.write ("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);//存储模型内点对应点云

(4)
其他待补充



  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值