PCL 用八叉树方法压缩点云
目录
一、概述
1.1原理
1.2实现步骤
1.3应用场景
二、代码实现
2.1关键函数
2.1.1点云压缩
2.1.2点云解压缩
2.2完整代码
三、实现效果
3.1原始点云
3.2数据显示
PCL点云算法汇总及实战案例汇总的目录地址链接:
PCL点云算法与项目实战案例汇总(长期更新)
一、概述
八叉树(Octree)是一种高效的三维空间数据结构,用于组织和索引点云数据。除了用于邻域搜索、体素化和可视化等操作,八叉树还可以用于点云的压缩。通过压缩,点云数据的大小可以显著减少,同时保留大致的几何形状,从而加速点云处理。
1.1原理
八叉树压缩点云的原理是通过递归地将三维空间划分为八个子空间,直到每个体素(空间区域)中只有一个或几个点。可以根据指定的分辨率对点云进行体素化,当多个点落在同一个体素时,只保留一个点作为代表点,这样就减少了点云中的点数,达到压缩的目的。
1.2实现步骤
- 读取点云数据。
- 使用 pcl::octree::OctreePointCloudCompression 创建八叉树压缩对象。
- 压缩点云,并保存压缩结果。
- 解压缩点云以恢复压缩后的数据,并将结果可视化。
1.3应用场景
- 存储优化:减少点云存储所需的空间。
- 传输优化:通过压缩减少点云在网络传输中的数据量。
- 点云降采样:通过压缩降低点云数据的密度。
二、代码实现
2.1关键函数
2.1.1点云压缩
通过 pcl::octree::OctreePointCloudCompression 对点云进行压缩,可以显著减少点云的大小。
#include <pcl/compression/octree_pointcloud_compression.h>
// 创建压缩对象
pcl::io::OctreePointCloudCompression<pcl::PointXYZ> pointCloudEncoder(true); // 压缩设置为true
// 将点云压缩到字符串流中
std::stringstream compressedData; // 存储压缩后的数据
pointCloudEncoder.encodePointCloud(cloud, compressedData); // 压缩点云数据
2.1.2点云解压缩
通过 pcl::io::OctreePointCloudCompression 解压缩先前压缩的点云数据,将数据还原为点云格式。
// 创建解压缩对象
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_decoded(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::OctreePointCloudCompression<pcl::PointXYZ> pointCloudDecoder(false); // 解压缩设置为false
// 从压缩数据流中解压缩点云
pointCloudDecoder.decodePointCloud(compressedData, cloud_decoded); // 解压缩
2.2完整代码
#include <pcl/compression/octree_pointcloud_compression.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <sstream>
// 封装的可视化函数,显示两个点云,一个原始点云,一个解压缩后的点云
void visualizePointClouds(
pcl::PointCloud<pcl::PointXYZ>::Ptr original_cloud,
pcl::PointCloud<pcl::PointXYZ>::Ptr decompressed_cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Dual PointCloud Viewer"));
// 设置视口1,显示原始点云
int vp_1;
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1);
viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_1);
viewer->addText("Original PointCloud", 10, 10, "vp1_text", vp_1);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> original_color_handler(original_cloud, 0, 255, 0);
viewer->addPointCloud(original_cloud, original_color_handler, "original_cloud_vp1", vp_1);
// 设置视口2,显示解压缩后的点云
int vp_2;
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2);
viewer->setBackgroundColor(1.0, 1.0, 1.0, vp_2);
viewer->addText("Decompressed PointCloud", 10, 10, "vp2_text", vp_2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> decompressed_color_handler(decompressed_cloud, 0, 0, 255);
viewer->addPointCloud(decompressed_cloud, decompressed_color_handler, "decompressed_cloud_vp2", vp_2);
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
}
}
int main(int argc, char** argv)
{
// 读取点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("person2.pcd", *cloud) == -1)
{
PCL_ERROR("Couldn't read file!");
return -1;
}
std::cout << "Original cloud size: " << cloud->points.size() << " points." << std::endl;
// 创建压缩器和解压缩器对象,使用默认配置
pcl::io::compression_Profiles_e compressionProfile = pcl::io::LOW_RES_ONLINE_COMPRESSION_WITHOUT_COLOR;
// 压缩对象
pcl::io::OctreePointCloudCompression<pcl::PointXYZ> pointCloudEncoder(compressionProfile, true);
std::stringstream compressedData;
// 压缩点云
pointCloudEncoder.encodePointCloud(cloud, compressedData);
std::cout << "Point cloud compressed successfully." << std::endl;
// 解压缩对象
pcl::io::OctreePointCloudCompression<pcl::PointXYZ> pointCloudDecoder(compressionProfile, false);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_decoded(new pcl::PointCloud<pcl::PointXYZ>);
// 解压缩点云
pointCloudDecoder.decodePointCloud(compressedData, cloud_decoded);
std::cout << "Point cloud decompressed successfully." << std::endl;
std::cout << "Decompressed cloud size: " << cloud_decoded->points.size() << " points." << std::endl;
// 可视化压缩前后的点云
visualizePointClouds(cloud, cloud_decoded);
return 0;
}
三、实现效果
3.1原始点云
3.2数据显示
Original cloud size: 10000 points.
*** POINTCLOUD ENCODING ***
Frame ID: 1
Encoding Frame: Intra frame
Number of encoded points: 8890
XYZ compression percentage: 7.122234%
XYZ bytes per point: 0.854668 bytes
Color compression percentage: 0.000000%
Color bytes per point: 0.000000 bytes
Size of uncompressed point cloud: 138.906250 kBytes
Size of compressed point cloud: 7.419922 kBytes
Total bytes per point: 0.854668 bytes
Total compression percentage: 5.341676%
Compression ratio: 18.720716
Point cloud compressed successfully.
Point cloud decompressed successfully.
Decompressed cloud size: 8890 points.