博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PCL库学习笔记(PCL Visualizer)
阅读量:4047 次
发布时间:2019-05-25

本文共 9835 字,大约阅读时间需要 32 分钟。

本文展示了PCL Visualizer的几种基本用法。代码的主体是点云库提供的源代码。由于其是由cmake进行编译,有输入参数的选择问题。为了方便VS的调试运行过程,将其中的输入接口进行了修改:

代码中学习到的几个知识点:

1、int main( int argc, char* argv[] ) 中arg和argv参数的含义;参考博客:https://blog.csdn.net/dgreh/article/details/809859282、注意RGB合成的原理uint32_t rgb = (static_cast
(r) << 16 | static_cast
(g) << 8 | static_cast
(b));
/* \author Geoffrey Biggs */#include 
#include
#include
#include
#include
#include
#include
#include
// 帮助voidprintUsage(const char* progName){ std::cout << "\n\nUsage: " << progName << " [options]\n\n" << "Options:\n" << "-------------------------------------------\n" << "-h this help\n" << "-s Simple visualisation example\n" << "-r RGB colour visualisation example\n" << "-c Custom colour visualisation example\n" << "-n Normals visualisation example\n" << "-a Shapes visualisation example\n" << "-v Viewports example\n" << "-i Interaction Customization example\n" << "\n\n";}boost::shared_ptr
simpleVis(pcl::PointCloud
::ConstPtr cloud){ //创建3D窗口并添加点云 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); viewer->addPointCloud
(cloud, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); return (viewer);}boost::shared_ptr
rgbVis(pcl::PointCloud
::ConstPtr cloud){ //创建3D窗口并添加点云 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); pcl::visualization::PointCloudColorHandlerRGBField
rgb(cloud); viewer->addPointCloud
(cloud, rgb, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); return (viewer);}boost::shared_ptr
customColourVis(pcl::PointCloud
::ConstPtr cloud){ //创建3D窗口并添加点云 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); pcl::visualization::PointCloudColorHandlerCustom
single_color(cloud, 0, 255, 0); viewer->addPointCloud
(cloud, single_color, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); return (viewer);}boost::shared_ptr
normalsVis( pcl::PointCloud
::ConstPtr cloud, pcl::PointCloud
::ConstPtr normals){ //创建3D窗口并添加点云其包括法线 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); pcl::visualization::PointCloudColorHandlerRGBField
rgb(cloud); viewer->addPointCloud
(cloud, rgb, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud"); viewer->addPointCloudNormals
(cloud, normals, 10, 0.05, "normals"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); return (viewer);}boost::shared_ptr
shapesVis(pcl::PointCloud
::ConstPtr cloud){ //创建3D窗口并添加点云 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); pcl::visualization::PointCloudColorHandlerRGBField
rgb(cloud); viewer->addPointCloud
(cloud, rgb, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud"); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); //在点云上添加直线和球体模型 viewer->addLine
(cloud->points[0], cloud->points[cloud->size() - 1], "line"); viewer->addSphere(cloud->points[0], 0.2, 0.5, 0.5, 0.0, "sphere"); //在其他位置添加基于模型参数的平面及圆锥体 pcl::ModelCoefficients coeffs; coeffs.values.push_back(0.0); coeffs.values.push_back(0.0); coeffs.values.push_back(1.0); coeffs.values.push_back(0.0); viewer->addPlane(coeffs, "plane"); coeffs.values.clear(); coeffs.values.push_back(0.3); coeffs.values.push_back(0.3); coeffs.values.push_back(0.0); coeffs.values.push_back(0.0); coeffs.values.push_back(1.0); coeffs.values.push_back(0.0); coeffs.values.push_back(5.0); viewer->addCone(coeffs, "cone"); return (viewer);}boost::shared_ptr
viewportsVis( pcl::PointCloud
::ConstPtr cloud, pcl::PointCloud
::ConstPtr normals1, pcl::PointCloud
::ConstPtr normals2){ // 创建3D窗口并添加显示点云其包括法线 boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->initCameraParameters(); int v1(0); viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); viewer->setBackgroundColor(0, 0, 0, v1); viewer->addText("Radius: 0.01", 10, 10, "v1 text", v1); pcl::visualization::PointCloudColorHandlerRGBField
rgb(cloud); viewer->addPointCloud
(cloud, rgb, "sample cloud1", v1); int v2(0); viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2); viewer->setBackgroundColor(0.3, 0.3, 0.3, v2); viewer->addText("Radius: 0.1", 10, 10, "v2 text", v2); pcl::visualization::PointCloudColorHandlerCustom
single_color(cloud, 0, 255, 0); viewer->addPointCloud
(cloud, single_color, "sample cloud2", v2); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud1"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud2"); viewer->addCoordinateSystem(1.0); viewer->addPointCloudNormals
(cloud, normals1, 10, 0.05, "normals1", v1); viewer->addPointCloudNormals
(cloud, normals2, 10, 0.05, "normals2", v2); return (viewer);}unsigned int text_id = 0;void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void* viewer_void){ boost::shared_ptr
viewer = *static_cast
*> (viewer_void); if (event.getKeySym() == "r" && event.keyDown()) { std::cout << "r was pressed => removing all text" << std::endl; char str[512]; for (unsigned int i = 0; i < text_id; ++i) { sprintf(str, "text#%03d", i); viewer->removeShape(str); } text_id = 0; }}void mouseEventOccurred(const pcl::visualization::MouseEvent &event, void* viewer_void){ boost::shared_ptr
viewer = *static_cast
*> (viewer_void); if (event.getButton() == pcl::visualization::MouseEvent::LeftButton && event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease) { std::cout << "Left mouse button released at position (" << event.getX() << ", " << event.getY() << ")" << std::endl; char str[512]; sprintf(str, "text#%03d", text_id++); viewer->addText("clicked here", event.getX(), event.getY(), str); }}boost::shared_ptr
interactionCustomizationVis(){ boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("3D Viewer")); viewer->setBackgroundColor(0, 0, 0); viewer->addCoordinateSystem(1.0); viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)&viewer); viewer->registerMouseCallback(mouseEventOccurred, (void*)&viewer); return (viewer);}// -----Main-----intmain(int argc, char** argv){ // 解析命令行参数 printUsage(argv[0]); std::cout << "Input your commend: "; std::string commend; getline(cin, commend); bool simple(false), rgb(false), custom_c(false), normals(false), shapes(false), viewports(false), interaction_customization(false); if (commend == "-s") { simple = true; std::cout << "Simple visualisation example\n"; } else if (commend == "-c") { custom_c = true; std::cout << "Custom colour visualisation example\n"; } else if (commend == "-r") { rgb = true; std::cout << "RGB colour visualisation example\n"; } else if (commend == "-n") { normals = true; std::cout << "Normals visualisation example\n"; } else if (commend == "-a") { shapes = true; std::cout << "Shapes visualisation example\n"; } else if (commend == "-v") { viewports = true; std::cout << "Viewports example\n"; } else if (commend == "-i") { interaction_customization = true; std::cout << "Interaction Customization example\n"; } // 自行创建一随机点云 pcl::PointCloud
::Ptr basic_cloud_ptr(new pcl::PointCloud
); pcl::PointCloud
::Ptr point_cloud_ptr(new pcl::PointCloud
); std::cout << "Genarating example point clouds.\n\n"; // 以椭圆为边线沿z轴拉伸获取其点云,并赋予红绿蓝渐变色。 uint8_t r(255), g(15), b(15); for (float z(-1.0); z <= 1.0; z += 0.05) { for (float angle(0.0); angle <= 360.0; angle += 5.0) { pcl::PointXYZ basic_point; basic_point.x = 0.5 * cosf(pcl::deg2rad(angle)); basic_point.y = sinf(pcl::deg2rad(angle)); basic_point.z = z; basic_cloud_ptr->points.push_back(basic_point); pcl::PointXYZRGB point; point.x = basic_point.x; point.y = basic_point.y; point.z = basic_point.z; uint32_t rgb = (static_cast
(r) << 16 | static_cast
(g) << 8 | static_cast
(b)); point.rgb = *reinterpret_cast
(&rgb); point_cloud_ptr->points.push_back(point); } if (z < 0.0) { r -= 12; g += 12; } else { g -= 12; b += 12; } } basic_cloud_ptr->width = (int)basic_cloud_ptr->points.size(); basic_cloud_ptr->height = 1; point_cloud_ptr->width = (int)point_cloud_ptr->points.size(); point_cloud_ptr->height = 1; // 0.05为搜索半径获取点云法线 pcl::NormalEstimation
ne; ne.setInputCloud(point_cloud_ptr); pcl::search::KdTree
::Ptr tree(new pcl::search::KdTree
()); ne.setSearchMethod(tree); pcl::PointCloud
::Ptr cloud_normals1(new pcl::PointCloud
); ne.setRadiusSearch(0.05); ne.compute(*cloud_normals1); // 0.1为搜索半径获取点云法线 pcl::PointCloud
::Ptr cloud_normals2(new pcl::PointCloud
); ne.setRadiusSearch(0.1); ne.compute(*cloud_normals2); boost::shared_ptr
viewer; if (simple) { viewer = simpleVis(basic_cloud_ptr); } else if (rgb) { viewer = rgbVis(point_cloud_ptr); } else if (custom_c) { viewer = customColourVis(basic_cloud_ptr); } else if (normals) { viewer = normalsVis(point_cloud_ptr, cloud_normals2); } else if (shapes) { viewer = shapesVis(point_cloud_ptr); } else if (viewports) { viewer = viewportsVis(point_cloud_ptr, cloud_normals1, cloud_normals2); } else if (interaction_customization) { viewer = interactionCustomizationVis(); } // 主循环 while (!viewer->wasStopped()) { viewer->spinOnce(100); boost::this_thread::sleep(boost::posix_time::microseconds(100000)); }}
你可能感兴趣的文章
标记一下
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
一文看清HBase的使用场景
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>