《PCL》批量txtTOpcd

针对于批量处理问题的解决,根据需要将txt格式数据转换为pcd格式

适用于txt文件名按照顺序排列的文件夹

电脑存放位置:F:\muchlyBackup\muchlyBackup\BatchProcessingFunctionTxtTOPcd

//批量将 txt 转换成 pcd 格式   适用于txt文件名按照顺序排列的文件夹
#include<iostream>
#include<fstream>
#include <string>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

using namespace std;

typedef struct txtPoint_3D{
	double x;
	double y;
	double z;
}txtPoint_3D;

/*
从 txt 格式的文件中读取点云数据,然后再存储为pcd格式文件
readPath 读取 txt 格式文件路径
savePath 写入 pcd 格式文件路径
*/
void txtTOpcd(const char* readPath,const char* savePath)//为什么 char* 类型,因为路径本身就是c语言形式的字符串,进行类型转换时的需要。
{
	FILE *fp_txt;
	txtPoint_3D txtPoint;          ///中间变量,用于存放单行数据的变量
	vector<txtPoint_3D> txtPoints;   ///用于存放整个txt文档内的数据变量
	

	if ((fp_txt = fopen(readPath, "r")) !=NULL)  ///if (fp_txt = fopen(readPath, "r"))   //可以简化NULL==0  
	{
		while (fscanf(fp_txt, "%lf;%lf;%lf", &txtPoint.x, &txtPoint.y, &txtPoint.z) != EOF)  ///判断是否读到最后一行
		{
			txtPoints.push_back(txtPoint);     ///读一行放进入
		}
	}

	///将txt文档中的全部数据放到 pcd 中,先给这个cloud 分配空间大小 再放入
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

	cloud->width = txtPoints.size();
	cloud->height = 1;
	cloud->is_dense = false;
	cloud->points.resize(cloud->width * cloud->height);

	for (size_t i = 0; i < cloud->points.size(); ++i){
		cloud->points[i].x = txtPoints[i].x;
		cloud->points[i].y = txtPoints[i].y;
		cloud->points[i].z = txtPoints[i].z;
	}
	pcl::io::savePCDFileASCII(savePath, *cloud);
}


int main()
{
	for (int i = 0; i < 6; ++i)
	{
		cout << i << endl;
		string a = "home\\2-" + to_string(i) + ".txt";
		string b = "home01\\2-" + to_string(i) + ".pcd";
		/*strcpy(readPath, a.c_str());
		strcpy(savePath, b.c_str());*/

		const char* readPath = a.c_str();
		const char* savePath = b.c_str();

		txtTOpcd(readPath, savePath);
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值