一、背景介绍
在工业定位场景中,常常存在多个无交叠视域的摄像头,对某一个平面下的物体进行定位测量。如何求解这些无交叠视野的摄像机的图像相对位置关系对于目标定位十分重要。这里借助ChAuco标定板建立世界坐标系,进而求解多个相机相对主相机的转换关系。
在实际场景中很难通过两个相机的世界坐标到像素坐标的投影矩阵
H
1
H_1
H1,
H
2
H_2
H2。一个简单的方法是,在求得
H
1
H_1
H1 和
H
2
H_2
H2 后可以构造一批世界坐标点
P
w
P_w
Pw 分别将其投影到像素平面A , 和像素平面B 。 得到
P
a
′
P'_a
Pa′ 和
P
b
′
P'_b
Pb′ 再求
P
a
′
P'_a
Pa′ 和
P
b
′
P'_b
Pb′ 的投影矩阵
H
b
a
H_{ba}
Hba。 这样就可以简单的求出二者的转换关系。
一、原理
1. 世界坐标和像素坐标的转换
由于在工业场景中,待测量的目标常常处于同一个平面,在建立世界坐标系时,认为ChAuco标定板的Z轴坐标为1。则可以得到 图像坐标系的点:
P
i
=
[
u
,
v
,
z
i
]
⊤
P_{\text{i}} = [u, v, z_i]^{\top}
Pi=[u,v,zi]⊤ 和 世界坐标系的点:
P
w
=
[
x
,
y
,
1
]
⊤
P_{\text{w}}=[x, y, 1]^{\top}
Pw=[x,y,1]⊤的转换关系如下:
s
i
∗
[
x
′
y
′
1
]
=
H
iw
P
w
(1.1)
s_i * \begin{bmatrix}x'\\y' \\ 1 \end{bmatrix} = H_{\text{iw}}P_{\text{w}} \tag{1.1}
si∗
x′y′1
=HiwPw(1.1)
像素坐标到世界坐标系下的转换:
P
w
=
s
i
∗
H
i
w
−
1
P
i
(1.2)
P_w = s_i * H_{iw}^{-1}P_{\text{i}} \tag{1.2}
Pw=si∗Hiw−1Pi(1.2)
此处的
H
iw
H_{\text{iw}}
Hiw 可以使用OpenCV的 Cv2.FindHomography API 求出。
2. FindHomography
寻找两个平面的单应矩阵
OpenCV 4.6.0 FindHomography官方文档
Mat cv::findHomography ( InputArray srcPoints,
InputArray dstPoints,
int method = 0,
double ransacReprojThreshold = 3,
OutputArray mask = noArray(),
const int maxIters = 2000,
const double confidence = 0.995
)
具体参数,参照官方文档。这个函数返回一个源平面和目标平面的投影变换矩阵
H
H
H:
s
i
[
x
i
′
y
i
′
1
]
∼
H
[
x
i
y
i
1
]
(2.1)
s_i \begin{bmatrix} x'_i \\ y'_i \\1\ \end{bmatrix} \sim H \begin{bmatrix} x_i \\ y_i \\1\ \end{bmatrix} \tag{2.1}
si
xi′yi′1
∼H
xiyi1
(2.1)
优化过程为最小化反向投影误差,反向投影误差为:
∑
i
[
(
x
i
′
−
h
11
x
i
+
h
12
y
i
+
h
13
h
31
x
i
+
h
32
y
i
+
h
33
)
2
+
(
y
i
′
−
h
21
x
i
+
h
22
y
i
+
h
23
h
31
x
i
+
h
32
y
i
+
h
33
)
2
]
(2.2)
\sum_i[(x'_i - \frac{h_{11}x_i + h_{12}y_i+ h_{13}}{h_{31}x_i + h_{32}y_i+ h_{33}})^2+(y'_i - \frac{h_{21}x_i + h_{22}y_i+ h_{23}}{h_{31}x_i + h_{32}y_i+ h_{33}})^2] \tag{2.2}
i∑[(xi′−h31xi+h32yi+h33h11xi+h12yi+h13)2+(yi′−h31xi+h32yi+h33h21xi+h22yi+h23)2](2.2)
3. Z_i 的求解
z
i
z_i
zi 是从世界坐标投影到像素坐标的Z 坐标,在图像中,需要将Z坐标归一化得到像素坐标。
观察矩阵的计算,设
H
iw
=
[
h
11
h
12
h
13
h
21
h
22
h
23
h
31
h
32
h
33
]
(3.1)
H_{\text{iw}} = \begin{bmatrix} h_{11} & h_{12}& h_{13} \\ h_{21} & h_{22}& h_{23} \\ h_{31} & h_{32}& h_{33} \\ \end{bmatrix} \tag{3.1}
Hiw=
h11h21h31h12h22h32h13h23h33
(3.1)
则有:
s
i
[
x
i
′
y
i
′
1
]
=
[
h
11
h
12
h
13
h
21
h
22
h
23
h
31
h
32
h
33
]
[
x
y
1
]
s_i \begin{bmatrix} x'_i \\ y'_i \\1\ \end{bmatrix} = \begin{bmatrix} h_{11} & h_{12}& h_{13} \\ h_{21} & h_{22}& h_{23} \\ h_{31} & h_{32}& h_{33} \\ \end{bmatrix} \begin{bmatrix} x\\ y\\1 \end{bmatrix}
si
xi′yi′1
=
h11h21h31h12h22h32h13h23h33
xy1
可以得到:
s
i
=
h
31
∗
x
+
h
32
∗
y
+
h
33
(3.2)
s_i = h_{31} * x + h_{32} * y + h_{33} \tag{3.2}
si=h31∗x+h32∗y+h33(3.2)
4. 多相机像素坐标相对关系
4.1 像素坐标相对关系
设有相机A、B,求解相机B的像素坐标到相机A的转换关系。则对于世界坐标系下的点
P
w
P_w
Pw在相机A、B下的的像素坐标分别为:
s
i
a
∗
P
i
a
=
H
iwa
∗
P
w
z
i
b
∗
P
i
b
=
H
iwb
∗
P
w
(3.1)
\begin{align*} s_{ia}*P_{ia} =& H_{\text{iwa}} * P_w \\ z_{ib}*P_{ib} =& H_{\text{iwb}} * P_w \end{align*} \tag{3.1}
sia∗Pia=zib∗Pib=Hiwa∗PwHiwb∗Pw(3.1)
则
P
i
a
P_{ia}
Pia 和
P
i
b
P_{ib}
Pib 的转换关系为:
P
i
a
=
s
i
b
s
i
a
H
iwa
H
iwb
−
1
P
i
b
(4.2)
P_{ia} = \frac{s_{ib}}{s_{ia} }H_{\text{iwa}}H_{\text{iwb}}^{-1}P_{ib} \tag{4.2}
Pia=siasibHiwaHiwb−1Pib(4.2)
由(3.2) 可以计算尺度系数
s
i
s_i
si ,但是这个方式是从世界坐标到图像坐标,我们在此处更希望利用图像坐标得到
s
i
s_i
si。我们这里有:
P
′
=
1
s
i
H
P
w
1
s
i
P
w
=
H
−
1
P
′
(4.3)
\begin{align*} P' =& \frac{1}{s_i} H P_w \\ \frac{1}{s_i}P_w =& H^{-1}P' \end{align*} \tag{4.3}
P′=si1Pw=si1HPwH−1P′(4.3)
同理我们可以得到
1
s
i
\frac{1}{s_i}
si1:
1
s
i
=
h
31
−
1
∗
x
′
+
h
32
−
1
∗
y
′
+
h
33
−
1
s
i
=
1
h
31
−
1
∗
x
′
+
h
32
−
1
∗
y
′
+
h
33
−
1
(4.4)
\begin{align*} \frac{1}{s_i} =& h^{-1}_{31} * x' + h^{-1}_{32} * y' + h^{-1}_{33} \\ s_i =& \frac{1}{h^{-1}_{31} * x' + h^{-1}_{32} * y' + h^{-1}_{33}} \end{align*} \tag{4.4}
si1=si=h31−1∗x′+h32−1∗y′+h33−1h31−1∗x′+h32−1∗y′+h33−11(4.4)
有了以上的内容,我们就可以得出相机坐标
b
b
b到
a
a
a的投影变换矩阵
H
a
b
H_{ab}
Hab了:
H
a
b
=
H
a
H
b
−
1
H_{ab} = H_aH_b^{-1}
Hab=HaHb−1
这种方式仍然难以将两个像素坐标系的转换关系统一到同一个矩阵。
4.2 直接计算像素平面的转换关系
在4.1 中可以看出,我们很难通过两个投影矩阵 H 1 H_1 H1, H 2 H_2 H2,直接得到两个像素平面的转换关系,究其原因还是在尺度因子难以归结到变换矩阵中。一个更简单的方法是,在求得 H 1 H_1 H1 和 H 2 H_2 H2 后可以构造一批世界坐标点 P w P_w Pw 分别将其投影到像素平面A , 和像素平面B 。 得到 P a ′ P'_a Pa′ 和 P b ′ P'_b Pb′ 再求 P a ′ P'_a Pa′ 和 P b ′ P'_b Pb′ 的投影矩阵 H b a H_{ba} Hba。 这样就可以简单的求出二者的转换关系。
二、 实验结果