c++和点云库PCL读取.xyz或者txt内的点云数据

当点位数据保存到.txt或者其他格式的时候,可以使用以下方式读取到PCL的数据格式上

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include<opencv2/opencv.hpp>
#include <fstream>


using namespace cv;
using namespace std;


//分割字符串的方法
void split(std::string& string_input, std::vector<std::string>&string_output, std::string& delema1)
{
	std::string::size_type start = string_input.find_first_not_of(delema1, 0);//找到第一个不为delema1的下标
	std::string::size_type pose = string_input.find_first_of(delema1, start);//找到第一个delema1的下标
	while (std::string::npos != start || std::string::npos != pose) {//当即没有delema1也没有字符的时候结束
		string_output.push_back(string_input.substr(start, pose - start));
		start = string_input.find_first_not_of(delema1, pose);//更新start 从pose开始
		pose = string_input.find_first_of(delema1, start);//更新pose,从start开始再次寻找
	}
}


//加载点云
bool get_parameter_xyz(std::string path, pcl::PointCloud<pcl::PointXYZ> &cloud)
{
	cloud.clear();
	std::ifstream inf;
	bool flag = false;
	try
	{
		inf.open(path);
	}
	catch (const std::exception&)
	{
		
		return false;
	}
	std::string sline;//每一行
	std::vector<std::string>string_output;
	std::string Delema = " ";
	pcl::PointXYZ point3d;
	while (getline(inf, sline))
	{
		int i = 0;
		split(sline, string_output, Delema);
		point3d.x = stold(string_output[i]);
		point3d.y = stold(string_output[i+1]);
		point3d.z = stold(string_output[i + 2]);
		string_output.clear();
		cloud.push_back(point3d);
	}
	inf.close();
}



int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	string path = "pointcloud.xyz";
	get_parameter_xyz(path, *cloud);
	std::cout << "Loaded "
		<< cloud->width * cloud->height
		<< " data points from test_pcd.pcd with the following fields: "
		<< std::endl;

	system("pause");
	return 0;
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值