当前位置: 首页 > article >正文

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.


http://www.kler.cn/news/318527.html

相关文章:

  • vue中使用exceljs和file-saver插件实现纯前端表格导出Excel(支持样式配置,多级表头)
  • MySQL程序
  • 观后感:《中国数据库前世今生》——时代变迁中的数据库崛起
  • 个性化大语言模型:PPlug——让AI更懂你
  • mfc异步TCP Client通信向主线程发送接收消息
  • wpf中如何访问控件和生成使用事件?
  • FME学习笔记
  • Leetcode 106. 从中序与后序遍历序列构造二叉树
  • 计算机毕业设计之:基于微信小程序的中药材科普系统(源码+文档+讲解)
  • vue3/Element/Tabs 标签页的使用与详解
  • 基于Qt5.12.2开发 MQTT客户端调试助手
  • Go基础学习04-变量重声明;类型转换;类型断言;Unicode代码点;类型别名;潜在类型
  • MobileNetV2: Inverted Residuals and Linear Bottlenecks
  • vue2和vue3页面加自定义水印(组件化)
  • 【计算机网络 - 基础问题】每日 3 题(二十)
  • SpringBoot的应用
  • 现代桌面UI框架科普及WPF入门1
  • Mac电脑上最简单安装Python的方式
  • Java:文件操作
  • [spring]用MyBatis XML操作数据库 其他查询操作 数据库连接池 mysql企业开发规范
  • WPF入门教学十四 命令与ICommand接口
  • OpenAI GPT o1技术报告阅读(5)-安全性对齐以及思维链等的综合评估与思考
  • Servlet入门:服务端小程序的初试(自己学习整理的资料)
  • R包:gplots经典热图
  • CentOS中使用Docker运行mysql并挂载本地目录
  • 滚雪球学SpringCloud[9.3讲]:微服务监控与运维详解
  • redis 快速入门
  • Serilog文档翻译系列(五) - 编写日志事件
  • [利用python进行数据分析01] “来⾃Bitly的USA.gov数据” 分析出各个地区的 windows和非windows用户
  • vue2 实现简易版的模糊查询功能