#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/openni2_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/compression/octree_pointcloud_compression.h>
#include <stdio.h>
#include <sstream>
#include <stdlib.h>
#ifdef WIN32
# define sleep(x) Sleep((x)*1000)
#endif
class SimpleOpenNIViewer
{
public:
//构造函数,C++独特语法糖,直接赋值到viewer
SimpleOpenNIViewer() :
viewer(" Point Cloud Compression Example")
{
}
//传入常数指针(常数指针的好处请见本博客内《C++与Java区别》)
void
cloud_cb_(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
{
if (!viewer.wasStopped())
{
// 字符串流保存压缩的数据
std::stringstream compressedData;
// 初始化输出点云数据
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudOut(new pcl::PointCloud<pcl::PointXYZRGBA>());
//编码压缩点云
PointCloudEncoder->encodePointCloud(cloud, compressedData);
// 反编译输出点云数据
PointCloudDecoder->decodePointCloud(compressedData, cloudOut);
// 可视化
viewer.showCloud(cloudOut);
}
}
void
run()
{
bool showStatistics = true;
// 完整配置对象在: /io/include/pcl/compression/compression_profiles.h
//枚举类型,里面包括压缩配置(分辨率,在/离线,颜色信息)
pcl::io::compression_Profiles_e compressionProfile = pcl::io::MED_RES_ONLINE_COMPRESSION_WITH_COLOR;
// 实例化编码与解码对象,并赋值配置,showStatistics为显示压缩统计信息
PointCloudEncoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>(compressionProfile, showStatistics);
PointCloudDecoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>();
// OpenNi设备捕获接口
pcl::Grabber* interface = new pcl::io::OpenNI2Grabber();
// 定义回调函数,用于设备回调
std::function<void(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
[this](const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& cloud) { cloud_cb_(cloud); };
//连接所需信号的回调函数。在这种情况下,它是一个带有颜色值的点云
boost::signals2::connection c = interface->registerCallback(f);
// 开始接收点云
interface->start();
while (!viewer.wasStopped())
{
sleep(1);
}
interface->stop();
// 释放内存
delete (PointCloudEncoder);
delete (PointCloudDecoder);
}
//全局对象
pcl::visualization::CloudViewer viewer;
pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>* PointCloudEncoder;
pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>* PointCloudDecoder;
};
int
main(int argc, char **argv)
{
SimpleOpenNIViewer v;
v.run();
return (0);
}
目前调试还有问题,不知道为什么这里的代码一直报错,而且之前已经按照自己的pcl版本将头文件中的"openni_grabber.h"修改成了"openni2_grabber.h",但是目前这里一直报错