笔者之前用cloudpare处理点云数据,由于项目原因,都是txt格式的;最近开始学习PCL点云库,大家都知道这个要求点云是pcd格式的,介于此,先学习点云格式的转换。在网上看了一些博客,大都是通过文件指针来实现格式转换,本文用文件流的方式实现格式的转换。注意:转换时基于pcl点云库的。
1.txtToPCD
原理: (1)获取txt点云的数量
(2)定义pcl cloude对象,设置cloud的大小
(3)通过文件流将txt数据导入cloud
(4)将cloud点云保存为pcd格式
代码如下:
#include<iostream>
#include<fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <sstream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
using namespace std;
struct point
{
double x;
double y;
double z;
double i;
};
int getTxtNumber(char *p )
{
ifstream f(p);
char c;
int num = 0;
while (!f.eof())
{
c = f.get();
if (c=='\n')
num++;
}
f.close();
return num;
}
void txt2pcd(int count,char* filename)
{
ifstream t(filename);
double x,y,z,intensity;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
size_t i= 0;
point temp;
cloud->height = count;
cloud->width = 1;
cloud->is_dense = false;
cloud->points.resize(cloud->height*cloud->width);
while (!t.eof())
{
t >> setiosflags(ios::fixed) >>setprecision(4) >> x >> y >> z ;
cloud->points[i].x = x;
cloud->points[i].y = y;
cloud->points[i].z = z;
i++;
}
cout << "output point number is " << i << endl;
t.close();
pcl::io::savePCDFile("i.pcd", *cloud);
}
int main()
{
//pcd2txt
char *filename = "1.txt";
int count = getTxtNumber(filename);
cout << "There are " << count << " points in " << filename << endl;
txt2pcd(count,filename);
return0;
}
这是基于pcl库的格式转换,当然不用pcl库同样可以转换为pcd文件。首先要了解pcd格式的头文件
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 5
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 5
DATA ascii
思路是:使用文件输入流将txt数据先导入容器,然后根据在用输出流输出文件头和数据,此时用到容器,所以就不需要再用
getTxtNumber获取txt文档的数量了,因为容器本身的size已经确定
主函数代码如下:
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
using namesapce std;
struct point
{
double x;
double y;
double z;
};
int main()
{
vector<point>src;
ifstream infile2("1.txt");
if (!infile2)
{
cerr << "open file failed" << endl;
return -1;
}
point temp;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->height = count;
cloud->width = 1;
cloud->is_dense = false;
cloud->points.resize(cloud->height*cloud->width);
while (!infile2.eof())
{
infile2 >> temp.x >> temp.y >> temp.z;
src.push_back(temp);
}
infile2.close();
string pcdHeader[11] = { { "# .PCD v0.7 - Point Cloud Data file format " }, {"VERSION 0.7"},
{ "FIELDS x y z" }, { "SIZE 4 4 4" }, { "TYPE F F F" }, { "COUNT 1 1 1 " }, { "WIDTH 1" }, { "HEIGHT " },
{ "VIEWPOINT 0 0 0 1 0 0 0" }, { "POINTS " }, {"DATA ascii"}
};
output to pcd file
ofstream outpcd("test.pcd");
if (!outpcd)
cerr << "open file failed" << endl;
for (int i = 0; i < 11; i++)
{
outpcd << pcdHeader[i];
if (i == 7|| i==9 )
outpcd << count;
outpcd << '\n';
}
for (size_t j = 0; j< src.size(); j++)
{
outpcd << setiosflags(ios::fixed)<<setprecision(6) << src[j].x << " ";
outpcd << setiosflags(ios::fixed) << setprecision(6) << src[j].y << " ";
outpcd << setiosflags(ios::fixed) << setprecision(6) << src[j].z <<endl;
}
outpcd.close();
for (size_t i = 0; i < cloud->points.size(); i++)
{
//cout << src[i].x<<" "<< src[i].y<<" "<< src[i].z << endl;
cloud->points[i].x = src[i].x;
cloud->points[i].y = src[i].y;
cloud->points[i].z = src[i].z;
}
pcl::io::savePCDFile("2.pcd", *cloud);
return 0;
}