PCL:读写点云

个人记录,不喜勿喷。
分享给有需要的人,代码质量勿喷。

一、头文件

#include <iostream>          //标准C++库中的输入输出类相关头文件。
#include <pcl/io/pcd_io.h>   //pcd 读写类相关的头文件。
#include <pcl/point_types.h> //PCL中支持的点类型头文件。

二、读点云

参考:从PCD文件中读取点云数据-PCL中国|点云库PCL|Point Cloud Library|点云|点云产学研资料-PCL中国 (pclcn.org)

/* 读点云 */
void ReadPCD(const std::string &xjFilePath)
{
	//std::string xjFilePath = QString.toLocal8Bit().toStdString();

	pcl::PointCloud<pcl::PointXYZRGBL>::Ptr xjPC(new pcl::PointCloud<pcl::PointXYZRGBL>);
	if (pcl::io::loadPCDFile<pcl::PointXYZRGBL>(xjFilePath, *xjPC) == -1)
	{
		PCL_ERROR("Couldn't read file \n");
		return;
	}

	for (int i = 0; i < xjPC->points.size(); i++)
	{
		//XYZ
		float x = xjPC->points[i].x;
		float y = xjPC->points[i].y;
		float z = xjPC->points[i].z;

		//RGB:0-255
		int red = xjPC->points[i].r;
		int green = xjPC->points[i].g;
		int blue = xjPC->points[i].b;

		//L:读取出来是0
		int label = xjPC->points[i].label;
	}

	xjPC->clear();
	xjPC = nullptr;
}

三、写点云

参考:向PCD文件写入点云数据-PCL中国|点云库PCL|Point Cloud Library|点云|点云产学研资料-PCL中国 (pclcn.org) 

/* 写点云 */
void WritePCD(const std::string &xjFilePath, std::vector<std::vector<float>> &dataPC)
{
	//std::string xjFilePath = QString.toLocal8Bit().toStdString();

	pcl::PointCloud<pcl::PointXYZRGB>::Ptr resultPC(new pcl::PointCloud<pcl::PointXYZRGB>());
	
	int pointCount = dataPC.size();
	resultPC->resize(pointCount);
	resultPC->width = 1;
	resultPC->height = pointCount;
	resultPC->is_dense = false;

	for (int i = 0; i < pointCount; i++)
	{
		std::vector<float> point = dataPC[i];

		/* XYZ */
		resultPC->points[i].x = point[0];
		resultPC->points[i].y = point[1];
		resultPC->points[i].z = point[2];

		/* RGB:0-255 */
		resultPC->points[i].r = (int)point[3];
		resultPC->points[i].g = (int)point[4];
		resultPC->points[i].b = (int)point[5];
	}
	
	pcl::io::savePCDFileBinary(xjFilePath, *resultPC);
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

累了就要打游戏

把我养胖,搞代码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值