> 文章列表 > PCL点云库(1) — 简介与数据类型

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

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

目录

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 测试程序 

cmake_minimum_required(VERSION 2.6)
project(pcl_test)find_package(PCL 1.10 REQUIRED)include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})add_executable(pcl_test pcl_test.cpp)target_link_libraries (pcl_test ${PCL_LIBRARIES})install(TARGETS pcl_test RUNTIME DESTINATION bin)
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>int main(int argc, char **argv) {std::cout << "Test PCL !!!" << std::endl;pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);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::PointXYZRGB point;point.x = 0.5 * cosf (pcl::deg2rad(angle));point.y = sinf (pcl::deg2rad(angle));point.z = z;uint32_t rgb = (static_cast<uint32_t>(r) << 16 |static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));point.rgb = *reinterpret_cast<float*>(&rgb);point_cloud_ptr->points.push_back (point);}if (z < 0.0){r -= 12;g += 12;}else{g -= 12;b += 12;}}point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();point_cloud_ptr->height = 1;pcl::visualization::CloudViewer viewer ("test");viewer.showCloud(point_cloud_ptr);while (!viewer.wasStopped()){ };return 0;
}

1.3 PCL数据类型

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

namespace pcl
{/** \\brief Members: float x, y, z* \\ingroup common*/struct PointXYZ;/** \\brief Members: rgba* \\ingroup common*/struct RGB;/** \\brief Members: intensity (float)* \\ingroup common*/struct Intensity;/** \\brief Members: intensity (std::uint8_t)* \\ingroup common*/struct Intensity8u;/** \\brief Members: intensity (std::uint32_t)* \\ingroup common*/struct Intensity32u;/** \\brief Members: float x, y, z, intensity* \\ingroup common*/struct PointXYZI;/** \\brief Members: float x, y, z, uin32_t label* \\ingroup common*/struct PointXYZL;/** \\brief Members: std::uint32_t label* \\ingroup common*/...
}

(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)首先先进行结构定义

// 定义点类型结构
struct EIGEN_ALIGN16 MyPoint
{PCL_ADD_POINT4D    // 点类型有4个元素 XYZ+paddingPCL_ADD_RGB        //加颜色double time_stamp;    //时间戳EIGEN_MAKE_ALIGNED_OPERATOR_NEW   // 确保new操作符对齐操作
};

 (2)注册到PCL库中

// 注册到PCL库
POINT_CLOUD_REGISTER_POINT_STRUCT(MyPoint,         //注册的结构类型(float, x, x)    //坐标(float, y, y)(float, z, z)(uint32_t,rgba,rgba)    //颜色(double, time_stamp, time_stamp)    //时间戳
);

1.4.2 完整代码

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

cmake_minimum_required(VERSION 2.6)
project(mypoint)find_package(PCL 1.10 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})add_executable(pcl_test pcl_test.cpp)
target_link_libraries (pcl_test ${PCL_LIBRARIES})
install(TARGETS pcl_test RUNTIME DESTINATION bin)
// 方式一:直接写在mypoint.cpp中
#define PCL_NO_PRECOMPILE
#include <iostream>
#include <chrono>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;// 定义点类型结构
struct EIGEN_ALIGN16 MyPoint
{PCL_ADD_POINT4D    // 点类型有4个元素 XYZ+paddingPCL_ADD_RGB        //加颜色double time_stamp;    //时间戳EIGEN_MAKE_ALIGNED_OPERATOR_NEW   // 确保new操作符对齐操作
};// 注册到PCL库
POINT_CLOUD_REGISTER_POINT_STRUCT(MyPoint,         //注册的结构类型(float, x, x)    //坐标(float, y, y)(float, z, z)(uint32_t,rgba,rgba)    //颜色(double, time_stamp, time_stamp)    //时间戳
);double getTimeStamp()
{auto timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch());return timeNow.count()/1000.0;
}int main(int argc, char *argv[])
{pcl::PointCloud<MyPoint>::Ptr cloud;cloud.reset(new pcl::PointCloud<MyPoint>);cloud->width = 100;cloud->height = 1;cloud->is_dense = false;cloud->points.resize(100);for(auto i = 0; i < 100; i++){// xyzcloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);// rgbcloud->points[i].r = 1024 * rand() / (256);cloud->points[i].g = 1024 * rand() / (256);cloud->points[i].b = 1024 * rand() / (256);cloud->points[i].time_stamp = getTimeStamp();}pcl::io::savePCDFile("mypoint.pcd",*cloud);// to show
#if 0pcl::visualization::CloudViewer viewer("Cloud Viewer");viewer.showCloud<MyPoint>(cloud);
#elsepcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(renderer, renderWindow, "viewer", false));viewer->addPointCloud<MyPoint>(cloud,color);viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 4);while (!viewer.wasStopped ()){viewer->spinOnce(100);}
#endifreturn 0;
}

 (2)头文件封装

// 方式二:独立的头文件
#ifndef MYPOINT_H
#define MYPOINT_H
#include<pcl/point_types.h>namespace MYPOINT {struct EIGEN_ALIGN16 _MYPOINT
{PCL_ADD_POINT4DPCL_ADD_RGBdouble time_stamp;EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};struct EIGEN_ALIGN16 MYPOINT : public _MYPOINT
{inline MYPOINT (const _MYPOINT &p){x = p.x; y = p.y; z = p.z; data[3] = 1.0f;rgba = p.rgba;a = 0;time_stamp = p.time_stamp;}inline MYPOINT (){x = y = z = 0.0f;rgba = 0;data[3] = 1.0f;time_stamp = 0;}inline MYPOINT (float _x, float _y, float _z, uint32_t _rgb,double _time_stamp){x = _x; y = _y; z = _z;rgba = _rgb;data[3] = 1.0f;time_stamp = _time_stamp;}friend std::ostream& operator << (std::ostream& os, const MYPOINT& p){os << "(" << p.x << "," << p.y << "," << p.z << "," << p.rgba << ","<< p.time_stamp << ")";return os;}EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
} POINT_CLOUD_REGISTER_POINT_STRUCT(MYPOINT::MYPOINT,(float, x, x)(float, y, y)(float, z, z)(uint32_t,rgb,rgb)(double, time_stamp, time_stamp)
);
#endif