1.前言
聊sim3之前需要聊聊单目视觉slam的尺度漂移问题:
单目尺度漂移问题:
单目视觉里程计前端主要涉及以下内容:
- 单目初始化:选择满足条件的前后帧图像,进行特征点提取与匹配,借助对极几何求取本质矩阵E,将本质矩阵分解得到前后帧位姿的R、t分量,随后,对图像匹配上的特征点进行三角化,以恢复空间点。(ps:分解得到的t仅是满足极线约束的特解,t乘以任意缩放系数所得到的新值仍满足约束条件。)
- 初始化成功后,对新进来的每一帧图像仍进行特征点提取与匹配,其中,和前一帧图像匹配上的部分特征点已三角化成功,此时,可以利用运动模型+BA/IMU预积分+BA/PNP,解算新图像帧与上一阵的R,t关系,并利用R,t对剩余特征点进行三角化。
然而,在第二步解算的R,t始终存在误差,这造成利用三角化恢复的特征点空间位置不准确,对新来的每一帧图像均存在这一问题,误差通过图像传递并积累,最终造成一段时间后三角化出来的特征点和初始化时的尺度不一致,即造成尺度漂移问题。
2.相似变换 sim3
由于上述单目尺度漂移的问题,在单目视觉检测到闭环时,当前帧和闭环帧对应的空间点在各自相机坐标系下的坐标不仅存在旋转R、平移t关系,还存在缩放s的关系,这一对应关系称为相似变换(Similarity Transformation),简称sim3,sim3= [ s R t 0 1 ] \begin{bmatrix} sR & t \\ 0& 1 \\ \end{bmatrix} [sR0t1]。
2.1 数学建模
给定匹配成功的两组三维点,分别记为{
P
i
P_i
Pi}、{
Q
i
Q_i
Qi},需找到如下变换关系,以描述{
P
i
P_i
Pi}到{
Q
i
Q_i
Qi}相似变换关系:
Q
i
=
s
R
P
i
+
t
(1)
Q_{i}=s R P_{i}+t \tag1
Qi=sRPi+t(1)
由于数据中不可避免存在噪声与误差,我们将上述任务转换为最小二乘求解问题,即寻找合适的尺度因子、旋转和平移,使得它在所有数据上的误差最小:
e
i
=
Q
i
−
s
R
P
i
−
t
min
s
,
R
,
t
∑
i
=
1
n
∥
e
i
∥
2
=
min
s
,
R
,
t
∑
i
=
1
n
∥
Q
i
−
s
R
P
i
−
t
∥
2
(2)
\begin{aligned} e_{i} &=Q_{i}-s R P_{i}-t \\ \min _{s, R, t} \sum_{i=1}^{n}\left\|e_{i}\right\|^{2} &=\min _{s, R, t} \sum_{i=1}^{n}\left\|Q_{i}-s R P_{i}-t\right\|^{2} \end{aligned}\tag2
eis,R,tmini=1∑n∥ei∥2=Qi−sRPi−t=s,R,tmini=1∑n∥Qi−sRPi−t∥2(2)
在正式开始求解时,需先定义一些相关变量:
两个三维点集合的质心定义如下:
P
ˉ
=
1
n
∑
i
=
1
n
P
i
Q
ˉ
=
1
n
∑
i
=
1
n
Q
i
(3)
\begin{aligned} &\bar{P}=\frac{1}{n} \sum_{i=1}^{n} P_{i} \\ &\bar{Q}=\frac{1}{n} \sum_{i=1}^{n} Q_{i} \end{aligned}\tag3
Pˉ=n1i=1∑nPiQˉ=n1i=1∑nQi(3)
每个{
P
i
P_i
Pi}、{
Q
i
Q_i
Qi}的去中心化坐标
P
i
′
,
Q
i
′
P_{i}^{\prime}, Q_{i}^{\prime}
Pi′,Qi′:
P
i
′
=
P
i
−
P
ˉ
Q
i
′
=
Q
i
−
Q
ˉ
(4)
\begin{aligned} &P_{i}^{\prime}=P_{i}-\bar{P} \\ &Q_{i}^{\prime}=Q_{i}-\bar{Q} \end{aligned}\tag4
Pi′=Pi−PˉQi′=Qi−Qˉ(4)
其中:
P
i
′
,
Q
i
′
P_{i}^{\prime}, Q_{i}^{\prime}
Pi′,Qi′满足:
∑
i
=
1
n
P
i
′
=
∑
i
=
1
n
(
P
i
−
P
ˉ
)
=
∑
i
=
1
n
P
i
−
n
P
ˉ
=
0
∑
i
=
1
n
Q
i
′
=
∑
i
=
1
n
(
Q
i
−
Q
ˉ
)
=
∑
i
=
1
n
Q
i
−
n
Q
ˉ
=
0
(5)
\begin{aligned} &\sum_{i=1}^{n} P_{i}^{\prime}=\sum_{i=1}^{n}\left(P_{i}-\bar{P}\right)=\sum_{i=1}^{n} P_{i}-n \bar{P}=0 \\ &\sum_{i=1}^{n} Q_{i}^{\prime}=\sum_{i=1}^{n}\left(Q_{i}-\bar{Q}\right)=\sum_{i=1}^{n} Q_{i}-n \bar{Q}=0 \end{aligned}\tag5
i=1∑nPi′=i=1∑n(Pi−Pˉ)=i=1∑nPi−nPˉ=0i=1∑nQi′=i=1∑n(Qi−Qˉ)=i=1∑nQi−nQˉ=0(5)
2.2 求解平移t
对(2)中的误差方程进行如下变换
∑
i
=
1
n
∥
e
i
∥
2
=
∑
i
=
1
n
∥
Q
i
−
s
R
P
i
−
t
∥
2
=
∑
i
=
1
n
∥
Q
i
′
+
Q
ˉ
−
s
R
P
i
′
−
s
R
P
ˉ
−
t
∥
2
=
∑
i
=
1
n
∥
(
Q
i
′
−
s
R
P
i
′
)
+
(
Q
ˉ
−
s
R
P
ˉ
−
t
)
⏟
t
0
∥
2
=
∑
i
=
1
n
∥
(
Q
i
′
−
s
R
P
i
′
)
∥
2
+
2
t
0
∑
i
=
1
n
(
Q
i
′
−
s
R
P
i
′
)
+
n
∥
t
0
∥
2
(6)
\begin{aligned} \sum_{i=1}^{n}\left\|e_{i}\right\|^{2} &=\sum_{i=1}^{n}\left\|Q_{i}-s R P_{i}-t\right\|^{2} \\ &=\sum_{i=1}^{n}\left\|Q_{i}^{\prime}+\bar{Q}-s R P_{i}^{\prime}-s R \bar{P}-t\right\|^{2} \\ &=\sum_{i=1}^{n}\|\left(Q_{i}^{\prime}-s R P_{i}^{\prime}\right)+\underbrace{(\bar{Q}-s R \bar{P}-t)}_{t_{0}}\|^{2} \\ &=\sum_{i=1}^{n}\left\|\left(Q_{i}^{\prime}-s R P_{i}^{\prime}\right)\right\|^{2}+2 t_{0} \sum_{i=1}^{n}\left(Q_{i}^{\prime}-s R P_{i}^{\prime}\right)+n\left\|t_{0}\right\|^{2} \end{aligned}\tag6
i=1∑n∥ei∥2=i=1∑n∥Qi−sRPi−t∥2=i=1∑n∥∥Qi′+Qˉ−sRPi′−sRPˉ−t∥∥2=i=1∑n∥(Qi′−sRPi′)+t0
(Qˉ−sRPˉ−t)∥2=i=1∑n∥(Qi′−sRPi′)∥2+2t0i=1∑n(Qi′−sRPi′)+n∥t0∥2(6)
简记:
t
0
=
Q
ˉ
−
s
R
P
ˉ
−
t
(7)
t_{0}=\bar{Q}-s R \bar{P}-t \tag7
t0=Qˉ−sRPˉ−t(7)
由等式(5)可知,(6)式中间项:
∑
i
=
1
n
(
Q
i
′
−
s
R
P
i
′
)
=
∑
i
=
1
n
Q
i
′
−
s
R
∑
i
=
1
n
P
i
′
=
0
\sum_{i=1}^{n}\left(Q_{i}^{\prime}-s R P_{i}^{\prime}\right)=\sum_{i=1}^{n} Q_{i}^{\prime}-s R \sum_{i=1}^{n} P_{i}^{\prime}=0
i=1∑n(Qi′−sRPi′)=i=1∑nQi′−sRi=1∑nPi′=0
故误差方程(6)进一步化简为:
∑
i
=
1
n
∥
e
i
∥
2
=
∑
i
=
1
n
∥
(
Q
i
′
−
s
R
P
i
′
)
∥
2
+
n
∥
t
0
∥
2
(8)
\sum_{i=1}^{n}\left\|e_{i}\right\|^{2}=\sum_{i=1}^{n}\left\|\left(Q_{i}^{\prime}-s R P_{i}^{\prime}\right)\right\|^{2}+n\left\|t_{0}\right\|^{2}\tag8
i=1∑n∥ei∥2=i=1∑n∥(Qi′−sRPi′)∥2+n∥t0∥2(8)
(8)式仅有第二项与待求的平移t有关,易当平方项
∥
t
0
∥
2
\left\|t_{0}\right\|^{2}
∥t0∥2=0时,误差方程取得最小值,即平移最优解
t
∗
t^*
t∗为
t
0
=
Q
ˉ
−
s
R
P
ˉ
−
t
=
0
t
∗
=
Q
ˉ
−
s
R
P
ˉ
(9)
\begin{aligned} &t_{0}=\bar{Q}-s R \bar{P}-t=0 \\ &t^{*}=\bar{Q}-s R \bar{P} \end{aligned}\tag9
t0=Qˉ−sRPˉ−t=0t∗=Qˉ−sRPˉ(9)
即,若我们已知旋转R与缩放系数s,即可求取平移最优解
t
∗
t^*
t∗。
2.3求解缩放因子
误差方程(8)可进一步简化为:
∑
i
=
1
n
∥
e
i
∥
2
=
∑
i
=
1
n
∥
Q
i
′
−
s
R
P
i
′
∥
2
=
∑
i
=
1
n
∥
Q
i
′
∥
2
−
2
s
∑
i
=
1
n
Q
i
′
R
P
i
′
+
s
2
∑
i
=
1
n
∥
R
P
i
′
∥
2
(10)
\begin{aligned} \sum_{i=1}^{n}\left\|e_{i}\right\|^{2} &=\sum_{i=1}^{n}\left\|Q_{i}^{\prime}-s R P_{i}^{\prime}\right\|^{2} \\ &=\sum_{i=1}^{n}\left\|Q_{i}^{\prime}\right\|^{2}-2 s \sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}+s^{2} \sum_{i=1}^{n}\left\|R P_{i}^{\prime}\right\|^{2}\tag{10} \end{aligned}
i=1∑n∥ei∥2=i=1∑n∥Qi′−sRPi′∥2=i=1∑n∥Qi′∥2−2si=1∑nQi′RPi′+s2i=1∑n∥RPi′∥2(10)
由于向量模长不受旋转的影响,故
∥
R
P
i
′
∥
2
=
∥
P
i
′
∥
2
\left\|R P_{i}^{\prime}\right\|^{2}=\left\|P_{i}^{\prime}\right\|^{2}
∥RPi′∥2=∥Pi′∥2,(10)式可进一步表示为:
∑
i
=
1
n
∥
e
i
∥
2
=
∑
i
=
1
n
∥
Q
i
′
∥
2
⏟
S
Q
−
2
s
∑
i
=
1
n
Q
i
′
R
P
i
′
⏟
D
+
s
2
∑
i
=
1
n
∥
P
i
′
∥
2
⏟
S
P
=
S
Q
−
2
s
D
+
s
2
S
P
(11)
\begin{aligned} \sum_{i=1}^{n}\left\|e_{i}\right\|^{2} &=\underbrace{\sum_{i=1}^{n}\left\|Q_{i}^{\prime}\right\|^{2}}_{S_{Q}}-2 s \underbrace{\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}}_{D}+s^{2} \underbrace{\sum_{i=1}^{n}\left\|P_{i}^{\prime}\right\|^{2}}_{S_{P}} \\ &=S_{Q}-2 s D+s^{2} S_{P} \end{aligned}\tag{11}
i=1∑n∥ei∥2=SQ
i=1∑n∥Qi′∥2−2sD
i=1∑nQi′RPi′+s2SP
i=1∑n∥Pi′∥2=SQ−2sD+s2SP(11)
式(11)是关于s的一元二次方程,其极小值点为:
s
=
D
S
P
=
∑
i
=
1
n
Q
i
′
R
P
i
′
∑
i
=
1
n
∥
P
i
′
∥
2
(12)
s=\frac{D}{S_{P}}=\frac{\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}}{\sum_{i=1}^{n}\left\|P_{i}^{\prime}\right\|^{2}}\tag{12}
s=SPD=∑i=1n∥Pi′∥2∑i=1nQi′RPi′(12)
注意上述s为的方向为{
P
i
P_i
Pi}→{
Q
i
Q_i
Qi},然而按照此方法计算的
s
Q
i
→
P
i
=
∑
i
=
1
n
P
i
′
R
T
Q
i
′
∑
i
=
1
n
∥
Q
i
′
∥
2
≠
1
s
s_{Q_i→P_i}=\frac{\sum_{i=1}^{n} P_{i}^{\prime} R^{T} Q_{i}^{\prime}}{\sum_{i=1}^{n}\left\|Q_{i}^{\prime}\right\|^{2}} \neq \frac{1}{s}
sQi→Pi=∑i=1n∥Qi′∥2∑i=1nPi′RTQi′=s1
即按照上述求解的缩放因子不具备对称性,因此对式(10)重新简化为:
∑
i
=
1
n
∥
e
i
∥
2
=
∑
i
=
1
n
∥
1
s
Q
i
′
−
s
R
P
i
′
∥
2
=
1
s
∑
i
=
1
n
∥
Q
i
′
∥
2
⏟
S
Q
−
2
∑
i
=
1
n
Q
i
′
R
P
i
′
⏟
D
+
s
∑
i
=
1
n
∥
R
P
i
′
∥
2
⏟
S
P
=
1
s
S
Q
−
2
D
+
s
S
P
=
(
s
S
P
−
S
Q
s
)
2
+
2
(
S
P
S
Q
−
D
)
(13)
\begin{aligned} \sum_{i=1}^{n}\left\|e_{i}\right\|^{2} &=\sum_{i=1}^{n}\left\|\frac{1}{\sqrt{s}} Q_{i}^{\prime}-\sqrt{s} R P_{i}^{\prime}\right\|^{2} \\ &=\frac{1}{s} \underbrace{\sum_{i=1}^{n}\left\|Q_{i}^{\prime}\right\|^{2}}_{S_{Q}}-2 \underbrace{\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}}_{D}+s \underbrace{\sum_{i=1}^{n}\left\|R P_{i}^{\prime}\right\|^{2}}_{S_{P}} \\ &=\frac{1}{s} S_{Q}-2 D+s S_{P} \\ &=\left(\sqrt{s S_{P}}-\sqrt{\frac{S_{Q}}{s}}\right)^{2}+2\left(S_{P} S_{Q}-D\right) \end{aligned}\tag{13}
i=1∑n∥ei∥2=i=1∑n∥∥∥∥s1Qi′−sRPi′∥∥∥∥2=s1SQ
i=1∑n∥Qi′∥2−2D
i=1∑nQi′RPi′+sSP
i=1∑n∥RPi′∥2=s1SQ−2D+sSP=(sSP−sSQ)2+2(SPSQ−D)(13)
令第一项为0,可得满足对称性的缩放因子
s
∗
=
S
Q
S
P
=
∑
i
=
1
n
∥
Q
i
′
∥
2
∑
i
=
1
n
∥
P
i
′
∥
2
s^{*}=\sqrt{\frac{S_{Q}}{S_{P}}}=\sqrt{\frac{\sum_{i=1}^{n}\left\|Q_{i}^{\prime}\right\|^{2}}{\sum_{i=1}^{n}\left\|P_{i}^{\prime}\right\|^{2}}}
s∗=SPSQ=∑i=1n∥Pi′∥2∑i=1n∥Qi′∥2
此时,误差方程仅剩第二项,令
D
=
∑
i
=
1
n
Q
i
′
R
P
i
′
D=\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}
D=∑i=1nQi′RPi′最大,即可使误差方程最小。
2.4 求解旋转R
由于旋转矩阵受制于矩阵正交性的约束,而四元数代替旋转矩阵R仅需保证模长为1的约束,故使用四元数代替矩阵进行旋转的求解。
- 四元数的旋转计算:
空间三维点 P = [ x , y , z ] P=[x, y, z] P=[x,y,z]对应的四元数表示为 p ˙ = [ 0 , x , y , z ] T \dot{p}=[0, x, y, z]^{T} p˙=[0,x,y,z]T 。
旋转用一个单位四元数 q ˙ \dot{q} q˙ 来表示,则 p ˙ \dot{p} p˙ 旋转后的三维点用四元数表示为:
p ˙ ′ = q ˙ p ˙ q ˙ − 1 = q ˙ p ˙ q ˙ ∗ \dot{p}^{\prime}=\dot{q} \dot{p} \dot{q}^{-1}=\dot{q} \dot{p} \dot{q}^{*} p˙′=q˙p˙q˙−1=q˙p˙q˙∗
四元数 p ˙ ′ \dot{p}^{\prime} p˙′ 的虚部即为旋转后的坐标。其中 q ˙ ∗ \dot{q}^{*} q˙∗ 表示取 q ˙ \dot{q} q˙ 的共轭。 - 四元数的性质1
三个四元数满足如下条件。直接相乘的形式,表示四元数乘法,中间的 ⋅ \cdot ⋅ 表示向量点乘。
p ˙ ⋅ ( r ˙ q ˙ ∗ ) = ( p ˙ q ˙ ) ⋅ r ˙ \dot{p} \cdot\left(\dot{r} \dot{q}^{*}\right)=(\dot{p} \dot{q}) \cdot \dot{r} p˙⋅(r˙q˙∗)=(p˙q˙)⋅r˙ - 四元数的性质2
假设四元数 r ˙ = [ r 0 , r x , r y , r z ] \dot{r}=\left[r_{0}, r_{x}, r_{y}, r_{z}\right] r˙=[r0,rx,ry,rz] ,则有
r ˙ q ˙ = [ r 0 − r x − r y − r z r x r 0 − r z r y r y r z r 0 − r x r z − r y r x r 0 ] q ˙ = R q ˙ q ˙ r ˙ = [ r 0 − r x − r y − r z r x r 0 r z − r y r y − r z r 0 r x r z r y − r x r 0 ] q ˙ = R ‾ q ˙ \begin{aligned} \dot{r} \dot{q} &=\left[\begin{array}{rrrr} r_{0} & -r_{x} & -r_{y} & -r_{z} \\ r_{x} & r_{0} & -r_{z} & r_{y} \\ r_{y} & r_{z} & r_{0} & -r_{x} \\ r_{z} & -r_{y} & r_{x} & r_{0} \end{array}\right] \dot{q}=\mathbb{R} \dot{q} \\ \dot{q} \dot{r} &=\left[\begin{array}{rrrr} r_{0} & -r_{x} & -r_{y} & -r_{z} \\ r_{x} & r_{0} & r_{z} & -r_{y} \\ r_{y} & -r_{z} & r_{0} & r_{x} \\ r_{z} & r_{y} & -r_{x} & r_{0} \end{array}\right] \dot{q}=\overline{\mathbb{R}} \dot{q} \end{aligned} r˙q˙q˙r˙=⎣⎢⎢⎡r0rxryrz−rxr0rz−ry−ry−rzr0rx−rzry−rxr0⎦⎥⎥⎤q˙=Rq˙=⎣⎢⎢⎡r0rxryrz−rxr0−rzry−ryrzr0−rx−rz−ryrxr0⎦⎥⎥⎤q˙=Rq˙
其中 R , R ‾ \mathbb{R}, \overline{\mathbb{R}} R,R 都是 4 × 4 4 \times 4 4×4 的对称矩阵。
下面进入旋转矩阵R的求解,求误差方程(13)的最小值等价于求下式的最大值:
D
=
∑
i
=
1
n
Q
i
′
R
P
i
′
=
∑
i
=
1
n
(
Q
˙
i
′
)
⋅
(
q
˙
P
˙
i
′
q
˙
∗
)
=
∑
i
=
1
n
(
Q
˙
i
′
q
˙
)
⋅
(
q
˙
P
˙
i
′
)
=
∑
i
=
1
n
(
R
Q
,
i
˙
q
˙
)
⋅
(
R
P
,
i
‾
q
˙
)
=
∑
i
=
1
n
q
˙
T
R
Q
,
i
T
R
P
,
i
‾
q
˙
=
q
˙
T
(
∑
i
=
1
n
R
Q
,
i
T
R
P
,
i
‾
)
q
˙
=
q
˙
T
N
q
˙
(14)
\begin{aligned} D=\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime} &=\sum_{i=1}^{n}\left(\dot{Q}_{i}^{\prime}\right) \cdot\left(\dot{q} \dot{P}_{i}^{\prime} \dot{q}^{*}\right) \\ &=\sum_{i=1}^{n}\left(\dot{Q}_{i}^{\prime} \dot{q}\right) \cdot\left(\dot{q} \dot{P}_{i}^{\prime}\right) \\ &=\sum_{i=1}^{n}\left(\mathbb{R}_{\mathbb{Q}, \dot{i}} \dot{q}\right) \cdot\left(\overline{\mathbb{R}_{\mathbb{P}, \mathrm{i}}} \dot{q}\right) \\ &=\sum_{i=1}^{n} \dot{q}^{T} \mathbb{R}_{\mathbb{Q}, \mathrm{i}}^{\mathbb{T}} \overline{\mathbb{R}_{\mathbb{P}, \mathrm{i}}} \dot{q} \\ &=\dot{q}^{T}\left(\sum_{i=1}^{n} \mathbb{R}_{\mathbb{Q}, \mathrm{i}}^{\mathbb{T}} \overline{\mathbb{R}_{\mathbb{P}, \mathrm{i}}}\right) \dot{q} \\ &=\dot{q}^{T} N \dot{q} \end{aligned} \tag{14}
D=i=1∑nQi′RPi′=i=1∑n(Q˙i′)⋅(q˙P˙i′q˙∗)=i=1∑n(Q˙i′q˙)⋅(q˙P˙i′)=i=1∑n(RQ,i˙q˙)⋅(RP,iq˙)=i=1∑nq˙TRQ,iTRP,iq˙=q˙T(i=1∑nRQ,iTRP,i)q˙=q˙TNq˙(14)
其中:
Q
i
′
=
[
Q
i
,
x
′
,
Q
i
,
y
′
,
Q
i
,
z
′
]
T
P
i
′
=
[
P
i
,
x
′
,
P
i
,
y
′
,
P
i
,
z
′
]
T
\begin{aligned} Q_{i}^{\prime} &=\left[Q_{i, x}^{\prime}, Q_{i, y}^{\prime}, Q_{i, z}^{\prime}\right]^{T} \\ P_{i}^{\prime} &=\left[P_{i, x}^{\prime}, P_{i, y}^{\prime}, P_{i, z}^{\prime}\right]^{T} \end{aligned}
Qi′Pi′=[Qi,x′,Qi,y′,Qi,z′]T=[Pi,x′,Pi,y′,Pi,z′]T
Q
˙
i
′
q
˙
=
[
0
−
Q
i
,
x
′
−
Q
i
,
y
′
−
Q
i
,
z
′
Q
i
,
x
′
0
−
Q
i
,
z
′
Q
i
,
y
′
Q
i
,
y
′
Q
i
,
z
′
0
−
Q
i
,
x
′
Q
i
,
z
′
−
Q
i
,
y
′
Q
i
,
x
′
0
]
q
˙
=
R
Q
,
i
q
˙
q
˙
P
˙
i
′
=
[
0
−
P
i
,
x
′
−
P
i
,
y
′
−
,
P
i
,
z
′
P
i
,
x
′
0
,
P
i
,
z
′
−
P
i
,
y
′
P
i
,
y
′
−
,
P
i
,
z
′
0
P
i
,
x
′
,
P
i
,
z
′
P
i
,
y
′
−
P
i
,
x
′
0
]
q
˙
=
R
P
,
i
‾
q
˙
\begin{aligned} \dot{Q}_{i}^{\prime} \dot{q}=\left[\begin{array}{cccc} 0 & -Q_{i, x}^{\prime} & -Q_{i, y}^{\prime} & -Q_{i, z}^{\prime} \\ Q_{i, x}^{\prime} & 0 & -Q_{i, z}^{\prime} & Q_{i, y}^{\prime} \\ Q_{i, y}^{\prime} & Q_{i, z}^{\prime} & 0 & -Q_{i, x}^{\prime} \\ Q_{i, z}^{\prime} & -Q_{i, y}^{\prime} & Q_{i, x}^{\prime} & 0 \end{array}\right] \dot{q}=\mathbb{R}_{\mathbb{Q}, i} \dot{q}\\ \dot{q} \dot{P}_{i}^{\prime}=\left[\begin{array}{cccc} 0 & -P_{i, x}^{\prime} & -P_{i, y}^{\prime} & -, P_{i, z}^{\prime} \\ P_{i, x}^{\prime} & 0 & , P_{i, z}^{\prime} & -P_{i, y}^{\prime} \\ P_{i, y}^{\prime} & -, P_{i, z}^{\prime} & 0 & P_{i, x}^{\prime} \\ , P_{i, z}^{\prime} & P_{i, y}^{\prime} & -P_{i, x}^{\prime} & 0 \end{array}\right] \dot{q}=\overline{\mathbb{R}_{P, \mathrm{i}}} \dot{q} \end{aligned}
Q˙i′q˙=⎣⎢⎢⎡0Qi,x′Qi,y′Qi,z′−Qi,x′0Qi,z′−Qi,y′−Qi,y′−Qi,z′0Qi,x′−Qi,z′Qi,y′−Qi,x′0⎦⎥⎥⎤q˙=RQ,iq˙q˙P˙i′=⎣⎢⎢⎡0Pi,x′Pi,y′,Pi,z′−Pi,x′0−,Pi,z′Pi,y′−Pi,y′,Pi,z′0−Pi,x′−,Pi,z′−Pi,y′Pi,x′0⎦⎥⎥⎤q˙=RP,iq˙
我们定义
M
=
∑
i
=
1
n
P
i
′
Q
i
′
T
=
[
S
x
x
S
x
y
S
x
z
S
y
x
S
y
y
S
y
z
S
z
x
S
z
y
S
z
z
]
\begin{aligned} M &=\sum_{i=1}^{n} P_{i}^{\prime} Q_{i}^{\prime T} \\ &=\left[\begin{array}{lll} S_{x x} & S_{x y} & S_{x z} \\ S_{y x} & S_{y y} & S_{y z} \\ S_{z x} & S_{z y} & S_{z z} \end{array}\right] \end{aligned}
M=i=1∑nPi′Qi′T=⎣⎡SxxSyxSzxSxySyySzySxzSyzSzz⎦⎤
其中
S
x
x
=
∑
i
=
1
n
P
i
,
x
Q
i
,
x
S
x
y
=
∑
i
=
1
n
P
i
,
x
Q
i
,
y
\begin{aligned} S_{x x} &=\sum_{i=1}^{n} P_{i, x} Q_{i, x} \\ S_{x y} &=\sum_{i=1}^{n} P_{i, x} Q_{i, y} \end{aligned}
SxxSxy=i=1∑nPi,xQi,x=i=1∑nPi,xQi,y
则
N
=
∑
i
=
1
n
R
Q
,
i
T
R
P
,
i
‾
=
[
(
S
x
x
+
S
y
y
+
S
z
z
)
S
y
z
−
S
z
y
S
z
x
−
S
x
z
S
x
y
−
S
y
x
S
y
z
−
S
z
y
(
S
x
x
−
S
y
y
−
S
z
z
)
S
x
y
+
S
y
x
S
z
x
+
S
x
z
S
z
x
−
S
x
z
S
x
y
+
S
y
x
(
−
S
x
x
+
S
y
y
−
S
z
z
)
S
y
z
+
S
z
y
S
x
y
−
S
y
x
S
z
x
+
S
x
z
S
y
z
+
S
z
y
(
−
S
x
x
−
S
y
y
+
S
z
z
)
]
\begin{aligned} N &=\sum_{i=1}^{n} \mathbb{R}_{\mathbb{Q}, \mathrm{i}}^{\mathbb{T}} \overline{\mathbb{R}_{\mathbb{P}, \mathrm{i}}} \\ &=\left[\begin{array}{cccc} \left(S_{x x}+S_{y y}+S_{z z}\right) & S_{y z}-S_{z y} & S_{z x}-S_{x z} & S_{x y}-S_{y x} \\ S_{y z}-S_{z y} & \left(S_{x x}-S_{y y}-S_{z z}\right) & S_{x y}+S_{y x} & S_{z x}+S_{x z} \\ S_{z x}-S_{x z} & S_{x y}+S_{y x} & \left(-S_{x x}+S_{y y}-S_{z z}\right) & S_{y z}+S_{z y} \\ S_{x y}-S_{y x} & S_{z x}+S_{x z} & S_{y z}+S_{z y} & \left(-S_{x x}-S_{y y}+S_{z z}\right) \end{array}\right] \end{aligned}
N=i=1∑nRQ,iTRP,i=⎣⎢⎢⎡(Sxx+Syy+Szz)Syz−SzySzx−SxzSxy−SyxSyz−Szy(Sxx−Syy−Szz)Sxy+SyxSzx+SxzSzx−SxzSxy+Syx(−Sxx+Syy−Szz)Syz+SzySxy−SyxSzx+SxzSyz+Szy(−Sxx−Syy+Szz)⎦⎥⎥⎤
式(14)等价于:
q
˙
=
a
r
g
m
a
x
q
˙
{
q
˙
T
N
q
˙
}
,
s
u
b
j
e
c
t
t
o
∣
∣
q
˙
∣
∣
2
=
1
(15)
\dot{q}= arg \underset{\dot q}{\mathrm{max}} \{\dot{q}^{T} N \dot{q}\},subject\ to ||\dot q||^2=1 \tag{15}
q˙=argq˙max{q˙TNq˙},subject to∣∣q˙∣∣2=1(15)
依据拉格朗日法:
L
(
q
˙
,
λ
)
=
q
˙
T
N
q
˙
+
λ
(
1
−
∣
∣
q
˙
∣
∣
2
)
(16)
L(\dot{q},\lambda)= \dot{q}^{T} N \dot{q}+\lambda(1-||\dot q||^2)\tag{16}
L(q˙,λ)=q˙TNq˙+λ(1−∣∣q˙∣∣2)(16)
对
q
˙
、
λ
\dot{q}、\lambda
q˙、λ计算偏导得:
L
′
(
q
˙
)
=
2
N
q
˙
−
2
λ
q
˙
=
0
(17)
L'(\dot{q})=2N\dot{q}-2\lambda \dot{q}=0 \tag{17}
L′(q˙)=2Nq˙−2λq˙=0(17)
L
′
(
λ
)
=
1
−
∣
∣
q
˙
∣
∣
2
=
0
(18)
L'(\lambda)=1-||\dot{q}||^2=0\tag{18}
L′(λ)=1−∣∣q˙∣∣2=0(18)
由(17)可知
q
˙
\dot{q}
q˙是矩阵N特征值为
λ
\lambda
λ的特征向量
将(17)代入(15)式可得:
q
˙
T
N
q
˙
=
q
˙
T
λ
q
˙
=
λ
(5)
\dot{q}^{T} N \dot{q}=\dot{q}^{T}\lambda \dot{q}=\lambda \tag{5}
q˙TNq˙=q˙Tλq˙=λ(5)
因此,当
λ
\lambda
λ取最大值时,
q
˙
T
N
q
˙
\dot{q}^{T} N \dot{q}
q˙TNq˙取得最大值,此时x对应为
N
N
N最大特征值对应的特征向量;
对N进行特征值分解,求得最大特征值对应的特征向量就是待求的用四元数表示的{
P
i
P_i
Pi}到{
Q
i
Q_i
Qi}的旋转 。
3.总结
sim3计算步骤:
1、先计算旋转
R
R
R 。
具体来说,先构建
M
\mathrm{M}
M 矩阵
M
=
∑
i
=
1
n
P
i
′
Q
i
′
T
=
[
S
x
x
S
x
y
S
x
z
S
y
x
S
y
y
S
y
z
S
z
x
S
z
y
S
z
z
]
\begin{aligned} M &=\sum_{i=1}^{n} P_{i}^{\prime} Q_{i}^{\prime T} \\ &=\left[\begin{array}{lll} S_{x x} & S_{x y} & S_{x z} \\ S_{y x} & S_{y y} & S_{y z} \\ S_{z x} & S_{z y} & S_{z z} \end{array}\right] \end{aligned}
M=i=1∑nPi′Qi′T=⎣⎡SxxSyxSzxSxySyySzySxzSyzSzz⎦⎤
然后得到矩阵
N
N
N ,对
N
N
N 进行特征值分解,求得最大特征值对应的特征向量就是待求的用四元数表示的旋 转
R
R
R 。注意这里旋转的方向是
{
P
i
}
→
{
Q
i
}
\left\{P_{i}\right\} \rightarrow\left\{Q_{i}\right\}
{Pi}→{Qi} 。
2、根据上面计算的旋转
R
R
R 来计算尺度
s
s
s 。
具体来说,可以使用以下两种方法来计算,第一种是具有对称性的尺度(推荐)
s
=
S
Q
S
P
=
∑
i
=
1
n
∥
Q
i
r
∥
2
∑
i
=
1
n
∥
P
i
′
∥
2
s=\sqrt{\frac{S_{Q}}{S_{P}}}=\sqrt{\frac{\sum_{i=1}^{n}\left\|Q_{i}^{r}\right\|^{2}}{\sum_{i=1}^{n}\left\|P_{i}^{\prime}\right\|^{2}}}
s=SPSQ=∑i=1n∥Pi′∥2∑i=1n∥Qir∥2
第二种是不具有对称性的尺度 (ORBSLAM使用)
s
=
D
S
p
=
∑
i
=
1
n
Q
i
′
R
P
i
′
∑
i
=
1
n
∥
P
i
′
∥
2
s=\frac{D}{S_{p}}=\frac{\sum_{i=1}^{n} Q_{i}^{\prime} R P_{i}^{\prime}}{\sum_{i=1}^{n}\left\|P_{i}^{\prime}\right\|^{2}}
s=SpD=∑i=1n∥Pi′∥2∑i=1nQi′RPi′
3、根据旋转
R
R
R 和尺度
s
s
s 计算平移
t
。
t_{\text {。 }}
t。
t
=
Q
ˉ
−
s
R
P
ˉ
t=\bar{Q}-s R \bar{P}
t=Qˉ−sRPˉ
Sim3的逆变换矩阵
假设Sim3的变换矩阵为:
Sim
3
=
[
s
R
t
0
1
]
\operatorname{Sim} 3=\left[\begin{array}{cc} s R & t \\ 0 & 1 \end{array}\right]
Sim3=[sR0t1]
那么,它的逆变换矩阵为:
(
Sim
3
)
−
1
=
[
s
R
t
0
1
]
−
1
=
[
1
s
R
T
−
1
s
R
T
t
0
1
]
(\operatorname{Sim} 3)^{-1}=\left[\begin{array}{cc} s R & t \\ 0 & 1 \end{array}\right]^{-1}=\left[\begin{array}{cc} \frac{1}{s} R^{T} & -\frac{1}{s} R^{T} t \\ 0 & 1 \end{array}\right]
(Sim3)−1=[sR0t1]−1=[s1RT0−s1RTt1]
ps:
本文部分内容参考《计算机视觉life》的ORB-SLAM2课程