#include <iostream>
#include <Eigen/Core>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/file_io.h> // 批量处理头文件
using namespace std;
int main()
{
string pcd_path = (argc > 1) ? argv[1] : "../test_data/result_1/";
vector<string> vpcldir;
pcl::getAllPcdFilesInDirectory(pcd_path,vpcldir); // 获取指定路径下的所有PCD文件
for (int i = 0; i < vpcldir.size(); i++) {
auto cloud = std::make_shared<pcl::PointCloud<PointI>>();
//pcl::io::loadPCDFile(pcd_path + vpcldir[i], *cloud);
if (pcl::io::loadPCDFile(pcd_path + vpcldir[i], *cloud) < 0)
{
PCL_ERROR("Couldn't read file \n");
system("pause");
return -1;
}
// cout << pcd_path + vpcldir[i] << " " << cloud->size() << endl;
}
return 0;
}
这里用到了pcl的函数getAllPcdFilesInDirectory,查看源码
void getAllPcdFilesInDirectory(const std::string& directory, std::vector<std::string>& file_names)
{
boost::filesystem::path p(directory);
if(boost::filesystem::is_directory(p))
{
for(const auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(p), {}))
{
if (boost::filesystem::is_regular_file(entry))
{
//修改这里的后缀名字 .pcd 改为想要获取文件的后缀名字,即可实现获取任何格式的文件
if (entry.path().extension() == ".pcd")
file_names.emplace_back(entry.path().filename().string());
}
}
}
else
{
std::cerr << "Given path is not a directory\n";
return;
}
std::sort(file_names.begin(), file_names.end());
}