基于C++ 和pcl实现以下格式的点云txt文档转pcd格式。
使用qt 的console实现:
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QDirIterator>
#include <iostream>
#include <fstream>
#include <strstream>
#include <vector>
#include <pcl/io/pcd_io.h> //文件输入输出
#include <pcl/point_types.h> //点类型相关定义
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
bool readTxtFile(const string &fileName, const char tag, const PointCloudT::Ptr &pointCloud)
{
cout << "reading file start..... " << endl;
ifstream fin(fileName);
string linestr;
vector<PointT> myPoint;
while (getline(fin, linestr))
{
vector<string> strvec;
string s;
stringstream ss(linestr);
while (getline(ss, s, tag))
{
strvec.push_back(s);
}
if (strvec.size() < 3){
cout << "格式不支持" << endl;
return false;
}
PointT p;
p.x = stod(strvec[0]);
p.y = stod(strvec[1]);
p.z = stod(strvec[2]);
myPoint.push_back(p);
}
fin.close();
//转换成pcd
pointCloud->width = (int)myPoint.size();
pointCloud->height = 1;
pointCloud->is_dense = false;
pointCloud->points.resize(pointCloud->width * pointCloud->height);
for (int i = 0; i < myPoint.size(); i++)
{
pointCloud->points[i].x = myPoint[i].x;
pointCloud->points[i].y = myPoint[i].y;
pointCloud->points[i].z = myPoint[i].z;
}
cout << "reading file finished! " << endl;
cout << "There are " << pointCloud->points.size() << " points!" << endl;
return true;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString dirPath = "/home/csl/livox_txt/7.27textfile/end/txt";
QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs, QDirIterator::Subdirectories);
while(it.hasNext())
{
it.next();
if(it.fileInfo().suffix() == "txt")
{
QString basename = it.fileInfo().baseName();
QString strNew = QString("%1").arg(basename.toInt(), 4, 10, QLatin1Char('0'));
QString fullname = it.fileInfo().absoluteFilePath();
qDebug() << fullname;
PointCloudT::Ptr cloud(new PointCloudT);
readTxtFile(fullname.toStdString(),' ',cloud);
//3.点云输出
QString out_dir = "/home/csl/livox_txt/7.27textfile/end/pcd/";
QString out_ex =".pcd";
QString pcd_name = out_dir + strNew+out_ex;
pcl::PCDWriter writer;
writer.writeASCII<PointT>(pcd_name.toStdString(), *cloud);
}
}
return a.exec();
}
标签:size,文件,include,cout,pointCloud,myPoint,点云,txt,points
From: https://blog.csdn.net/qq_33912182/article/details/140531559