• 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer


    一、什么是PCLVisualizer

      PCLVisualizer是PCL的全功能可视化类。虽然使用起来比CloudViewer更复杂,但它也更强大,提供了显示法线、绘制形状和多个视图等功能。

    二、使用案例

      本节我们会使用历程展示PCLVisualizer的使用方法和一些特性,包括可视化单片点云,添加颜色,自定义颜色,显示法线和其他信息、多视窗显示等功能。
      请参考如下代码,注释非常清晰。

     /* 示例代码作者: Geoffrey Biggs */
    
     #include <iostream>
     #include <thread>
     
     #include <pcl/common/angles.h> // for pcl::deg2rad
     #include <pcl/features/normal_3d.h>
     #include <pcl/io/pcd_io.h>
     #include <pcl/visualization/pcl_visualizer.h>
     #include <pcl/console/parse.h>
     
     using namespace std::chrono_literals;
     
     // ---------------------
     // -----显示帮助信息-----
     // ---------------------
     void
     printUsage (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";
     }
     
     
     pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
     {
       // -------------------------------------------------
       // -----打开三维可视化工具PCLVisualizer并添加普通点云-----
       // -------------------------------------------------
       pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
       viewer->setBackgroundColor (0, 0, 0);// 设置背景颜色
       viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");// 添加点云到窗口sample cloud
       viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud"); // 设置点云单点的尺寸
       viewer->addCoordinateSystem (1.0);// 添加坐标系
       viewer->initCameraParameters (); // 初始化相机参数
       return (viewer);
     }
     
     
     pcl::visualization::PCLVisualizer::Ptr rgbVis (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
     {
       // -------------------------------------------------
       // -----打开三维可视化工具PCLVisualizer并添加RGB颜色点云-----
       // -------------------------------------------------
       pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
       viewer->setBackgroundColor (0, 0, 0);
       pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud); // 创建RGB点云句柄,用于添加RGB点云
       viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
       viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
       viewer->addCoordinateSystem (1.0);
       viewer->initCameraParameters ();
       return (viewer);
     }
     
     
     pcl::visualization::PCLVisualizer::Ptr customColourVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
     {
       // -----------------------------------------------
       // ----打开三维可视化工具PCLVisualizer并添加指定颜色点云----
       // -----------------------------------------------
       pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
       viewer->setBackgroundColor (0, 0, 0);
       pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0); // 设置指定颜色的点云(绿色)
       viewer->addPointCloud<pcl::PointXYZ> (cloud, single_color, "sample cloud");
       viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
       viewer->addCoordinateSystem (1.0);
       viewer->initCameraParameters ();
       return (viewer);
     }
     
    
     pcl::visualization::PCLVisualizer::Ptr normalsVis (
         pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals)
     {
       // --------------------------------------------------------
       // -----打开三维可视化工具PCLVisualizer并添加点云和法向-----
       // --------------------------------------------------------
       pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
       viewer->setBackgroundColor (0, 0, 0);
       pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
       viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
       viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
       viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals, 10, 0.05, "normals");// 显示法向,10表示只显示1/10的点的法向,0.05表示法向显示长度。
       viewer->addCoordinateSystem (1.0);
       viewer->initCameraParameters ();
       return (viewer);
     }
     
     
    pcl::visualization::PCLVisualizer::Ptr shapesVis (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
    {
      // --------------------------------------------
      // -----打开三维可视化工具PCLVisualizer并添加点云-----
      // --------------------------------------------
      pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
    106  viewer->setBackgroundColor (0, 0, 0);
    107  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
    108  viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
    109  viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
      viewer->addCoordinateSystem (1.0);
      viewer->initCameraParameters ();
    
      //------------------------------------
      //-----Add 添加指定形状的点云-----
      //------------------------------------
      viewer->addLine<pcl::PointXYZRGB> ((*cloud)[0],
                                         (*cloud)[cloud->size() - 1], "line"); // 添加线状点云(点云的第一个点和最后一个点的连线)
      viewer->addSphere ((*cloud)[0], 0.2, 0.5, 0.5, 0.0, "sphere");
      // 添加球状点云,中心为第一个点位置,半径为0.5,后三个参数是颜色
    
      //---------------------------------------
      //-----在其他位置添加形状-----
      //---------------------------------------
      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"); // 4个系数ABCD可以确定一个平面
      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"); // 7个系数ABCDEFG可以确定一个圆锥体
    
      return (viewer);
    }
    
    
    pcl::visualization::PCLVisualizer::Ptr viewportsVis (
        pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals1, pcl::PointCloud<pcl::Normal>::ConstPtr normals2)
    {
      // --------------------------------------------------------
      // -----在同一窗口添加不同的点云和法向并列显示-----
      // --------------------------------------------------------
      pcl::visualization::PCLVisualizer::Ptr viewer (new 	   pcl::visualization::PCLVisualizer ("3D Viewer"));
     viewer->initCameraParameters ();
    
      int v1(0);
      viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); // 添加视角V1,四个参数分别是X轴和Y轴的显示最大最小范围
      viewer->setBackgroundColor (0, 0, 0, v1);
      viewer->addText("Radius: 0.01", 10, 10, "v1 text", v1);
     pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
      viewer->addPointCloud<pcl::PointXYZRGB> (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<pcl::PointXYZRGB> single_color(cloud, 0, 255, 0);
      viewer->addPointCloud<pcl::PointXYZRGB> (cloud, single_color, "sample cloud2", v2);
    
      viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud1");
    167  viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud2");
      viewer->addCoordinateSystem (1.0);
    
      viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals1, 10, 0.05, "normals1", v1);
      viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals2, 10, 0.05, "normals2", v2);
    
      return (viewer);
    }
    
    // 以下是键盘输入响应
    unsigned int text_id = 0;
    void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event,
                                void* viewer_void)
    {
      pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (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)
    {
      pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (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);
      }
    }
    
    pcl::visualization::PCLVisualizer::Ptr interactionCustomizationVis ()
    {
      pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
      viewer->setBackgroundColor (0, 0, 0);
      viewer->addCoordinateSystem (1.0);
    // 注册键盘和和鼠标响应回调函数
      viewer->registerKeyboardCallback (keyboardEventOccurred, (void*)viewer.get ());
      viewer->registerMouseCallback (mouseEventOccurred, (void*)viewer.get ());
    
      return (viewer);
    }
    
    
    // --------------
    // -----Main-----
    // --------------
    int
    main (int argc, char** argv)
    {
      // --------------------------------------
      // -----命令行解析-----
      // --------------------------------------
      if (pcl::console::find_argument (argc, argv, "-h") >= 0)
      {
        printUsage (argv[0]);
        return 0;
      }
      bool simple(false), rgb(false), custom_c(false), normals(false),
        shapes(false), viewports(false), interaction_customization(false);
      if (pcl::console::find_argument (argc, argv, "-s") >= 0)
      {
        simple = true;
        std::cout << "Simple visualisation example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-c") >= 0)
      {
        custom_c = true;
       std::cout << "Custom colour visualisation example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-r") >= 0)
      {
        rgb = true;
        std::cout << "RGB colour visualisation example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-n") >= 0)
      {
        normals = true;
        std::cout << "Normals visualisation example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-a") >= 0)
      {
        shapes = true;
        std::cout << "Shapes visualisation example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-v") >= 0)
      {
        viewports = true;
        std::cout << "Viewports example\n";
      }
      else if (pcl::console::find_argument (argc, argv, "-i") >= 0)
      {
        interaction_customization = true;
        std::cout << "Interaction Customization example\n";
      }
      else
      {
        printUsage (argv[0]);
        return 0;
      }
    
      // ------------------------------------
      // -----创建示例点云-----
      // ------------------------------------
      pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
      pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
      std::cout << "Generating example point clouds.\n\n";
     // 我们要做一个沿z轴挤压的椭圆。XYZRGB云的颜色将逐渐从红色到绿色再到蓝色。
      std::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 * std::cos (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;
          point.r = r;
          point.g = g;
          point.b = b;
          point_cloud_ptr->points.push_back (point);
        }
        if (z < 0.0)
        {
          r -= 12;
          g += 12;
        }
        else
        {
          g -= 12;
          b += 12;
        }
      }
      basic_cloud_ptr->width = basic_cloud_ptr->size ();
      basic_cloud_ptr->height = 1;
      point_cloud_ptr->width = point_cloud_ptr->size ();
      point_cloud_ptr->height = 1;
    
      // ----------------------------------------------------------------
      // -----计算表面法向,搜索半径0.05-----
      // ----------------------------------------------------------------
      pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
      ne.setInputCloud (point_cloud_ptr);
      pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
      ne.setSearchMethod (tree);
      pcl::PointCloud<pcl::Normal>::Ptr cloud_normals1 (new pcl::PointCloud<pcl::Normal>);
      ne.setRadiusSearch (0.05);
      ne.compute (*cloud_normals1);
    
      // ---------------------------------------------------------------
      // -----计算表面法向,搜索半径0.1-----
      // ---------------------------------------------------------------
      pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2 (new pcl::PointCloud<pcl::Normal>);
      ne.setRadiusSearch (0.1);
      ne.compute (*cloud_normals2);
    
    // 开始显示点云
      pcl::visualization::PCLVisualizer::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();
      }
    
      //--------------------
      // -----Main loop-----
      //--------------------
      while (!viewer->wasStopped ())
     {
        viewer->spinOnce (100);
        std::this_thread::sleep_for(100ms);
      }
    }
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383

    以上操作的视图显示如下:
    1.普通点云
    在这里插入图片描述
    2.RGB点云
    在这里插入图片描述
    3.单色点云
    在这里插入图片描述
    4.RGB点云带法向
    在这里插入图片描述
    5.RGB点云带法向,并创建线、圆锥和球点云
    在这里插入图片描述
    6. 一个视图内两片点云
    在这里插入图片描述
    7.鼠标响应和键盘响应
    在这里插入图片描述


    【博主简介】
      斯坦福的兔子,男,天津大学工学硕士。毕业至今从事光学三维成像及点云处理相关工作。因工作中使用的三维处理库为公司内部库,不具有普遍适用性,遂自学开源PCL库及其相关数学知识以备使用。谨此将自学过程与君共享。
    博主才疏学浅,尚不具有指导能力,如有问题还请各位在评论处留言供大家共同讨论。

  • 相关阅读:
    Java Spring Bean的生命周期 三级缓存
    深度学习——day43 ID-YOLO——基于驾驶员固定区域的实时突出目标检测
    Nginx Rewrite
    基于PHP+MySQL音乐网站的设计与实现
    java计算机毕业设计商场后台管理系统源码+系统+数据库+lw文档+mybatis+运行部署
    是什么原因影响到服务器的稳定性
    线性查找(思路分析和代码实现) [数据结构][Java]
    MySQL CASE表达式简单使用
    数据结构笔记 4 树和二叉树
    使用IntelliJ Idea开发Spark Streaming流应用程序
  • 原文地址:https://blog.csdn.net/weixin_41966507/article/details/125427778