pcl点云 Voxel map

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <pcl/io/pcd_io.h>
#include <pcl/octree/octree.h>
#include <boost/thread/thread.hpp>


using namespace std;

void cloud_voxel_map(pcl::PointCloud<PointI>::Ptr cloud, float resolution, string out_path){

    // ---------------------使用八叉树快速构建体素索引------------------------------
    pcl::octree::OctreePointCloud<PointI> octree(resolution);
    octree.setInputCloud(cloud);
    octree.addPointsFromInputCloud(); // 从点云中构建八叉树
    pcl::Indices pointIdxVec;         // 体素内点的索引
    //-----------------------------开始分块-----------------------------------------
    pcl::PointCloud<PointI>::Ptr seg_cloud(new pcl::PointCloud<PointI>);
    int begin = 0;
    // 构建八叉树叶子节点迭代器,遍历八叉树
    for (auto iter = octree.leaf_breadth_begin(); iter != octree.leaf_breadth_end(); ++iter)
    {
        auto key = iter.getCurrentOctreeKey(); // 获取当前迭代器八叉树节点的八叉树键
        auto it_key = octree.findLeaf(key.x, key.y, key.z); // 检查是否存在于八叉树中
        if (it_key != nullptr)
        {
            //从八叉树叶子节点中获取单个叶子容器中的点索引
            pointIdxVec = iter.getLeafContainer().getPointIndicesVector();
            if (pointIdxVec.size() < 5) // 体素内点个数为0则跳过
            {
                continue;
            }
            else
            {

                std::stringstream ss;
                ss << out_path<< "block_" << begin << ".pcd";
                string ss_temp = ss.str();
                pcl::copyPointCloud(*cloud, pointIdxVec, *seg_cloud);
                if(seg_cloud->size() >= 5)
                    pcl::io::savePCDFileBinary(ss.str(), *seg_cloud);
                cout << "第[" << begin << "]块点云分割完毕!   " << seg_cloud->size() << endl;
                begin++;

            }

            seg_cloud->clear(); // 每分割一次,清空一下容器,进而提高电脑工作效率
        }
    }
}


int main()
{

    std::string path = (argc > 1) ? argv[1] : "../test_data/smoke_svm/background.pcd";// 原始点云所在文件夹
    std::string outpath = (argc > 2) ? argv[2] : "../test_data/smoke_svm/neg_test/"; // 分块保存路径文件夹名
    float resolution = 1;        // 设置体素分辨率
    //-------------------------- 加载点云 --------------------------
    pcl::PointCloud<PointI>::Ptr cloud(new pcl::PointCloud<PointI>);
    if (pcl::io::loadPCDFile(path, *cloud) < 0)
    {
        PCL_ERROR("Couldn't read file \n");
        system("pause");
        return -1;
    }


    cloud_voxel_map(cloud,resolution,outpath);



	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值