这篇博客提供一个PCL读取点云并可视化的最简Demo,并提供扩充学习资料的链接。其中点云读取比较简单,而可视化比较复杂。
PCL的可视化类有好几号,最强大的是PCLvisualizer,这个类是这篇博客的主要内容。如果只是简单显示点云,可以不需要PCLvisualizer类,调用CloudViewer就可以了。CloudViewer的调用过程更加简单直接,PCLvisualizer更加强大,功能更加丰富。
PCLvisualizer还可以设置鼠标键盘操作回调函数,功能之多,并且开源的PCL也在不断丰富和完善,所以在调用PCLvisualizer时,大部分编程是需要参考文档手册的。
点云可视化参考资料
读取点云并可视化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include
#include
#include
#include
#include
#include
#include
usingpcl::visualization::PointCloudColorHandlerGenericField;
usingpcl::visualization::PointCloudColorHandlerCustom;
intmain(intargc,char*argv[]){
pcl::PointCloud<:pointxyz>::Ptrcloud(newpcl::PointCloud<:pointxyz>);
if(pcl::io::loadPCDFile<:pointxyz>("../pcd/a1.pcd",*cloud1)==-1){
std::cerr<
return-1;
}
boost::shared_ptr<:visualization::pclvisualizer>viewer(newpcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0,0,0);
viewer->addPointCloud<:pointxyz>(cloud,"sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,1,"sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return0;
}
DEMO代码简单解释
一般在代码最后,都会出现这样一个while循环,在spinOnce中为界面刷新保留了时间
1
2
3
4
5
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
createViewPort函数还可以创建多个视口,比如把视窗分为左右两个部分
1
2
p->createViewPort(0.0,0,0.5,1.0,vp_1);//创建左视区
p->createViewPort(0.5,0,1.0,1.0,vp_2);//创建右视区
每次要刷新点云,都要先删除原先的,再添加新的。
下面这个函数是用来设置显示的点的大小。
1
viewer->addCoordinateSystem(1.0);
更多功能参见官方API手册。
关于CloudViewer
使用非常简单,但是除了显示什么都不能干:
1
2
3
4
pcl::visualization::CloudViewerviewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while(!viewer.wasStopped())
{}