当前位置:   article > 正文

PCL点云库(1) — 简介与数据类型_pcl库

pcl库

目录

1.1 简介

1.2 PCL安装

1.2.1 安装方法

1.2.2 测试程序 

1.3 PCL数据类型

1.4 PCL中自定义point类型

1.4.1 增加自定义point的步骤

1.4.2 完整代码


1.1 简介

来源:PCL(点云库)_百度百科

        PCL(Point Cloud Library)是在吸收了前人点云相关研究基础上建立起来的大型跨平台开源C++编程库,它实现了大量点云相关的通用算法和高效数据结构,涉及到点云获取、滤波、分割、配准、检索、特征提取、识别、追踪、曲面重建、可视化等。支持多种操作系统平台,可在Windows、Linux、Android、Mac OS X、部分嵌入式实时系统上运行。如果说OpenCV是2D信息获取与处理的结晶,那么PCL就在3D信息获取与处理上具有同等地位。

        如《PCL架构图》所示,对于3D点云处理来说,PCL完全是一个的模块化的现代C++模板库。其基于以下第三方库:Boost、Eigen、FLANN、VTK、CUDA、OpenNI、Qhull,实现点云相关的获取、滤波、分割、配准、检索、特征提取、识别、追踪、曲面重建、可视化等。

1.2 PCL安装

1.2.1 安装方法

安装链接:ubuntu20.04下安装pcl_ubuntu安装pcl_Yuannau_jk的博客-CSDN博客

1.2.2 测试程序 

  1. cmake_minimum_required(VERSION 2.6)
  2. project(pcl_test)
  3. find_package(PCL 1.10 REQUIRED)
  4. include_directories(${PCL_INCLUDE_DIRS})
  5. link_directories(${PCL_LIBRARY_DIRS})
  6. add_definitions(${PCL_DEFINITIONS})
  7. add_executable(pcl_test pcl_test.cpp)
  8. target_link_libraries (pcl_test ${PCL_LIBRARIES})
  9. install(TARGETS pcl_test RUNTIME DESTINATION bin)
  1. #include <iostream>
  2. #include <pcl/common/common_headers.h>
  3. #include <pcl/io/pcd_io.h>
  4. #include <pcl/visualization/pcl_visualizer.h>
  5. #include <pcl/visualization/cloud_viewer.h>
  6. #include <pcl/console/parse.h>
  7. int main(int argc, char **argv) {
  8. std::cout << "Test PCL !!!" << std::endl;
  9. pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
  10. uint8_t r(255), g(15), b(15);
  11. for (float z(-1.0); z <= 1.0; z += 0.05)
  12. {
  13. for (float angle(0.0); angle <= 360.0; angle += 5.0)
  14. {
  15. pcl::PointXYZRGB point;
  16. point.x = 0.5 * cosf (pcl::deg2rad(angle));
  17. point.y = sinf (pcl::deg2rad(angle));
  18. point.z = z;
  19. uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
  20. static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
  21. point.rgb = *reinterpret_cast<float*>(&rgb);
  22. point_cloud_ptr->points.push_back (point);
  23. }
  24. if (z < 0.0)
  25. {
  26. r -= 12;
  27. g += 12;
  28. }
  29. else
  30. {
  31. g -= 12;
  32. b += 12;
  33. }
  34. }
  35. point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
  36. point_cloud_ptr->height = 1;
  37. pcl::visualization::CloudViewer viewer ("test");
  38. viewer.showCloud(point_cloud_ptr);
  39. while (!viewer.wasStopped()){ };
  40. return 0;
  41. }

1.3 PCL数据类型

在头文件#include <pcl/point_types.h>中包含了所有内置的点云类型,部分如下:

  1. namespace pcl
  2. {
  3. /** \brief Members: float x, y, z
  4. * \ingroup common
  5. */
  6. struct PointXYZ;
  7. /** \brief Members: rgba
  8. * \ingroup common
  9. */
  10. struct RGB;
  11. /** \brief Members: intensity (float)
  12. * \ingroup common
  13. */
  14. struct Intensity;
  15. /** \brief Members: intensity (std::uint8_t)
  16. * \ingroup common
  17. */
  18. struct Intensity8u;
  19. /** \brief Members: intensity (std::uint32_t)
  20. * \ingroup common
  21. */
  22. struct Intensity32u;
  23. /** \brief Members: float x, y, z, intensity
  24. * \ingroup common
  25. */
  26. struct PointXYZI;
  27. /** \brief Members: float x, y, z, uin32_t label
  28. * \ingroup common
  29. */
  30. struct PointXYZL;
  31. /** \brief Members: std::uint32_t label
  32. * \ingroup common
  33. */
  34. ...
  35. }

(1)PointXYZ-成员变量:float x, y, z

PointXYZ是使用最常见的一个点数据类型,因为它只包含三维xyz坐标信息,这三个浮点数附加一个浮点数来满足存储对齐,用户可以利用points[i].data[0],或者points[i].x访问点的x坐标值。

(2)PointXYZI-成员变量:float x,y,z,intensity

Point是一个简单的XYZ坐标加intensity的point类型,xyz未与intensity位于同一个结构体。具体说xyz位于一个结构体,内存对齐,intensity位于另一个结构体,内存对齐(填充3个浮点数)。

(3)PointXYZRGBA-成员变量:float x,y,z,uint32_t rgba

rgba包含在一个整型变量中,其余与PointXYZI结构类似。

(4)PointXYZRGB-成员变量:float x,y,z,rgb

rgb信息包含在一个浮点型变量中,其余与PointXYZI结构类似。

(5)InterestPoint-float x,y,z,strength

strength用来表示关键点的强度测量值,其余与PointXYZI结构类似。

(6)Normal-float normal[3], curvature

Norma结构体表示给定点所在样本曲面上的法线方向,以及对应曲率的测量值。

(7)PointNormal-float x,y,z,normal[3], curvature

PointNormal是存储XYZ数据的point结构体,并且包括采样点对应法线和曲率。

(8)PointXYZRGBNormal-float x,y,z,rgb,normal[3], curvature

PointXYZNormal是存储XYZ数据和RGB颜色的point结构体,并且包括采样点曲面法线和曲率。

(9)PointXYZINormal-float x,y,z,intensity,normal[3], curvature

PointXYZNormal是存储XYZ数据和强度值的point结构体,并且包括采样点曲面法线和曲率。

(10)PointWithRange-float x,y,z(union with float point[4], range)

PointWithRange除了range包含从所获得的视点到采样点的距离测量值之外,其余与PointXYZI结构类似。

(11)PointWithViewpoint-float x,y,z,vp_x,vp_y,vp_z

PointWithViewpoint除了vp_x,vp_y,vp_z以三维点表示所获得的视点之外之外,其余与PointXYZI结构类似。

(12)MomentInvariantst-float j1,j2,j3

MomentInvariantst视野更包含采样曲面上面片的三个不变矩的point类型,描述面片上的顶带你分布情况。

(13)PrincipalRadiiRSD-float r_min,r_max

PrincipalRadiiRSD是一个包含曲面块上两个RSD半径的point类型。

(14)Boundary-uint_8 boundary_point

Boundary存储一个点是否位于曲面边界上的简单point类型。

(15)PrincipalCurvatures-float principal_curvature[3],pc1,pc2

PrincipalCurvatures包含给定点主曲率的简单point类型。

(16)PFHSignature125-float pfh[125]

PFHSignature125包含给定点的PFH(点特征直方图)的简单point类型。

(17)FPFHSignature33-float pfh[33]

FPFHSignature33包含给定点的FPFH(快速点特征直方图)的简单point类型。

(18)VFHSignature308-float vfh[308]

VFHSignature308包含给定点的VFH(视点特征直方图)的简单point类型。

(19)Narf36-float x,y,z,rool,pitch,yaw;fooat descriptor[36]

Narf36包含给定点NARF(归一化对齐半径特征)的简单point类型。

(20)BorderDescription-int x,y; BorderTraits traits

BorderDescription包含给定点边界类型的简单point类型。

(21)IntensityGradient-float gradient[3]

IntensityGradient包含给定点强度的梯度point类型。

(22)Histogram-float histogram[N]

Histogram用来存储一般用途的n维直方图。

(23)PointWithScale-float x,y,z,scale

PointWithScale除了scale表示某点用于几何操作的尺度外,其余与PointXYZI结构类似。

(24)PointSufel-float x,y,z,normal[3],rgba,radius,confidence,curvature

PointSufel存储XYZ坐标、曲面法线、RGB信息、半径、可信度和曲面曲率的复杂point类型。

1.4 PCL中自定义point类型

1.4.1 增加自定义point的步骤

(1)首先先进行结构定义

  1. // 定义点类型结构
  2. struct EIGEN_ALIGN16 MyPoint
  3. {
  4. PCL_ADD_POINT4D // 点类型有4个元素 XYZ+padding
  5. PCL_ADD_RGB //加颜色
  6. double time_stamp; //时间戳
  7. EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 确保new操作符对齐操作
  8. };

 (2)注册到PCL库中

  1. // 注册到PCL库
  2. POINT_CLOUD_REGISTER_POINT_STRUCT(MyPoint, //注册的结构类型
  3. (float, x, x) //坐标
  4. (float, y, y)
  5. (float, z, z)
  6. (uint32_t,rgba,rgba) //颜色
  7. (double, time_stamp, time_stamp) //时间戳
  8. );

1.4.2 完整代码

(1)自定义点云类型程序

  1. cmake_minimum_required(VERSION 2.6)
  2. project(mypoint)
  3.  
  4. find_package(PCL 1.10 REQUIRED)
  5. include_directories(${PCL_INCLUDE_DIRS})
  6. link_directories(${PCL_LIBRARY_DIRS})
  7. add_definitions(${PCL_DEFINITIONS})
  8.  
  9. add_executable(pcl_test pcl_test.cpp)
  10. target_link_libraries (pcl_test ${PCL_LIBRARIES})
  11. install(TARGETS pcl_test RUNTIME DESTINATION bin)
  1. // 方式一:直接写在mypoint.cpp中
  2. #define PCL_NO_PRECOMPILE
  3. #include <iostream>
  4. #include <chrono>
  5. #include <pcl/point_cloud.h>
  6. #include <pcl/io/pcd_io.h>
  7. #include <pcl/visualization/cloud_viewer.h>
  8. #include <pcl/PCLPointCloud2.h>
  9. #include <pcl/visualization/pcl_visualizer.h>
  10. using namespace std;
  11. // 定义点类型结构
  12. struct EIGEN_ALIGN16 MyPoint
  13. {
  14. PCL_ADD_POINT4D // 点类型有4个元素 XYZ+padding
  15. PCL_ADD_RGB //加颜色
  16. double time_stamp; //时间戳
  17. EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 确保new操作符对齐操作
  18. };
  19. // 注册到PCL库
  20. POINT_CLOUD_REGISTER_POINT_STRUCT(MyPoint, //注册的结构类型
  21. (float, x, x) //坐标
  22. (float, y, y)
  23. (float, z, z)
  24. (uint32_t,rgba,rgba) //颜色
  25. (double, time_stamp, time_stamp) //时间戳
  26. );
  27. double getTimeStamp()
  28. {
  29. auto timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch());
  30. return timeNow.count()/1000.0;
  31. }
  32. int main(int argc, char *argv[])
  33. {
  34. pcl::PointCloud<MyPoint>::Ptr cloud;
  35. cloud.reset(new pcl::PointCloud<MyPoint>);
  36. cloud->width = 100;
  37. cloud->height = 1;
  38. cloud->is_dense = false;
  39. cloud->points.resize(100);
  40. for(auto i = 0; i < 100; i++)
  41. {
  42. // xyz
  43. cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
  44. cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
  45. cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
  46. // rgb
  47. cloud->points[i].r = 1024 * rand() / (256);
  48. cloud->points[i].g = 1024 * rand() / (256);
  49. cloud->points[i].b = 1024 * rand() / (256);
  50. cloud->points[i].time_stamp = getTimeStamp();
  51. }
  52. pcl::io::savePCDFile("mypoint.pcd",*cloud);
  53. // to show
  54. #if 0
  55. pcl::visualization::CloudViewer viewer("Cloud Viewer");
  56. viewer.showCloud<MyPoint>(cloud);
  57. #else
  58. pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(renderer, renderWindow, "viewer", false));
  59. viewer->addPointCloud<MyPoint>(cloud,color);
  60. viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 4);
  61. while (!viewer.wasStopped ())
  62. {
  63. viewer->spinOnce(100);
  64. }
  65. #endif
  66. return 0;
  67. }

 (2)头文件封装

  1. // 方式二:独立的头文件
  2. #ifndef MYPOINT_H
  3. #define MYPOINT_H
  4. #include<pcl/point_types.h>
  5. namespace MYPOINT {
  6. struct EIGEN_ALIGN16 _MYPOINT
  7. {
  8. PCL_ADD_POINT4D
  9. PCL_ADD_RGB
  10. double time_stamp;
  11. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  12. };
  13. struct EIGEN_ALIGN16 MYPOINT : public _MYPOINT
  14. {
  15. inline MYPOINT (const _MYPOINT &p)
  16. {
  17. x = p.x; y = p.y; z = p.z; data[3] = 1.0f;
  18. rgba = p.rgba;
  19. a = 0;
  20. time_stamp = p.time_stamp;
  21. }
  22. inline MYPOINT ()
  23. {
  24. x = y = z = 0.0f;
  25. rgba = 0;
  26. data[3] = 1.0f;
  27. time_stamp = 0;
  28. }
  29. inline MYPOINT (float _x, float _y, float _z, uint32_t _rgb,double _time_stamp)
  30. {
  31. x = _x; y = _y; z = _z;
  32. rgba = _rgb;
  33. data[3] = 1.0f;
  34. time_stamp = _time_stamp;
  35. }
  36. friend std::ostream& operator << (std::ostream& os, const MYPOINT& p)
  37. {
  38. os << "(" << p.x << "," << p.y << "," << p.z << "," << p.rgba << ","<< p.time_stamp << ")";
  39. return os;
  40. }
  41. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  42. };
  43. }
  44. POINT_CLOUD_REGISTER_POINT_STRUCT(MYPOINT::MYPOINT,
  45. (float, x, x)
  46. (float, y, y)
  47. (float, z, z)
  48. (uint32_t,rgb,rgb)
  49. (double, time_stamp, time_stamp)
  50. );
  51. #endif
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/267949
推荐阅读
相关标签
  

闽ICP备14008679号