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实现步骤

  1. 读取点云数据。
  2. 使用 pcl::octree::OctreePointCloudCompression 创建八叉树压缩对象。
  3. 压缩点云,并保存压缩结果。
  4. 解压缩点云以恢复压缩后的数据,并将结果可视化。

1.3应用场景

  1. 存储优化:减少点云存储所需的空间。
  2. 传输优化:通过压缩减少点云在网络传输中的数据量。
  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.

对于使用PCA方法估计点云的法线,可以使用PCL(Point Cloud Library)库来实现。下面是一个基本的步骤: 1. 加载点云数据:使用PCL的`pcl::PointCloud`数据结构加载点云数据。 2. 计算点云的协方差矩阵:使用`pcl::computeCovarianceMatrix`函数计算点云的协方差矩阵。 3. 计算协方差矩阵的特征向量和特征值:使用`pcl::eigen33`函数计算协方差矩阵的特征向量和特征值。 4. 提取法线:选择最小特征值对应的特征向量作为估计的法线方向。 下面是一个简单的示例代码: ```cpp #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/io/pcd_io.h> int main() { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("point_cloud.pcd", *cloud); pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud(cloud); pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>()); ne.setSearchMethod(tree); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); ne.setKSearch(10); // 设置最近邻搜索的数量 ne.compute(*cloud_normals); // 可以通过cloud_normals访问估计得到的法线信息 // cloud_normals->points[i].normal_x, cloud_normals->points[i].normal_y, cloud_normals->points[i].normal_z 分别表示第i个点的法线方向 return 0; } ``` 以上代码中,我们首先加载了一个点云数据文件(`point_cloud.pcd`),然后使用`NormalEstimation`类来进行法线估计。最后,我们可以通过访问`cloud_normals`来获取估计得到的法线信息。 请确保你已经正确安装了PCL库,并将代码中的文件路径和点云数据文件名替换为你自己的文件路径和文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值