State Estimation and Localization for Self-Driving Cars 第四周作业 利用点云进行平面拟合

1.平面拟合算法

  三维平面方程为 z = a x + b y + c z=ax+by+c z=ax+by+c。希望通过LIDAR测得的多组 ( x , y , z ) (x,y,z) (x,y,z)来拟合 ( a , b , c ) (a,b,c) (a,b,c)

测量误差为 e j = z ^ j − z j = ( a ^ + b ^ x j + c ^ y j ) − z j j = 1 … n \begin{aligned} e_{j} &=\hat{z}_{j}-z_{j} \\ &=\left(\hat{a}+\hat{b}{x_{j}}+\hat{c} y_{j}\right)-z_{j} \quad j=1 \ldots n \end{aligned} ej=z^jzj=(a^+b^xj+c^yj)zjj=1n

将多组测量数据进行罗列,得到相应的矩阵形式。
[ e 1 e 2 ⋮ e n ] ⏟ e = [ 1 x 1 y 1 1 x 2 y 2 ⋮ ⋮ ⋮ 1 x n y n ] ⏟ A [ a b c ] ⏟ x − [ z 1 z 2 ⋮ z n ] ⏟ b \underbrace{\left[\begin{array}{c} e_{1} \\ e_{2} \\ \vdots \\ e_{n} \end{array}\right]}_{e}=\underbrace{\left[\begin{array}{ccc} 1 & x_{1} & y_{1} \\ 1 & x_{2} & y_{2} \\ \vdots & \vdots & \vdots \\ 1 & x_{n} & y_{n} \end{array}\right]}_{\mathbf{A}} \underbrace{\left[\begin{array}{c} a \\ b \\ c \end{array}\right]}_{\mathbf{x}}-\underbrace{\left[\begin{array}{c} z_{1} \\ z_{2} \\ \vdots \\ z_{n} \end{array}\right]}_{\mathbf{b}} e e1e2en=A 111x1x2xny1y2ynx abcb z1z2zn

这是一个最小二乘问题,其解为
x ^ = ( A T A ) − 1 A T b \hat{\mathbf{x}}=\left(\mathbf{A}^{T} \mathbf{A}\right)^{-1} \mathbf{A}^{T} \mathbf{b} x^=(ATA)1ATb

2. 相关程序

from numpy import *
import math
import numpy as np
def estimate_params§:
“”"
Estimate parameters from sensor readings in the Cartesian frame.
Each row in the P matrix contains a single 3D point measurement;
the matrix P has size n x 3 (for n points). The format is:

P = [[x1, y1, z1],
[x2, x2, z2], …]

where all coordinate values are in metres. Three parameters are
required to fit the plane, a, b, and c, according to the equation

z = a + bx + cy

The function should retrn the parameters as a NumPy array of size
three, in the order [a, b, c].
“”"
param_est = zeros(3)

A = np.hstack([ones([len§, 1]), P[:, :2]])
b = P[:, 2:]
cc = linalg.inv(A.T.dot(A)).dot(A.T).dot(b)
param_est[0] = cc[0,0]
param_est[1] = cc[1,0]
param_est[2] = cc[2,0]

return param_est

注释:python 语言中,将两个矩阵左右拼接可以用np.hstack

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
PCL(Point Cloud Library)是一个非常流行的点云处理库,其中包含了许多点云处理的算法和工具函数。下面是使用PCL对点云进行平面拟合的示例代码: ```cpp #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/sample_consensus/method_types.h> #include <pcl/sample_consensus/model_types.h> #include <pcl/segmentation/sac_segmentation.h> pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // Fill in the cloud data cloud->width = 15; cloud->height = 1; cloud->points.resize (cloud->width * cloud->height); for (size_t i = 0; i < cloud->points.size (); ++i) { cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f); cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f); cloud->points[i].z = 1.0; } // Create the normal estimation class, and pass the input dataset to it pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud (cloud); // Create an empty kdtree representation, and pass it to the normal estimation object. // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given). pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ()); ne.setSearchMethod (tree); // Output datasets pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>); // Use all neighbors in a sphere of radius 3cm ne.setRadiusSearch (0.03); // Compute the features ne.compute (*cloud_normals); // Create the segmentation object for the planar model and set all the parameters pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg; seg.setOptimizeCoefficients (true); seg.setModelType (pcl::SACMODEL_PLANE); seg.setMethodType (pcl::SAC_RANSAC); seg.setMaxIterations (1000); seg.setDistanceThreshold (0.01); seg.setInputCloud (cloud); seg.setInputNormals (cloud_normals); // Obtain the plane inliers and coefficients pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); pcl::PointIndices::Ptr inliers (new pcl::PointIndices); seg.segment (*inliers, *coefficients); // Print the model coefficients std::cerr << "Model coefficients: " << coefficients->values[0] << " " << coefficients->values[1] << " " << coefficients->values[2] << " " << coefficients->values[3] << std::endl; ``` 这段代码的主要作用是随机生成一个包含15个点的点云,然后使用PCL对点云进行平面拟合。具体步骤如下: 1. 创建一个PointCloud对象,并随机生成15个点。 2. 创建一个NormalEstimation对象,并设置输入点云和搜索方法。 3. 创建一个PointCloud对象,用于存储法线。 4. 设置法线估计的参数,并计算法线。 5. 创建一个SACSegmentationFromNormals对象,并设置模型类型、方法类型、最大迭代次数和距离阈值等参数。 6. 设置输入点云和法线,执行平面拟合。 7. 输出平面拟合的模型系数。 需要注意的是,这里使用的是SACSegmentationFromNormals算法,是基于法线的平面拟合,因此需要先计算法线。如果不需要计算法线,可以使用SACSegmentation算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值