PCL中点云数据的读写

一、PCD文件读写

pcd格式的读写方式如下:

#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
int PCDDataReadAndWrite()
{
	string pointCloudPath = "capture0002.pcd";
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);  //定义一个PointXYZ类型的点云指针
	if (pcl::io::loadPCDFile<pcl::PointXYZ>(pointCloudPath, *cloud) == -1/*加载PCD点云数据*/)
	{
		PCL_ERROR("Couldn't read file capture0002.pcd");
		return -1;
	}

	pcl::io::savePCDFileASCII("capture0002_1.pcd", *cloud); //将点云保存为PCD文件(保存为ASCII)
	pcl::io::savePCDFileBinary("capture0002_2.pcd", *cloud);//将点云保存为PCD文件(保存为二进制数据)
}

二、PLY文件读写

ply文件的读写方式如下:

int PLYDataReadAndWrite()
{
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
	pcl::PLYReader reader;
	if (reader.read("test.ply", *cloud) == -1)
	{
		PCL_ERROR("Could not read ply file!\n");
		return -1;
	}
	/*if (pcl::io::loadPLYFile("test.ply", *cloud) == -1)
	{
		PCL_ERROR("Could not read ply file!\n");
		return -1;
	}*/
	for (int i = 0; i < cloud->points.size(); i++)
	{
		float rgb = cloud->points[i].rgb;
		std::uint32_t u_rgb = *reinterpret_cast<int*>(&rgb);
		std::uint8_t r = (u_rgb >> 16) & 0x0000ff;
		std::uint8_t g = (u_rgb >> 8) & 0x0000ff;
		std::uint8_t b = (u_rgb) & 0x0000ff;
		cout << "X = " << cloud->points[i].x << ", " << "Y = " << cloud->points[i].y << ", " << "Z = " << cloud->points[i].z <<
			", " << "RGB = " << u_rgb << ", " << "R = " << std::to_string(r) << ", " << "G = " << std::to_string(g) << ", " <<
			"B = " << std::to_string(b) << endl;
		//printf("%d", r);
	}
	pcl::io::savePLYFileASCII("test_1.ply", *cloud);  //保存为ASCII格式
	pcl::io::savePLYFileBinary("test_2.ply", *cloud);  //保存为二进制格式
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值