PCL点云库学习笔记(可视化)
可视化
一、CloudViewer
1.1 用几行代码可视化应用程序中的某些内容
#include <pcl/visualization/cloud_viewer.h>
//
void foo ()
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
//..........填充点云
//创建一个CloudViewer类的可视化的对象,进行简单的显示
pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
viewer.showCloud (cloud);
while (!viewer.wasStopped ())
{
}
}
1.2 完整的可视化例子
例子中PCLVisualizer``是``CloudViewer
的后端,但它在自己的线程中运行。要访问它,必须使用回调函数.
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
int user_data;
//下面的函数是回调函数,只执行一次,参数是PCLVisualizer可视化对象
//设置可视化背景的颜色,和添加一个圆球几何体
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 ()
{
//创建点云,然后读取PCD文件
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");
//停在此处,直到渲染出点云
viewer.showCloud(cloud);
//use the following functions to get access to the underlying more advanced/powerful
//PCLVisualizer
//在可视化的时候,创建球的函数,只调用一次
viewer.runOnVisualizationThreadOnce (viewerOneOff);
//在渲染输出时每一次都调用
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;
}
1.3编译报错
1.编译警告
error C4996: 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators
解决:属性-> c/c++ -> 预处理器 -> 预处理器定义 里添加_SCL_SECURE_NO_WARNINGS
2.error LNK2019: 无法解析的外部符号,lib文件未引入,添加
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib, "gdi32.lib")
3.运行vtk文件库的时候出现错误,分别添加
#include "vtkAutoInit.h" //vtk库
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
二、可视化深度图像
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/range_image/range_image.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include "vtkAutoInit.h" //vtk库
typedef pcl::PointXYZ PointType;
//全局参数-
float angular_resolution_x = 0.5f,
angular_resolution_y = angular_resolution_x;
pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
bool live_update = false;
// 打印帮助
void printUsage(const char* progName)
{
std::cout << "\n\nUsage: " << progName << " [options] <scene.pcd>\n\n"
<< "Options:\n"
<< "-------------------------------------------\n"
<< "-rx <float> angular resolution in degrees (default " << angular_resolution_x << ")\n"
<< "-ry <float> angular resolution in degrees (default " << angular_resolution_y << ")\n"
<< "-c <int> coordinate frame (default " << (int)coordinate_frame << ")\n"
<< "-l live update - update the range image according to the selected view in the 3D viewer.\n"
<< "-h this help\n"
<< "\n\n";
}
//设置视点
void setViewerPose(pcl::visualization::PCLVisua