环境:Ubuntu
如果是windows系统,读取文件的代码可参考:https://blog.csdn.net/qq_19332527/article/details/78404583
前提:
1)在Linux系统编译环境下,C++头文件<io.h>。在查询到findfirst的百度百科的时候有如下的解释:
Linux下的FindFirst
在linux操作系统下,编译器用findfirst(),而不是_findfirst().
linux操作系统下的查找文件的操作,需要包含dirent.h头文件.
但是尝试之后仍旧不行就放弃了。。。。(怎么总是放弃啊。。。。。但是真的查好久了,如果有新的解答,欢迎评论私信!!谢谢!!)
2)另尝试用python-pcl来完成,但是这个安装实在有点坑,放弃了。最后还是用C++完成。
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <fstream>
#include <string>
#include <vector>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/console/time.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/visualization/cloud_viewer.h>
using namespace std;
void scanFile(char *szDir){
DIR *pDir = NULL;
struct dirent *pFile = NULL;
pDir = opendir(szDir);
if (pDir == NULL) return;
vector<string> result;
while ((pFile = readdir(pDir)) != NULL){
if (pFile->d_type & DT_DIR){
if (strcmp(pFile->d_name, ".") ==0
|| strcmp(pFile->d_name, "..") == 0) continue;
char Path[256];
int len = strlen(szDir);
strncpy(Path, szDir, len + 1);
if (szDir[len-1] !='/') strncat(Path, "/", 2);
strncat(Path, pFile->d_name, strlen(pFile->d_name) + 1);
scanFile(Path);
}
else {
result.push_back(pFile->d_name);
}
}
string path(szDir);
sort(result.begin(), result.end());
for (int i=4; i<result.size(); i++){
pcl::PointCloud<pcl::PointXYZI> cloud_1;
pcl::PointCloud<pcl::PointXYZI> cloud_2;
pcl::PointCloud<pcl::PointXYZI> cloud_3;
pcl::PointCloud<pcl::PointXYZI> cloud_4; //如果是三帧,则将cloud_4,cloud_5这几句相关的去掉即可
pcl::PointCloud<pcl::PointXYZI> cloud_5;
pcl::console::TicToc tt;
cout<<szDir<<endl;
pcl::PCDReader reader1;
string path1 = path + '/' + result[i-4];
reader1.read(path1, cloud_1);
pcl::PCDReader reader2;
string path2 = path + '/' + result[i-3];
reader2.read(path2, cloud_2);
pcl::PCDReader reader3;
string path3 = path +'/' + result[i-2];
reader3.read(path3, cloud_3);
pcl::PCDReader reader4;
string path4 = path + '/' + result[i-1];
reader4.read(path4, cloud_4);
pcl::PCDReader reader5;
string path5 = path + '/' + result[i];
reader5.read(path5, cloud_5);
pcl::PointCloud<pcl::PointXYZI> cloud;
cloud = cloud_1 + cloud_2;
cloud = cloud + cloud_3;
cloud = cloud + cloud_4;
cloud = cloud + cloud_5;
std::cerr<<"The point cloud_1 has: "<<cloud_1.points.size()<<" points data."<<std::endl;
std::cerr<<"The point cloud_2 has: "<<cloud_2.points.size()<<" points data."<<std::endl;
std::cerr<<"The point cloud_3 has: "<<cloud_3.points.size()<<" points data."<<std::endl;
std::cerr<<"The point cloud_4 has: "<<cloud_4.points.size()<<" points data."<<std::endl;
std::cerr<<"The point cloud_5 has: "<<cloud_5.points.size()<<" points data."<<std::endl;
std::cerr<<"The point cloud has: "<<cloud.points.size()<<" points data."<<std::endl;
pcl::PCDWriter writer;
writer.write(result[i],cloud);
//printf("path:%s fileName:%s\n", szDir, result[i].c_str());
}
closedir(pDir);
}
int main(){
char path[100]="/home/yxh/Code/data/train/multi-frame/5";
scanFile(path);
return 0;
}
参考文献:
https://blog.csdn.net/xiexingshishu/article/details/42886885
https://blog.csdn.net/qq_42318305/article/details/81988138