PCL中点云数据的读写

一、PCD文件读写

pcd格式的读写方式如下:

#include
#include
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);  //保存为二进制格式
}

你可能感兴趣的:(PCL学习,计算机视觉)