点云学习----PCL读写点云

1.读取pcd文件:

int main()
{
	//定义点云对象
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	//加载读取点云数据到cloud中
	if (pcl::io::loadPCDFile("D:\\test_pcd.pcd", *cloud) == -1)
	{
		PCL_ERROR("Read file fail!\n");
		return -1;
	}
	//查看点云数据
	cout << "文件共有" << cloud->size() << "个点:" << endl;
	cout.setf(ios::fixed);

	for (size_t i = 0; i < cloud->size(); i++)
	{
		cout << setprecision(4) << "X:" << cloud->points[i].x << "\tY:" << cloud->points[i].y << "\tZ:" << cloud->points[i].z << endl;
	}
	return 0;
}

结果:

点云学习----PCL读写点云_第1张图片

        写入:

int main()
{
	pcl::PointCloud cloud;
	// 创建点云
	cloud.width = 5;
	cloud.height = 1;
	cloud.is_dense = false;
	cloud.points.resize(cloud.width * cloud.height);
	for (size_t i = 0; i < cloud.points.size(); ++i)
	{
		cloud.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
	}
	pcl::io::savePCDFileASCII("D:/test_write.pcd", cloud);
    //可用savePCDFileBinary()方法将结果保存为二进制类型,用savePCDFileBinaryCompressed()方法将结果保存为二进制并压缩
	cout << "Saved " << cloud.points.size() << " data points to test_write.pcd." << std::endl;
	for (size_t i = 0; i < cloud.points.size(); ++i)
		cout << "X:" << cloud.points[i].x << "\tY:" << cloud.points[i].y << "\tZ:" << cloud.points[i].z << endl;

	return 0;
}

 结果:

点云学习----PCL读写点云_第2张图片

 点云学习----PCL读写点云_第3张图片

 2.读取ply,并保存为pcd

#include 
#include 
int main(int argc, char** argv)
{
	//定义cloud对象
	pcl::PCLPointCloud2 cloud2;

	//ply数据读取
    //也可以直接用pcl::io::loadPLYFile(filename, *cloud)读取
	pcl::PLYReader reader;
	if (reader.read("D:/test.ply", cloud2))
	{
		return -1;
	}
	//写入
	pcl::PCDWriter writer;
	//最后一个参数,TRUE为二进制形式输出,FALSE为ASCII码输出
	writer.write("D:/text.pcd", cloud2, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false);
	return 0;
}

        读取pcd保存ply

    //读pcd
	pcl::PCLPointCloud2 cloud;
	if (pcl::io::loadPCDFile("D:/text.pcd", cloud) < 0)
		return (false);
	//写ply
	pcl::PLYWriter writer;
	writer.write("D:/test.ply", cloud, Eigen::Vector4f::Zero(),     Eigen::Quaternionf::Identity(), false);

 

3.读取las文件(这里需要编译安装libLas库,后续补上):

int mian(int argc, char** argv)
{
	// 打开las文件
	std::ifstream ifs("D:/ground.las", std::ios::in | std::ios::binary); 
	// 读取las数据流
	liblas::ReaderFactory f;
	liblas::Reader reader = f.CreateWithStream(ifs); 
	//获取las数据点的个数
	unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();

	pcl::PointCloud cloud;
	cloud.width = nbPoints;	//保证与las数据点的个数一致	
	cloud.height = 1;
	cloud.is_dense = false;
	cloud.points.resize(cloud.width * cloud.height);

	int i = 0;
	uint16_t r1, g1, b1;
	int r2, g2, b2;
	uint32_t rgb;

	while (reader.ReadNextPoint())
	{
		// 获取las数据的x,y,z信息
		cloud.points[i].x = (reader.GetPoint().GetX());
		cloud.points[i].y = (reader.GetPoint().GetY());
		cloud.points[i].z = (reader.GetPoint().GetZ());

		//获取las数据的r,g,b信息
		r1 = (reader.GetPoint().GetColor().GetRed());
		g1 = (reader.GetPoint().GetColor().GetGreen());
		b1 = (reader.GetPoint().GetColor().GetBlue());
		r2 = ceil(((float)r1 / 65536) * (float)256);
		g2 = ceil(((float)g1 / 65536) * (float)256);
		b2 = ceil(((float)b1 / 65536) * (float)256);
		rgb = ((int)r2) << 16 | ((int)g2) << 8 | ((int)b2);
		cloud.points[i].rgb = *reinterpret_cast(&rgb);

		i++;
	}

	pcl::io::savePCDFileASCII("D:/pointcloud.pcd", cloud);//存储为pcd类型文件
	return (0);
}

参考文献:

PCL 从LAS文件中获取点云的全部属性_点云侠的博客-CSDN博客_读取las文件点云数据属性 

PCL实现读取las文件_S小超的博客-CSDN博客_pcl读取las

PCL读取ply文件_知识海洋里的咸鱼的博客-CSDN博客_pcl读取ply文件

其他类型数据待实践后补充

你可能感兴趣的:(c++,pcl,点云,学习,c++,开发语言)