一、函数调用
对点云数据进行批处理时,通常要调用PCL自带的批处理函数: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))
{
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());
}
std::string getFilenameWithoutPath(const std::string& input)
{
std::size_t filename_start = input.find_last_of('/', static_cast<std::size_t>(-1)) + 1;
return input.substr(filename_start, input.size()-filename_start);
}
std::string getFilenameWithoutExtension(const std::string& input)
{
std::size_t dot_position = input.find_last_of('.', input.size());
return input.substr(0, dot_position);
}
std::string getFileExtension(const std::string& input)
{
std::size_t dot_position = input.find_last_of('.', input.size());
return input.substr(dot_position+1, input.size());
}
该函数提供批量读取.pcd文件的功能。
二、实现批量点云合并
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <pcl/common/file_io.h> // 文件批量处理头文件
using namespace std;
int main()
{
//------------------------定义批处理对应的文件夹-------------------------------
string folder_path = "E://test"; // 读取文件的路径
string save_folder_path = "E://test//test"; // 保存文件的路径
vector<string> PCD; // 获取文件名
pcl::getAllPcdFilesInDirectory(folder_path, PCD); // 获取指定路径下的所有PCD文件
cout << "PCD文件个数:" << PCD.size() << endl;
//-----------------批量读取PCD文件-----------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_all(new pcl::PointCloud<pcl::PointXYZ>);
for (size_t i = 0; i < PCD.size(); ++i)
{
//-----------加载点云数据----------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile(folder_path + "//" + PCD[i], *cloud);
*cloud_all += *cloud;
}
cout << "合并的点云数量:" << cloud_all->size() << endl;
//----------------------设置文件保存路径------------------------------
string file_type = "merge.pcd";
string save_path = save_folder_path + "//" + file_type;
cout << "Save file in: " << save_folder_path << endl;
pcl::io::savePCDFileASCII(save_path, *cloud_all);
system("pause");
return 0;
}