[小程序系列] C++ PCL pcd与txt转换

[小程序系列] C++ PCL pcd与txt转换

引言

记录一下简单的点云txt文件的转换,用一个小程序实现。以pcd为例,利用stream实现即可。

代码

#include<iostream>
#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
#include <fstream>
#include <string.h>
#include <pcl/visualization/cloud_viewer.h>

void Txt2Cloud(pcl::PointCloud<pcl::PointXYZI>& cloud,const std::string & txt_path){
    char buffer[255];
    std::fstream out;
    out.open(txt_path, ios::in);
    while (!out.eof()){
        out.getline(buffer, 256, '\n');
        std::istringstream record(buffer);
        std::string data;
        std::vector<float> point_d;
        while (record >> data) {
            point_d.push_back(atof(data.c_str())); //将获取的数据存入vector中
        }
        if(point_d.size() !=4 ) break;
        
        pcl::PointXYZI pt;
        pt.x = point_d[0];
        pt.y = point_d[1];
        pt.z = point_d[2];
        pt.intensity = point_d[3];
        cloud.points.push_back(pt);
    }
    out.close();
    std::cout<<"cloud size: "<<cloud.points.size()<<std::endl;
    return;
}

void Cloud2Txt(const pcl::PointCloud<pcl::PointXYZI> & cloud,const std::string txt_path){
    std::fstream fs;
    fs.open(txt_path,std::fstream::out);
    for (size_t i = 0; i < cloud.points.size(); ++i){
        fs << cloud.points[i].x << "  "
            << cloud.points[i].y << "  "
            << cloud.points[i].z << "  "
            << cloud.points[i].intensity << "\n";
    }
    fs.close();
    return;
}

int main(int argc, char** argv){
	pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
	if(pcl::io::loadPCDFile<pcl::PointXYZI>(argv[1], *cloud) == -1){
		PCL_ERROR("Couldn't read file *.pcd\n");
	    return(-1);
	}
    std::string txt_path = std::string (argv[2]);
    Cloud2Txt(*cloud,txt_path);
    pcl::PointCloud<pcl::PointXYZI> out_cloud;
    std::cout<<"save txt."<<std::endl;
    Txt2Cloud(out_cloud,txt_path);
    pcl::visualization::CloudViewer viewer("cloud viewer");
    viewer.showCloud(out_cloud.makeShared());
    while(!viewer.wasStopped()){}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值