C++ 多帧 pcd 格式点云转化成 bin格式

背景

点云由pcd 格式转化成 bin格式,节省内存。

代码

#include <string>
#include <iostream>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>


bool get_filelist_from_dir(std::string _path, std::vector<std::string>& _files)
{
    DIR* dir = opendir(_path.c_str());
    struct dirent* ptr;
    std::vector<std::string> file;
    while((ptr = readdir(dir)) != NULL)
    {
        if(ptr->d_name[0] == '.')	continue;
        file.push_back(ptr->d_name);
    }
    closedir(dir);
    sort(file.begin(), file.end());
    _files = file;
}

void convertPCDtoBin(std::string & in_file, std::string & out_file)
{
    pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);

    if (pcl::io::loadPCDFile<pcl::PointXYZI>(in_file, *cloud) == -1)
    {
        std::string err = "Couldn't read file " + in_file;
        PCL_ERROR(err.c_str());
        return;
    }
    std::cout << "Loaded "
         << cloud->width * cloud->height
         << " data points from "
         << in_file
         << " with the following fields: "
         << std::endl;

    std::ofstream output(out_file.c_str(), std::ios::out | std::ios::binary);
    float intensity = 1.0;
    for (int j = 0; j < cloud->size(); j++)
    {
        output.write((char*)& cloud->at(j).x, sizeof(cloud->at(j).x));
        output.write((char*)& cloud->at(j).y, sizeof(cloud->at(j).y));
        output.write((char*)& cloud->at(j).z, sizeof(cloud->at(j).z));
        intensity = cloud->at(j).intensity / 255.0;
        output.write((char*)& intensity, sizeof(intensity));
        /*std::cout << cloud->at(j).x << " "
                  << cloud->at(j).y << " "
                  << cloud->at(j).z << " "
                  << intensity << std::endl;*/
    }
    output.close();
}

int fileNum =20000;
std::string pcd_dir ="/media/wxf/Elements/data/preADD/combine_data/cloud_origin_addZ/";
std::string bin_dir ="/media/wxf/Elements/data/preADD/combine_data/bin/";


int main(){

    std::vector<std::string> pcdNames;
    pcdNames.reserve(fileNum);
    get_filelist_from_dir(pcd_dir, pcdNames);

    for(int i = 0; i < pcdNames.size(); i++) {
        std::cout << "<<<<<<<<<<<<<<<<<<<<<<<<< " << i << " >>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
        std::string pcdfile = pcd_dir + pcdNames[i].c_str();
        std::string frameName =  pcdNames[i].substr(0,pcdNames[i].find('.',0));
        std::string binfile = bin_dir + frameName +".bin";
        convertPCDtoBin(pcdfile, binfile);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值