PCL批量合并点云

一、函数调用

对点云数据进行批处理时,通常要调用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;
}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值