PCL读取csv文件并且存储为pcd文件

参考连接:

https://icode.best/i/92490844638260

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
using namespace std;

//删除字符串中空格,制表符tab等无效字符
string Trim(string& str)
{
	//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
	str.erase(0, str.find_first_not_of(" \t\r\n"));
	str.erase(str.find_last_not_of(" \t\r\n") + 1);
	return str;
}

void csv2pointCloud(std::string filename, pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud)
{
	cloud->points.clear();
	ifstream fin(filename); //打开文件流操作
	string line;
	while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
	{
	//	cout << "原始字符串:" << line << endl; //整行输出
		istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
		vector<string> fields; //声明一个字符串向量
		string field;
		while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
		{
			fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
		}
		pcl::PointXYZ p;

		p.x = atof(Trim(fields[0]).c_str()); //清除掉向量fields中第一个元素的无效字符,并赋值给变量name
		p.y = atof(Trim(fields[1]).c_str()); //清除掉向量fields中第二个元素的无效字符,并赋值给变量age
		p.z = atof(Trim(fields[2]).c_str()); //清除掉向量fields中第三个元素的无效字符,并赋值给变量birthday
	//	cout << "处理之后的字符串:" << p.x << "\t" << p.y << "\t" << p.z << endl;
		cloud->points.push_back(p);
	}
	
}

int main()
{ 
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	std::string filename = "00001.csv";
	csv2pointCloud(filename, cloud);
	// 注意,以下2行代码很重要 否则无法存储pcd文件
	cloud->width = (int)cloud->points.size();
	cloud->height = 1;
	pcl::io::savePCDFileASCII("csv.pcd", *cloud);

	return 0;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值