初始配置
CMake配置
1 2 3 4 5 6 cmake_minimum_required (VERSION 3.10 FATAL_ERROR)project (pcl)find_package (PCL 1.12 REQUIRED)find_package (Eigen3 REQUIRED)add_executable (pcl main.cpp)target_link_libraries (pcl ${PCL_LIBRARIES} Eigen3::Eigen)
软连接设置
1 2 cd /usr/include/sudo ln -s pcl-1.12/pcl pcl
基本数据类型
pcl中的基本数据类型为点云,点云是C++中的一个类,包含以下部分:
width (int)
对于unorganized点云来说,它代表着点云中点的总数
对于organized点云来说,它代表着点云的宽度
有序点云数据集(organized point cloud dataset)
是指在结构上类似于一幅有序图像(或矩阵 matrix )的点云数据,其中数据按照行(rows)和列(columns)****进行排列。
这类点云的典型来源包括 双目相机(stereo cameras)和 飞行时间相机(Time Of Flight, ToF cameras) 。
有序数据集的优势在于:由于可以直接知道相邻点之间的关系 (例如像素 pixels 之间的邻接关系),最近邻搜索(nearest neighbor operations)可以被高效地执行,从而 加快计算速度 ,并降低 PCL 中某些算法的计算成本 。
height (int)
对于unorganized点云来说,它被设为1(可以用来检测点云数据集是否为organized)
对于organized点云来说,它代表高度
points (std::vector<PointT>)
存储点数据,点的类型为PointT 。对于存储XYZ数据的点云来说,points包含一个pcl::PointXYZ的vector:
1 2 pcl::PointCloud<pcl::PointXYZ> cloud; std::vector<pcl::PointXYZ> data = cloud.points;
is_dense (bool)
用于指定 points 中的数据是否全部为有限值(finite)。
若为 true,表示所有点的数据都是有限的;
若为 false,则表示某些点的 XYZ 坐标 可能包含 无穷大(Inf) 或 非数值(NaN)。
sensor_origin_ (Eigen::Vector4f)
用于指定传感器采集时的位姿中的位置部分(origin / translation,即平移)。
该成员变量通常是可选的(optional),在 PCL 的大多数算法中并不会被直接使用。
sensor_orientation_ (Eigen::Quaternionf)
用于指定传感器采集时的位姿中的方向部分(orientation,即旋转)。
同样地,该成员变量通常也是可选的,在 PCL 的大多数算法中并不会被用到
初步尝试
写了一个简单的加载pcl文件->载入点云->显示点云的程序:
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 #include <pcl/impl/point_types.hpp> #include <pcl/point_cloud.h> #include <pcl/point_types.h> #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/io/ply_io.h> #include <pcl/point_cloud.h> #include <pcl/console/parse.h> #include <pcl/common/transforms.h> #include <pcl/visualization/pcl_visualizer.h> using namespace std;int main () { string filename = "test.pcd" ; pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBNormal>) ; if (pcl::io::loadPCDFile <pcl::PointXYZRGBNormal> (filename, *cloud) == -1 ) { PCL_ERROR ("Couldn't read file test.pcd \n" ); return (-1 ); } cout << "Loaded " << cloud->width * cloud->height << " data points from test.pcd" << endl; pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer" )) ; viewer->addPointCloud <pcl::PointXYZRGBNormal>(cloud, "sample cloud" ); viewer->setBackgroundColor (0 , 0 , 0 ); viewer->spin (); return 0 ; }
最终效果:
滤波器Filter
直通滤波(PassThrough)
原理详解
直通滤波不同于其他滤波算法,它进行的操作是:
范围裁剪(range cropping)
空间条件筛选(spatial condition)
类似与二维图像的裁剪,直通滤波:“只保留在某个坐标范围内的点,其余全部丢掉”
比如说,当遇到如下情况时,常用直通滤波:
只要 z ∈ [0.5, 2.0] 的点
去掉地面
去掉太远 / 太近的点
代码示例
1 2 3 4 5 6 7 8 #include <pcl/filters/passthrough.h> pcl::PassThrough<pcl::PointXYZ> pass; pass.setInputCloud (cloud); pass.setFilterFieldName ("z" ); pass.setFilterLimits (0.5 , 2.0 ); pass.filter (*filtered_cloud);
其中:
1 pass.setFilterFieldName ("z" );
指定 过滤维度
可选:“x” / “y” / “z”
1 pass.setFilterLimits (0.5 , 2.0 );
设置 下限 lower bound
设置 上限 upper bound
使用场景
非常实际的三个场景:
点云预处理第一步
地面 / 天花板快速剔除
给后续算法减负
聚类(Euclidean Clustering)
法向量估计(Normal Estimation)
RANSAC
一般来说,它相当于OpenCV中对图像的预处理操作:先 PassThrough,再搞复杂算法。
体素滤波(VoxelGrid)
如果说 直通滤波(PassThrough)是“砍范围”,那 体素滤波(Voxel Grid Filter)就是“压密度”。
原理详解
把空间切成一个个小立方体(voxel),每个立方体里只保留一个代表点(通常是质心) 。
特点:downsampling(下采样):减少数据点数量,同时尽量保持原始结构和信息,最终实现:reduce(减少数量)、preserve(保持结构)。
比如当原始点云:每平方米 10 万个点(太密)、算法算不动、内存爆炸
通过体素滤波,我们可以做的是:
把空间划成:5cm × 5cm × 5cm的小方块
每个方块里:可能有 1 个点、也可能有 100 个点
每个方块中只留 1 个代表点
这样使得点云中点的数目大幅下降,但整体的形状未发生改变。
数学本质
体素定义
设体素尺寸为:l x , l y , l z l_x, l_y, l_z
l x , l y , l z ,那么其中点(x,y,z)的体素索引为:
( ⌊ x l x ⌋ , ⌊ y l y ⌋ , ⌊ z l z ⌋ ) \left(
\left\lfloor \frac{x}{l_x} \right\rfloor,\;
\left\lfloor \frac{y}{l_y} \right\rfloor,\;
\left\lfloor \frac{z}{l_z} \right\rfloor
\right)
( ⌊ l x x ⌋ , ⌊ l y y ⌋ , ⌊ l z z ⌋ )
获得代表点
设一个体素中有n个点:
P i = ( x n , y i , z i ) \mathbf{P_i}=(x_n,y_i,z_i)
P i = ( x n , y i , z i )
则求的其质心点为:
p ˉ = 1 N ∑ i = 1 N p i \bar{\mathbf{p}} = \frac{1}{N} \sum_{i=1}^{N} \mathbf{p}_i
p ˉ = N 1 ∑ i = 1 N p i
代码示例
1 2 3 4 5 6 #include <pcl/filters/voxel_grid.h> pcl::VoxelGrid<pcl::PointXYZ> voxel; voxel.setInputCloud (cloud); voxel.setLeafSize (0.05f , 0.05f , 0.05f ); voxel.filter (*downsampled_cloud);
其中:
1 voxel.setLeafSize (0.05f , 0.05f , 0.05f );
指的是体素大小(leaf size),单位为米,三个方向可以不同(为非正方体)
SOR 滤波
Statistical Outlier Removal = 用统计方法去掉离群点
原理详解
对 每一个点
看它和 附近 K 个邻居的平均距离
如果这个距离 明显大于整体平均水平**->**删掉它
数学本质
计算平均邻域距离
对于一个点p i p_i p i ,找到它的k k k 个邻居,并计算平均邻域距离;
d i = 1 k ∑ j = 1 k ∥ p i − p i j ∥ d_i = \frac{1}{k} \sum_{j=1}^{k} \| p_i - p_{ij} \|
d i = k 1 ∑ j = 1 k ∥ p i − p ij ∥
统计点的分布
对所有 d i d_i d i :
计算均值 μ \mu μ
计算标准差 σ \sigma σ
判定是否离群
如果某个点满足:
d i > μ + α σ d_i > \mu + \alpha \sigma
d i > μ + α σ
就认为它是 outlier(离群点)
其中α \alpha α 就是 StddevMulThresh
代码示例
1 2 3 4 5 6 7 #include <pcl/filters/statistical_outlier_removal.h> pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor; sor.setInputCloud (cloud); sor.setMeanK (50 ); sor.setStddevMulThresh (1.0 ); sor.filter (*filtered_cloud);
sor.setMeanK(50)
每个点看多少个邻居
setStddevMulThresh(double α)
1 sor.setStddevMulThresh (1.0 );
常见取值:
0.5 :非常激进
1.0 :工程常用
2.0 :偏保守
投影滤波(Parametric Model)
原理详解
Projecting points using a parametric model =
把点云从“自由空间”约束到一个低维几何流形(manifold)上,是一个降维 + 约束的问题,而不是普通滤波。
编程示例
定义平面
a x + b y + c z + d = 0 ax + by + cz + d = 0
a x + b y + cz + d = 0
其中:
a=0,b=0,c=1,d=0;
1 2 3 4 5 pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ()) ; coefficients->values.resize (4 ); coefficients->values[0 ] = coefficients->values[1 ] = 0 ; coefficients->values[2 ] = 1.0 ; coefficients->values[3 ] = 0 ;
创建 ProjectInliers 对象
接下来,我们创建一个 ProjectInliers 对象,并使用上面定义好的 ModelCoefficients 作为投影目标模型(the model to project onto)。
1 2 3 4 5 pcl::ProjectInliers<pcl::PointXYZ> proj; proj.setModelType (pcl::SACMODEL_PLANE); proj.setInputCloud (cloud); proj.setModelCoefficients (coefficients); proj.filter (*cloud_projected);
这一步 将输入点云中的点,**正交投影(orthogonally project)**到所定义的 X–Y 平面上。
配准 Registration
点云配准是什么
求一个刚体变换,让 source 点云尽可能对齐 target 点云。
个人理解:配准就是原点云中的点分别去对应目标点云中点的过程。
方法
对应点规则
Point-to-Point ICP
最近欧氏距离
Point-to-Plane ICP
投影到平面
GICP
高斯分布匹配
NDT
概率最大化
点云配准流程
预处理
初始对齐
精配准
ICP算法
原理
分别在带匹配的目标点云P和源点云Q中,按照一定的约束条件,找到最邻近点(pi,qi),然后计算出最优匹配参数R和t,使得误差函数最小。误差函数为E(R,t)为:
基本步骤
(1)在目标点云P中取点集pi∈P;
(2)找出源点云Q中的对应点集qi∈Q,使得||qi-pi||=min;
(3)计算旋转矩阵R和平移矩阵t,使得误差函数最小;
(4)对pi使用上一步求得的旋转矩阵R和平移矩阵t进行旋转和平移变换,的到新的对应点集pi’={pi’=Rpi+t,pi∈P};
(5)计算pi’与对应点集qi的平均距离;
(6)如果d小于某一给定的阈值或者大于预设的最大迭代次数,则停止迭代计算。
否则返回第2步,直到满足收敛条件为止。
算法参数
(1)优化的目标函数:点到点的距离,点到线的距离,点到面的距离
(2)点云采样方法:
(3)迭代停止条件:mse小于某个值,两次mse差值小于某个值,迭代次数
(4)对应点关系查找:KD树数据结构
代码示例
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 #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/registration/icp.h> #include <pcl/visualization/pcl_visualizer.h> int main () { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in ( new pcl::PointCloud<pcl::PointXYZ>(5 , 1 )) ; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_out ( new pcl::PointCloud<pcl::PointXYZ>) ; for (auto &point : *cloud_in) { point.x = 1024 * rand () / (RAND_MAX + 1.0f ); point.y = 1024 * rand () / (RAND_MAX + 1.0f ); point.z = 1024 * rand () / (RAND_MAX + 1.0f ); } std::cout << "Saved " << cloud_in->size () << " data points to input:" << std::endl; for (auto &point : *cloud_in) std::cout << point << std::endl; *cloud_out = *cloud_in; std::cout << "size:" << cloud_out->size () << std::endl; for (auto &point : *cloud_out) point.x += 0.7f ; std::cout << "Transformed " << cloud_in->size () << " data points:" << std::endl; for (auto &point : *cloud_out) std::cout << point << std::endl; pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; icp.setInputSource (cloud_in); icp.setInputTarget (cloud_out); pcl::PointCloud<pcl::PointXYZ> Final; icp.align (Final); std::cout << "ICP has " << (icp.hasConverged () ? "converged" : "not converged" ) << ", score: " << icp.getFitnessScore () << std::endl; std::cout << icp.getFinalTransformation () << std::endl; pcl::visualization::PCLVisualizer viewer ("ICP Alignment" ) ; viewer.addPointCloud (cloud_in, "source" ); viewer.addPointCloud (cloud_out, "target" ); viewer.addPointCloud (Final.makeShared (), "aligned" ); viewer.spin (); return (0 ); }
其中:
1 2 3 4 5 pcl::PointCloud<pcl::PointXYZ> Final; icp.align (Final); std::cout << "ICP has " << (icp.hasConverged ()?"converged" :"not converged" ) << ", score: " << icp.getFitnessScore () << std::endl;
创建点云Final,存储配准后的结果。
如果匹配成功,则hasConverged()返回true,这意味着它们几乎是相同的。
getFitnessScore返回匹配度。
参数调整
在不单独设置参数的情况下,icp配准的参数具有默认值:
1 2 3 4 icp.setMaximumIterations (50 ); icp.setMaxCorrespondenceDistance (std::numeric_limits<double >::max ()); icp.setTransformationEpsilon (0 );
icp.setMaximumIterations:最大迭代次数
经验值:
setMaxCorrespondenceDistance: 最大对应点距离
只允许距离小于 d max d_{\max} d m a x 的点对参与优化:
∥ p i − q i ∥ < d max \|\mathbf{p}_i - \mathbf{q}_i\| < d_{\max}
∥ p i − q i ∥ < d m a x
太小 → 匹配点太少 → 解不稳定
太大 → 错误匹配 → MSE 虚假下降
∥ Δ T ∥ < ϵ \|\Delta \mathbf{T}\| < \epsilon
∥Δ T ∥ < ϵ
经验值:1e-6 ~ 1e-8
setEuclideanFitnessEpsilon:迭代使得误差降低的程度
∣ MSE k + 1 − MSE k ∣ < ϵ |\text{MSE}_{k+1} - \text{MSE}_k| < \epsilon
∣ MSE k + 1 − MSE k ∣ < ϵ
工程中常用1e-3
NDT算法
?
将目标点云按照体素(栅格)的方法划分,得到每个格子中点云的正态分布参数,再计算待配准点云在各个格子中的概率分布之和,这个值越大,那么配准结果越好。
不再直接做点对点匹配,而是把目标点云建成一系列小体素格子(Voxel/Grid),每个格子用高斯分布描述点的局部形状,然后把源点云点映射到这些概率分布上进行优化。
方法
对应点
优点
缺点
ICP
最近点
简单快速
对初值敏感,局部收敛
NDT
概率分布
对初值不敏感
体素划分,计算量大
特性
ICP
NDT
对初值敏感
高
低(更稳)
对噪声敏感
高
低(体素平滑)
收敛速度
快
稍慢(迭代 + 体素计算)
精度
高(点密度高)
中高(依赖体素大小)
代码示例
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 #include <pcl/registration/ndt.h> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> int main () { pcl::PointCloud<pcl::PointXYZ>::Ptr target (new pcl::PointCloud<pcl::PointXYZ>) ; pcl::PointCloud<pcl::PointXYZ>::Ptr source (new pcl::PointCloud<pcl::PointXYZ>) ; pcl::io::loadPCDFile ("target.pcd" , *target); pcl::io::loadPCDFile ("source.pcd" , *source); pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> ndt; ndt.setTransformationEpsilon (0.01 ); ndt.setStepSize (0.1 ); ndt.setResolution (1.0 ); ndt.setMaximumIterations (35 ); ndt.setInputSource (source); ndt.setInputTarget (target); pcl::PointCloud<pcl::PointXYZ> aligned; ndt.align (aligned); std::cout << "Has converged: " << ndt.hasConverged () << " score: " << ndt.getFitnessScore () << std::endl; }
参数
1 2 3 4 ndt.setTransformationEpsilon (1e-6 ); ndt.setStepSize (0.1 ); ndt.setResolution (1.0 ); ndt.setMaximumIterations (35 );