我是靠谱客的博主 自由板栗,这篇文章主要介绍SLAM学习:三维空间刚体变换(1),现在分享给大家,希望可以做个参考。

1. 欧式变换

相机运动是一个刚体运动,它保证了同一个向量在各个坐标系下的长度和夹角都不会发生变化。这种变换称为欧氏变换

一个欧式变换由一个旋转和一个平移两部分组成;其中,旋转矩阵表征绕各个坐标轴所旋转的角度,平移矩阵表征沿各个坐标轴移动的大小;

如:考虑世界坐标系中的向量 a,经过一次

旋转(用 R 描述)和一次平移 t 后,得到了 a,那么把旋转和平移合到一起,有:

a= Ra + t. (1)

其中,t 称为平移向量。相比于旋转,平移部分只需把这个平移量加到旋转之后的坐标上。通过上式,我们用一个旋转矩阵 R 和一个平移向量 t 完整地描述了一个欧氏空间的坐标变换关系。

2. 变换矩阵与齐次坐标

引入齐次坐标和变换矩阵重写式a= Ra + t,得到:

把一个三维向量的末尾添加 1,变成了四维向量,称为齐次坐标。对于这个四维向量,我们可以把旋转和平移写在一个矩阵里面,使得整个关系变成 了线性关系。该式中,矩阵 T 称为变换矩阵Transform Matrix);

测试:使用 Eigen 来表示矩阵、向量。

Eigen是一个 C++ 开源线性代数库。它提供了快速的有关矩阵的线性代数运算,还 包括解方程等功能。许多上层的软件库也使用 Eigen 进行矩阵运算,包括 g2oSophus 等。

Eigen的安装:

sudo apat-get install libeigen3-dev

Eigen:使用的时候,只需要在CMakeLIsts.txt指定Eigen的头文件目录即可:

# 添加Eigen头文件
include_directories( "/usr/include/eigen3" )

复制代码
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
#include <iostream> using namespace std; #include <ctime> // Eigen 部分 #include <Eigen/Core> // 稠密矩阵的代数运算(逆,特征值等) #include <Eigen/Dense> #define MATRIX_SIZE 50 /**************************** * 本程序演示了 Eigen 基本类型的使用 ****************************/ int main( int argc, char** argv ) { // Eigen 中所有向量和矩阵都是Eigen::Matrix,它是一个模板类。它的前三个参数为:数据类型,行,列 // 声明一个2*3的float矩阵 Eigen::Matrix<float, 2, 3> matrix_23; // 同时,Eigen 通过 typedef 提供了许多内置类型,不过底层仍是Eigen::Matrix // 例如 Vector3d 实质上是 Eigen::Matrix<double, 3, 1>,即三维向量 Eigen::Vector3d v_3d; // 这是一样的 Eigen::Matrix<float,3,1> vd_3d; // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3> Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero(); //初始化为零 // 如果不确定矩阵大小,可以使用动态大小的矩阵 Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_dynamic; // 更简单的 Eigen::MatrixXd matrix_x; // 这种类型还有很多,我们不一一列举 // 下面是对Eigen阵的操作 // 输入数据(初始化) matrix_23 << 1, 2, 3, 4, 5, 6; // 输出 cout << matrix_23 << endl; // 用()访问矩阵中的元素 for (int i=0; i<2; i++) { for (int j=0; j<3; j++) cout<<matrix_23(i,j)<<"t"; cout<<endl; } // 矩阵和向量相乘(实际上仍是矩阵和矩阵) v_3d << 3, 2, 1; vd_3d << 4,5,6; // 但是在Eigen里你不能混合两种不同类型的矩阵,像这样是错的 // Eigen::Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d; // 应该显式转换 Eigen::Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d; cout << result << endl; Eigen::Matrix<float, 2, 1> result2 = matrix_23 * vd_3d; cout << result2 << endl; // 同样你不能搞错矩阵的维度 // 试着取消下面的注释,看看Eigen会报什么错 // Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d; // 一些矩阵运算 // 四则运算就不演示了,直接用+-*/即可。 matrix_33 = Eigen::Matrix3d::Random(); // 随机数矩阵 cout << matrix_33 << endl << endl; cout << matrix_33.transpose() << endl; // 转置 cout << matrix_33.sum() << endl; // 各元素和 cout << matrix_33.trace() << endl; // 迹 cout << 10*matrix_33 << endl; // 数乘 cout << matrix_33.inverse() << endl; // 逆 cout << matrix_33.determinant() << endl; // 行列式 // 特征值 // 实对称矩阵可以保证对角化成功 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver ( matrix_33.transpose()*matrix_33 ); cout << "Eigen values = n" << eigen_solver.eigenvalues() << endl; cout << "Eigen vectors = n" << eigen_solver.eigenvectors() << endl; // 解方程 // 我们求解 matrix_NN * x = v_Nd 这个方程 // N的大小在前边的宏里定义,它由随机数生成 // 直接求逆自然是最直接的,但是求逆运算量大 Eigen::Matrix< double, MATRIX_SIZE, MATRIX_SIZE > matrix_NN; matrix_NN = Eigen::MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE ); Eigen::Matrix< double, MATRIX_SIZE, 1> v_Nd; v_Nd = Eigen::MatrixXd::Random( MATRIX_SIZE,1 ); clock_t time_stt = clock(); // 计时 // 直接求逆 Eigen::Matrix<double,MATRIX_SIZE,1> x = matrix_NN.inverse()*v_Nd; cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl; // 通常用矩阵分解来求,例如QR分解,速度会快很多 time_stt = clock(); x = matrix_NN.colPivHouseholderQr().solve(v_Nd); cout <<"time use in Qr decomposition is " <<1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl; return 0; }

最后

以上就是自由板栗最近收集整理的关于SLAM学习:三维空间刚体变换(1)的全部内容,更多相关SLAM学习内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部