文章目录
更新:2019年8月
为了促进同行业人员(特指 LiDAR 点云处理人员或相近行业)的技术交流,解决平时开发过程中遇到的技术性问题,博主建立一个QQ群,欢迎大家积极加入,共同引领点云行业的快速发展 ~
群名:LiDAR点云部落
群号:190162198
说明
- 以下均为 Being_young 前辈所写,现转载过来,再加上自己的理解,重新写了一遍,方便自己日后使用
- 博客地址:http://blog.csdn.net/u013019296/article/
PCL经典测试代码
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
int user_data;
void viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor(1.0, 0.5, 1.0);
pcl::PointXYZ o;
o.x = 1.0;
o.y = 0;
o.z = 0;
viewer.addSphere(o, 0.25, "sphere", 0);
std::cout << "i only run once" << std::endl;
}
void viewerPsycho(pcl::visualization::PCLVisualizer& viewer)
{
static unsigned count = 0;
std::stringstream ss;
ss << "Once per viewer loop: " << count++;
viewer.removeShape("text", 0);
viewer.addText(ss.str(), 200, 300, "text", 0);
//FIXME: possible race condition here:
user_data++;
}
int main()
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile("my_point_cloud.pcd", *cloud);
pcl::visualization::CloudViewer viewer("Cloud Viewer");
//blocks until the cloud is actually rendered
viewer.showCloud(cloud);
//use the following functions to get access to the underlying more advanced/powerful
//PCLVisualizer
//This will only get called once
viewer.runOnVisualizationThreadOnce(viewerOneOff);
//This will get called once per visualization iteration
viewer.runOnVisualizationThread(viewerPsycho);
while (!viewer.wasStopped())
{
//you can also do cool processing here
//FIXME: Note that this is running in a separate thread from viewerPsycho
//and you should guard against race conditions yourself...
user_data++;
}
return 0;
}
向PCD文件写入点云数据
#include <iostream> //标准C++库中的输入输出的头文件
#include <pcl/io/pcd_io.h> //PCD读写类相关的头文件
#include <pcl/point_types.h> //PCL中支持的点类型的头文件
int main(int argc, char** argv)
{
//实例化的模板类PointCloud 每一个点的类型都设置为pcl::PointXYZ
/*************************************************
点PointXYZ类型对应的数据结构
Structure PointXYZ{
float x;
float y;
float z;
};
**************************************************/
pcl::PointCloud<pcl::PointXYZ> cloud;
// 创建点云 并设置适当的参数(width height is_dense)
cloud.width = 5;
cloud.height = 1;
cloud.is_dense = false; //不是稠密型的
cloud.points.resize(cloud.width * cloud.height); //点云总数大小
//用随机数的值填充PointCloud点云对象
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);
}
//把PointCloud对象数据存储在 test_pcd.pcd文件中
pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
//打印输出存储的点云数据
std::cerr << "Saved " << cloud.points.size() << " data points to test_pcd.pcd." << std::endl;
for (size_t i = 0; i < cloud.points.size(); ++i)
std::cerr << " " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
return (0);
}
从PCD文件读取点云数据
#include <iostream> //标准C++库中的输入输出的头文件
#include <pcl/io/pcd_io.h> //PCD读写类相关的头文件
#include <pcl/point_types.h> //PCL中支持的点类型的头文件
int main(int argc, char** argv)
{
//创建一个PointCloud<pcl::PointXYZ> boost共享指针并进行实例化
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//打开点云文件
if (pcl::io::loadPCDFile<pcl::PointXYZ>("test_pcd.pcd", *cloud) == -1)
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n");
return (-1);
}
//默认就是而二进制块读取转换为模块化的PointCLoud格式里pcl::PointXYZ作为点类型 然后打印出来
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
for (size_t i = 0; i < cloud->points.size(); ++i)
std::cout << " " << cloud->points[i].x
<< " " << cloud->points[i].y
<< " " << cloud->points[i].z << std::endl;
return (0);
}
连接两个点云中的字段或数据形成新点云
#include <iostream>
#include <pcl/io/pcd_io.h> //io模块
#include <pcl/point_types.h> //数据类型
/***********************************************************
这里一共有两种合并方式:
第一种:点与点的合并
第二种:点与向量的合并
***********************************************************/
int main(int argc, char** argv)
{
//if (argc != 2) //提示如果执行可执行文件输入两个参数 -f 或者-p
//{
//std::cerr << "please specify command line arg '-f' or '-p'" << std::endl;
//exit(0);
//}
argv[0] = "-f";
//argv[1] = "-p"; //点云 + 点云
argv[1] = "-f"; //点云 + 向量
//申明三个pcl::PointXYZ点云数据类型,分别为cloud_a, cloud_b, cloud_c
pcl::PointCloud<pcl::PointXYZ> cloud_a, cloud_b, cloud_c;
//存储进行连接时需要的Normal点云,Normal (float n_x, float n_y, float n_z)
pcl::PointCloud<pcl::Normal> n_cloud_b; //点与点合并的结果
//存储连接XYZ与normal后的点云
pcl::PointCloud<pcl::PointNormal> p_n_cloud_c; //点与向量的合并结构
// 创建点云数据
//设置cloud_a的个数为5
cloud_a.width = 5;
cloud_a.height = cloud_b.height = n_cloud_b.height = 1; //设置都为无序点云
cloud_a.points.resize(cloud_a.width * cloud_a.height); //总数
if (strcmp(argv[1], "-p") == 0) //判断是否为连接a+b=c(点云连接)
{
cloud_b.width = 3; //这里代表点与点连接
cloud_b.points.resize(cloud_b.width * cloud_b.height);
}
else{ //这里是点与法向量连接
n_cloud_b.width = 5; //如果是连接XYZ与normal则生成5个法线(字段间连接)
n_cloud_b.points.resize(n_cloud_b.width * n_cloud_b.height);
}
//以下循环生成无序点云填充上面定义的两种类型的点云数据
//先填充数据 cloud_a
for (size_t i = 0; i < cloud_a.points.size(); ++i)
{ //cloud_a产生三个点(每个点都有X Y Z 三个随机填充的值)
cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}
if (strcmp(argv[1], "-p") == 0)
for (size_t i = 0; i < cloud_b.points.size(); ++i)
{ //如果连接a+b=c,则填充cloud_b,用三个点作为xyz的数据
cloud_b.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_b.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
cloud_b.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}
else
for (size_t i = 0; i < n_cloud_b.points.size(); ++i)
{ //如果连接xyz+normal=xyznormal,则填充n_cloud_b,用5个点作为normal数据
n_cloud_b.points[i].normal[0] = 1024 * rand() / (RAND_MAX + 1.0f);
n_cloud_b.points[i].normal[1] = 1024 * rand() / (RAND_MAX + 1.0f);
n_cloud_b.points[i].normal[2] = 1024 * rand() / (RAND_MAX + 1.0f);
}
/***************************************************************
到这里完成了以下工作:
一、定义了连接点云会用到的5个点云对象:
3个输入(cloud_a cloud_b 和n_cloud_b)
2个输出(cloud_c n_cloud_c)
二、然后就是为两个输入点云cloud_a和 cloud_b或者cloud_a 和n_cloud_b填充数据
****************************************************************/
//输出Cloud A
std::cerr << "Cloud A: " << std::endl;
for (size_t i = 0; i < cloud_a.points.size(); ++i)
std::cerr << " " << cloud_a.points[i].x
<< " " << cloud_a.points[i].y
<< " " << cloud_a.points[i].z
<< std::endl;
//输出Cloud B
std::cerr << "Cloud B: " << std::endl;
if (strcmp(argv[1], "-p") == 0)
for (size_t i = 0; i < cloud_b.points.size(); ++i) //输出 Cloud_b
std::cerr << " " << cloud_b.points[i].x
<< " " << cloud_b.points[i].y
<< " " << cloud_b.points[i].z
<< std::endl;
else//输出n_Cloud_b
for (size_t i = 0; i < n_cloud_b.points.size(); ++i)
std::cerr << " " << n_cloud_b.points[i].normal[0]
<< " " << n_cloud_b.points[i].normal[1]
<< " " << n_cloud_b.points[i].normal[2]
<< std::endl;
// Copy the point cloud data
if (strcmp(argv[1], "-p") == 0)
{
cloud_c = cloud_a;
cloud_c += cloud_b;//把cloud_a和cloud_b连接一起创建cloud_c 后输出
std::cerr << "Cloud C: " << std::endl;
for (size_t i = 0; i < cloud_c.points.size(); ++i)
std::cerr << " " << cloud_c.points[i].x << " " << cloud_c.points[i].y << " " << cloud_c.points[i].z << " " << std::endl;
}
else
{ //连接字段 把cloud_a和 n_cloud_b字段连接 一起创建 p_n_cloud_c)
pcl::concatenateFields(cloud_a, n_cloud_b, p_n_cloud_c);
std::cerr << "Cloud C: " << std::endl;
for (size_t i = 0; i < p_n_cloud_c.points.size(); ++i)
std::cerr << " " <<
p_n_cloud_c.points[i].x << " " << p_n_cloud_c.points[i].y << " " << p_n_cloud_c.points[i].z << " " <<
p_n_cloud_c.points[i].normal[0] << " " << p_n_cloud_c.points[i].normal[1] << " " << p_n_cloud_c.points[i].normal[2] << std::endl;
}
return (0);
}
给点云添加高斯噪声:给坐标添加随机数
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/console/time.h>
#include <iostream>
#include <boost/random.hpp>
int main(int argc, char** argv)
{
//创建一个点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = 1000;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size(); ++i)
{
cloud->points[i].x = 1024.0f * rand() / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024.0f * rand() / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024.0f * rand() / (RAND_MAX + 1.0f);
}
//添加高斯噪声,范围(0-0.01)
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudfiltered(new pcl::PointCloud<pcl::PointXYZ>());
cloudfiltered->points.resize(cloud->points.size());
cloudfiltered->header = cloud->header;
cloudfiltered->width = cloud->width;
cloudfiltered->height = cloud->height;
boost::mt19937 rng;
rng.seed(static_cast<unsigned int> (time(0)));
boost::normal_distribution<> nd(0, 0.01);
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng, nd);
for (size_t point_i = 0; point_i < cloud->points.size(); ++point_i)
{
cloudfiltered->points[point_i].x = cloud->points[point_i].x + static_cast<float> (var_nor());
cloudfiltered->points[point_i].y = cloud->points[point_i].y + static_cast<float> (var_nor());
cloudfiltered->points[point_i].z = cloud->points[point_i].z + static_cast<float> (var_nor());
}
return 0;
}
kd-tree 的实现
#include <pcl/point_cloud.h> //点类型定义头文件
#include <pcl/kdtree/kdtree_flann.h> //kdtree类定义头文件
#include <iostream>
#include <vector>
#include <ctime>
int
main (int argc, char** argv)
{
srand (time (NULL)); //用系统时间初始化随机种子
//创建一个PointCloud<pcl::PointXYZ>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
// 随机点云生成
cloud->width = 1000; //此处点云数量
cloud->height = 1; //表示点云为无序点云
cloud->points.resize (cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size (); ++i) //循环填充点云数据
{
cloud->points[i].x = 1024.0f * rand () / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024.0f * rand () / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024.0f * rand () / (RAND_MAX + 1.0f);
}
//创建KdTreeFLANN对象,并把创建的点云设置为输入,创建一个searchPoint变量作为查询点
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
//设置搜索空间
kdtree.setInputCloud (cloud);
//设置查询点并赋随机值
pcl::PointXYZ searchPoint;
searchPoint.x = 1024.0f * rand () / (RAND_MAX + 1.0f);
searchPoint.y = 1024.0f * rand () / (RAND_MAX + 1.0f);
searchPoint.z = 1024.0f * rand () / (RAND_MAX + 1.0f);
// K 临近搜索
//创建一个整数(设置为10)和两个向量来存储搜索到的K近邻,两个向量中,一个存储搜索到查询点近邻的索引,另一个存储对应近邻的距离平方
int K = 10;
std::vector<int> pointIdxNKNSearch(K); //存储查询点近邻索引
std::vector<float> pointNKNSquaredDistance(K); //存储近邻点对应距离平方
//打印相关信息
std::cout << "K nearest neighbor search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with K=" << K << std::endl;
if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 ) //执行K近邻搜索
{
//打印所有近邻坐标
for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
std::cout << " " << cloud->points[ pointIdxNKNSearch[i] ].x
<< " " << cloud->points[ pointIdxNKNSearch[i] ].y
<< " " << cloud->points[ pointIdxNKNSearch[i] ].z
<< " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
}
/**********************************************************************************
下面的代码展示查找到给定的searchPoint的某一半径(随机产生)内所有近邻,重新定义两个向量
pointIdxRadiusSearch pointRadiusSquaredDistance来存储关于近邻的信息
********************************************************************************/
// 半径 R内近邻搜索方法
std::vector<int> pointIdxRadiusSearch; //存储近邻索引
std::vector<float> pointRadiusSquaredDistance; //存储近邻对应距离的平方
float radius = 256.0f * rand () / (RAND_MAX + 1.0f); //随机的生成某一半径
//打印输出
std::cout << "Neighbors within radius search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with radius=" << radius << std::endl;
if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 ) //执行半径R内近邻搜索方法
{
for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
std::cout << " " << cloud->points[ pointIdxRadiusSearch[i] ].x
<< " " << cloud->points[ pointIdxRadiusSearch[i] ].y
<< " " << cloud->points[ pointIdxRadiusSearch[i] ].z
<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
}
return 0;
}
利用八叉树进行点云压缩
#include <pcl/point_cloud.h> // 点云类型
#include <pcl/point_types.h> //点数据类型
#include <pcl/io/openni_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:
SimpleOpenNIViewer () :
viewer (" Point Cloud Compression Example")
{
}
/************************************************************************************************
在OpenNIGrabber采集循环执行的回调函数cloud_cb_中,首先把获取的点云压缩到stringstream缓冲区,下一步就是解压缩,
它对压缩了的二进制数据进行解码,存储在新的点云中解码了点云被发送到点云可视化对象中进行实时可视化
*************************************************************************************************/
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);
}
}
/**************************************************************************************************************
在函数中创建PointCloudCompression类的对象来编码和解码,这些对象把压缩配置文件作为配置压缩算法的参数
所提供的压缩配置文件为OpenNI兼容设备采集到的点云预先确定的通用参数集,本例中使用MED_RES_ONLINE_COMPRESSION_WITH_COLOR
配置参数集,用于快速在线的压缩,压缩配置方法可以在文件/io/include/pcl/compression/compression_profiles.h中找到,
在PointCloudCompression构造函数中使用MANUAL——CONFIGURATION属性就可以手动的配置压缩算法的全部参数
******************************************************************************************************************/
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;
// 初始化压缩和解压缩对象 其中压缩对象需要设定压缩参数选项,解压缩按照数据源自行判断
PointCloudEncoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> (compressionProfile, showStatistics);
PointCloudDecoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> ();
/***********************************************************************************************************
下面的代码为OpenNI兼容设备实例化一个新的采样器,并且启动循环回调接口,每从设备获取一帧数据就回调函数一次,,这里的
回调函数就是实现数据压缩和可视化解压缩结果。
************************************************************************************************************/
//创建从OpenNI获取点云的抓取对象
pcl::Grabber* interface = new pcl::OpenNIGrabber ();
// 建立回调函数
boost::function<void
(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
//建立回调函数和回调信息的绑定
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; //创建一个新的SimpleOpenNIViewer 实例并调用他的run方法
v.run ();
return (0);
}
八叉树的学习
#include <pcl/point_cloud.h> // 点云类型
#include <pcl/point_types.h> //点数据类型
#include <pcl/io/openni_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:
SimpleOpenNIViewer () :
viewer (" Point Cloud Compression Example")
{
}
/************************************************************************************************
在OpenNIGrabber采集循环执行的回调函数cloud_cb_中,首先把获取的点云压缩到stringstream缓冲区,下一步就是解压缩,
它对压缩了的二进制数据进行解码,存储在新的点云中解码了点云被发送到点云可视化对象中进行实时可视化
*************************************************************************************************/
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);
}
}
/**************************************************************************************************************
在函数中创建PointCloudCompression类的对象来编码和解码,这些对象把压缩配置文件作为配置压缩算法的参数
所提供的压缩配置文件为OpenNI兼容设备采集到的点云预先确定的通用参数集,本例中使用MED_RES_ONLINE_COMPRESSION_WITH_COLOR
配置参数集,用于快速在线的压缩,压缩配置方法可以在文件/io/include/pcl/compression/compression_profiles.h中找到,
在PointCloudCompression构造函数中使用MANUAL——CONFIGURATION属性就可以手动的配置压缩算法的全部参数
******************************************************************************************************************/
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;
// 初始化压缩和解压缩对象 其中压缩对象需要设定压缩参数选项,解压缩按照数据源自行判断
PointCloudEncoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> (compressionProfile, showStatistics);
PointCloudDecoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> ();
/***********************************************************************************************************
下面的代码为OpenNI兼容设备实例化一个新的采样器,并且启动循环回调接口,每从设备获取一帧数据就回调函数一次,,这里的
回调函数就是实现数据压缩和可视化解压缩结果。
************************************************************************************************************/
//创建从OpenNI获取点云的抓取对象
pcl::Grabber* interface = new pcl::OpenNIGrabber ();
// 建立回调函数
boost::function<void
(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
//建立回调函数和回调信息的绑定
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; //创建一个新的SimpleOpenNIViewer 实例并调用他的run方法
v.run ();
return (0);
}
本片段未能测试成功,貌似要采集点云
可视化(经典圆球测试)
#include <pcl/visualization/cloud_viewer.h> //类cloud_viewer头文件申明
#include <iostream> //标准输入输出头文件申明
#include <pcl/io/io.h> //I/O相关头文件申明
#include <pcl/io/pcd_io.h> //PCD文件读取
/**********************************************************************************
函数是作为回调函数,在主函数中只注册一次 ,函数实现对可视化对象背景颜色的设置,添加一个圆球几何体
*********************************************************************************/
int user_data;
void
viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor (1.0, 0.5, 1.0); //设置背景颜色
pcl::PointXYZ o; //存储球的圆心位置
o.x = 1.0;
o.y = 0;
o.z = 0;
viewer.addSphere (o, 0.25, "sphere", 0); //添加圆球几何对象
std::cout << "i only run once" << std::endl;
}
/***********************************************************************************
作为回调函数,在主函数中注册后每帧显示都执行一次,函数具体实现在可视化对象中添加一个刷新显示字符串
*************************************************************************************/
void
viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
{
static unsigned count = 0;
std::stringstream ss;
ss << "Once per viewer loop: " << count++;
viewer.removeShape ("text", 0);
viewer.addText (ss.str(), 200, 300, "text", 0);
//FIXME: possible race condition here:
user_data++;
}
/**************************************************************
首先加载点云文件到点云对象,并初始化可视化对象viewer,注册上面的回
调函数,执行循环直到收到关闭viewer的消息退出程序
*************************************************************/
int
main ()
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>); //声明cloud
pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud); //加载点云文件
pcl::visualization::CloudViewer viewer("Cloud Viewer"); //创建viewer对象
//showCloud函数是同步的,在此处等待直到渲染显示为止
viewer.showCloud(cloud);
//该注册函数在可视化的时候只执行一次
viewer.runOnVisualizationThreadOnce (viewerOneOff);
//该注册函数在渲染输出时每次都调用
viewer.runOnVisualizationThread (viewerPsycho);
while (!viewer.wasStopped ())
{
//此处可以添加其他处理
//FIXME: Note that this is running in a separate thread from viewerPsycho
//and you should guard against race conditions yourself...
user_data++;
}
return 0;
}
基于octree的空间划分及搜索操作
#include <pcl/point_cloud.h>
#include <pcl/octree/octree.h>
#include <iostream>
#include <vector>
#include <ctime>
int
main (int argc, char** argv)
{
srand ((unsigned int) time (NULL)); //用系统时间初始化随机种子
// 八叉树的分辨率,即体素的大小
float resolution = 32.0f;
// 初始化空间变化检测对象
pcl::octree::OctreePointCloudChangeDetector<pcl::PointXYZ> octree (resolution);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudA (new pcl::PointCloud<pcl::PointXYZ> ); //创建点云实例cloudA生成的点云数据用于建立八叉树octree对象
// 为cloudA点云填充点数据
cloudA->width = 128; //设置点云cloudA的点数
cloudA->height = 1; //无序点
cloudA->points.resize (cloudA->width * cloudA->height); //总数
for (size_t i = 0; i < cloudA->points.size (); ++i) //循环填充
{
cloudA->points[i].x = 64.0f * rand () / (RAND_MAX + 1.0f);
cloudA->points[i].y = 64.0f * rand () / (RAND_MAX + 1.0f);
cloudA->points[i].z = 64.0f * rand () / (RAND_MAX + 1.0f);
}
// 添加点云到八叉树中,构建八叉树
octree.setInputCloud (cloudA); //设置输入点云
octree.addPointsFromInputCloud (); //从输入点云构建八叉树
/***********************************************************************************
点云cloudA是参考点云用其建立的八叉树对象描述它的空间分布,octreePointCloudChangeDetector
类继承自Octree2BufBae类,Octree2BufBae类允许同时在内存中保存和管理两个octree,另外它应用了内存池
该机制能够重新利用已经分配了的节点对象,因此减少了在生成点云八叉树对象时昂贵的内存分配和释放操作
通过访问 octree.switchBuffers ()重置八叉树 octree对象的缓冲区,但把之前的octree数据仍然保留在内存中
************************************************************************************/
// 交换八叉树的缓冲,但是CloudA对应的八叉树结构仍然在内存中
octree.switchBuffers ();
//cloudB点云用于建立新的八叉树结构,与前一个cloudA对应的八叉树共享octree对象,同时在内存中驻留
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudB (new pcl::PointCloud<pcl::PointXYZ> ); //实例化点云对象cloudB
// 为cloudB创建点云
cloudB->width = 128;
cloudB->height = 1;
cloudB->points.resize (cloudB->width * cloudB->height);
for (size_t i = 0; i < cloudB->points.size (); ++i)
{
cloudB->points[i].x = 64.0f * rand () / (RAND_MAX + 1.0f);
cloudB->points[i].y = 64.0f * rand () / (RAND_MAX + 1.0f);
cloudB->points[i].z = 64.0f * rand () / (RAND_MAX + 1.0f);
}
// 添加cloudB到八叉树中
octree.setInputCloud (cloudB);
octree.addPointsFromInputCloud ();
/**************************************************************************************************************
为了检索获取存在于couodB的点集R,此R并没有cloudA中的元素,可以调用getPointIndicesFromNewVoxels方法,通过探测两个八叉树之间
体素的不同,它返回cloudB 中新加点的索引的向量,通过索引向量可以获取R点集 很明显这样就探测了cloudB相对于cloudA变化的点集,但是只能探测
到在cloudA上增加的点集,二不能探测减少的
****************************************************************************************************************/
std::vector<int> newPointIdxVector; //存储新添加的索引的向量
// 获取前一cloudA对应八叉树在cloudB对应在八叉树中没有的点集
octree.getPointIndicesFromNewVoxels (newPointIdxVector);
// 打印点集
std::cout << "Output from getPointIndicesFromNewVoxels:" << std::endl;
for (size_t i = 0; i < newPointIdxVector.size (); ++i)
std::cout << i << "# Index:" << newPointIdxVector[i]
<< " Point:" << cloudB->points[newPointIdxVector[i]].x << " "
<< cloudB->points[newPointIdxVector[i]].y << " "
<< cloudB->points[newPointIdxVector[i]].z << std::endl;
}
PCL点云类型的转换
#include <pcl/common/impl/io.h>
// ...
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ> ());
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_xyzrgba (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::copyPointCloud(*cloud_xyz, *cloud_xyzrgba);
// ...
也可以自行手动转换
编译PCL遇到的错误
Q1: warning C4003: “max”宏的实参不足 | warning C4003: “min”宏的实参不足
双击进入对应的额头文件,将MAX前边的内容用括号括起来,见下:
修改前:
KNNSimpleResultSet(size_t capacity_) :
capacity_(capacity_)
{
// reserving capacity to prevent memory re-allocations
dist_index_.resize(capacity_, DistIndex(std::numeric_limits<DistanceType>::max(),-1));
clear();
}
修改后:
KNNSimpleResultSet(size_t capacity_) :
capacity_(capacity_)
{
// reserving capacity to prevent memory re-allocations
dist_index_.resize(capacity_, DistIndex((std::numeric_limits<DistanceType>::max)(),-1));
clear();
}
Q2: warning C4819:…(936)
双击报错信息进入该文件,全选中该文件,然后点击文件 - 高级保存选项 - 存为 UNICODE 代码页 1200 即可