相机成像原理中各个坐标系的转化(数学推导)

1.前言

由于最近在学习关于相机标定的知识,所以了解大量相机知识,但通过大量资料学习后发现,网上很多对坐标系的转化过程无明确过程,使得数学基础不好的笔者十分苦恼,由此整理了一篇关于坐标系转化的文章,仅供参考。

2.正文

首先我们先明白为什么我们要进行坐标系得转换,是因为我们需要从世界坐标系得三维坐标转化到我们图像二维坐标中,从而获得我们所需要得参数。

2.1世界坐标系与相机坐标系

我们需要通过旋转和平移进行转化,从世界坐标系旋转到与相机坐标系相平行的位置,再进行平移,使得世界坐标系和相机坐标系重合,从而达到从世界坐标系到相机坐标系得目的。

由于涉及到坐标系的旋转,我们得先学习坐标系绕着不同的坐标轴旋转不同的角度,得到相应的矩阵。下图为x,y坐标绕着z轴转动θ角度后的图像
在这里插入图片描述
因为绕着z轴旋转,我们先看二维的xy坐标系
已知P(x,y),求x’和y’.
(1)求X’
由图知,我们要求x’,就相当于求OC(OC=OB+BC)
OB = OA cosθ 即OB = x cosθ
由于BC=AD,
AD = AP sinθ 即BC = y sinθ;
可以得到:OC = OB +BC 即x’ = x cosθ +y sinθ
(2)求y’
由图知,我们要求Y’,就相当于求OG(OG=OF-FG)、
OF = OE cosθ 即OF = y cosθ
由于FG = EH
EH = EP sinθ 即FG = x sinθ
可以得到:OG=OF-FG(x’ = -x sinθ+y cosθ)

{ x = x c o s θ + y s i n θ y = x s i n θ + y c o s θ \left\{ \begin{aligned} x' = x cosθ +y sinθ\\y' = -x sinθ+y cosθ \end{aligned} \right.

二维旋转矩阵为:
[ x y ] = [ c o s θ s i n θ s i n θ c o s θ ] [ x y ] = T [ x y ] \begin{gathered} \begin{bmatrix} x' \\ y' \end{bmatrix} =\begin{bmatrix} cosθ & sinθ \\ -sinθ & cosθ \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}=T\begin{bmatrix} x \\ y \end{bmatrix} \quad \end{gathered}
化为3维矩阵的形式:
[ x y z ] = [ c o s θ s i n θ 0 s i n θ c o s θ 0 0 0 1 ] [ x y z ] = T [ x y z ] \begin{gathered} \begin{bmatrix} x' \\ y' \\z' \end{bmatrix} =\begin{bmatrix} cosθ & sinθ & 0\\ -sinθ & cosθ &0\\0&0&1 \end{bmatrix} \begin{bmatrix} x \\ y \\z\end{bmatrix}=T\begin{bmatrix} x \\ y \\z\end{bmatrix} \quad \end{gathered}
化为4维矩阵的形式:
[ x y z 1 ] = [ c o s θ s i n θ 0 0 s i n θ c o s θ 0 0 0 0 1 0 0 0 0 1 ] [ x y z 1 ] = T [ x y z 1 ] \begin{gathered} \begin{bmatrix} x' \\ y' \\z' \\1\end{bmatrix} =\begin{bmatrix} cosθ & sinθ & 0&0\\ -sinθ & cosθ &0&0\\0&0&1&0\\0&0&0&1 \end{bmatrix} \begin{bmatrix} x \\ y \\z\\1\end{bmatrix}=T\begin{bmatrix} x \\ y \\z\\1\end{bmatrix} \quad \end{gathered}

如果我们是一直P(x’,y’,z’),求x,y,z呢?
在这里插入图片描述

已知P(x’,y’,z’),求x,y,z。
(1)求x
由图知我们要求X即要求OA(OA=OB-AB)
OB = OC cosθ 即OB = x’ cosθ
由于AB=CD,
CD = OF sinθ 即AB = y’ sinθ
所以 OA=OB-AB (x=x’ cosθ-y’ sinθ)

(2)求y
由图知我们要求X即要求OH(OH=OE+EH)
OE = OF cosθ 即OE = y’ cosθ
由于EH=FG
FG = FP sinθ 即EH = x’ sinθ
所以OH=OE+EH (y = x sinθ + y sinθ)
{ x = x c o s θ y s i n θ y = x s i n θ + y c o s θ \left\{ \begin{aligned} x = x' cosθ -y' sinθ\\ y'= x' sinθ+y' cosθ \end{aligned} \right.
二维旋转矩阵为:
[ x y ] = [ c o s θ s i n θ s i n θ c o s θ ] [ x y ] = R 1 [ x y ] \begin{gathered} \begin{bmatrix} x \\ y \end{bmatrix} =\begin{bmatrix} cosθ & -sinθ \\ sinθ & cosθ \end{bmatrix} \begin{bmatrix} x' \\ y' \end{bmatrix} =R1\begin{bmatrix} x' \\ y' \end{bmatrix}\quad \end{gathered}
化为3维矩阵的形式:
[ x y z ] = [ c o s θ s i n θ 0 s i n θ c o s θ 0 0 0 1 ] [ x y z ] = R 1 [ x y z ] \begin{gathered} \begin{bmatrix} x \\ y \\z \end{bmatrix} =\begin{bmatrix} cosθ & -sinθ & 0\\ sinθ & cosθ &0\\0&0&1 \end{bmatrix} \begin{bmatrix} x' \\ y' \\z'\end{bmatrix}=R1\begin{bmatrix} x' \\ y' \\z'\end{bmatrix} \quad \end{gathered}
化为4维矩阵的形式:
[ x y z 1 ] = [ c o s θ s i n θ 0 0 s i n θ c o s θ 0 0 0 0 1 0 0 0 0 1 ] [ x y z 1 ] = R 1 [ x y z 1 ] \begin{gathered} \begin{bmatrix} x \\ y \\z\\1\end{bmatrix} =\begin{bmatrix} cosθ & -sinθ & 0&0\\ sinθ & cosθ &0&0\\0&0&1&0\\0&0&0&1 \end{bmatrix} \begin{bmatrix} x' \\ y' \\z'\\1\end{bmatrix}=R1\begin{bmatrix} x' \\ y' \\z'\\1\end{bmatrix} \quad \end{gathered}

同理我们可以求得绕X轴和Y轴旋转ω,β的3阶矩阵
[ x y z ] = [ 1 0 0 0 c o s ω s i n ω 0 s i n ω c o s ω ] [ x y z ] = R 2 [ x y z ] \begin{gathered} \begin{bmatrix} x \\ y \\z \end{bmatrix} =\begin{bmatrix} 1 & 0 & 0\\ 0 & cosω &sinω\\0&-sinω&cosω \end{bmatrix} \begin{bmatrix} x' \\ y' \\z'\end{bmatrix}=R2\begin{bmatrix} x' \\ y' \\z'\end{bmatrix} \quad \end{gathered}

[ x y z ] = [ c o s β 0 s i n β 0 1 0 s i n β 0 c o s β ] [ x y z ] = R 3 [ x y z ] \begin{gathered} \begin{bmatrix} x \\ y \\z \end{bmatrix} =\begin{bmatrix} cosβ & 0 & -sinβ\\ 0 & 1 &0\\sinβ&0&cosβ \end{bmatrix} \begin{bmatrix} x' \\ y' \\z'\end{bmatrix}=R3\begin{bmatrix} x' \\ y' \\z'\end{bmatrix} \quad \end{gathered}
然后吧三个绕着不同方向得旋转进行相乘
可以得到旋转矩阵R=R1·R2·R3
在这里插入图片描述
这里先解释一下什么是相机坐标系。
在这里插入图片描述

相机坐标系其实也是一个三维直角坐标系,原点O是位于镜头光心处,X,Y周分别与成像面得两边平行,z轴是镜头得光轴,与像平面垂直。

*这里给大家增加一个知识点:
在坐标系变化过程中我们应该左乘还是右乘
(1)左乘:相对于固定坐标系进行转换
V’ = R * V = Rz * Ry * Rx V
例如:
固定坐标系=变换需要变换的坐标系
(x2,y2)=A * (x1,y1)
这里得“需要变换的矩阵”都是围绕着极坐标描述得,那也可以理解成我们生活中以大地为基坐标的坐标系XYZ。
(2)右乘 相对于自身坐标系进行变换(每一次都以变换后的自己为新的坐标系进行变换)
例如:
固定坐标系=需变换坐标系变换
(x2, y2) = (x1, y1) *A

我们在上面已经得到了旋转矩阵R,所以
[ x c y c z c ] = R [ x w y w z w ] + T = [ r 11 r 12 r 12 r 21 r 22 r 23 r 31 r 32 r 33 ] [ x w y w z w ] + [ t x t y t z ] \begin{gathered} \begin{bmatrix} xc \\ yc \\zc \end{bmatrix} =R\begin{bmatrix} xw\\ yw\\zw \end{bmatrix} +T=\begin{bmatrix} r11&r12 &r12 \\ r21&r22&r23 \\r31&r32&r33\end{bmatrix}\begin{bmatrix} xw\\ yw\\zw \end{bmatrix} +\begin{bmatrix} tx \\ ty \\tz\end{bmatrix} \quad \end{gathered}
其中T为偏移向量

R T = [ x c y c z c 1 ] = [ R T 0 1 ] [ x w y w z w 1 ] \begin{gathered} RT=\begin{bmatrix} xc \\ yc \\zc\\1 \end{bmatrix} =\begin{bmatrix} R&T \\0&1\end{bmatrix}\begin{bmatrix} xw\\ yw\\zw\\1 \end{bmatrix} \quad \end{gathered}
其实R为33矩阵,T是31矩阵,RT变成了4*4矩阵。而R,T记为相机外参(Camera Extrinsic)。

2.2 像素坐标系与图像坐标系

像素坐标系
像素坐标系uov是二位指教坐标系,反映了相机CCD/CMOS两种芯片中像素得排列情况。
原点o位于图像左上角,u轴,v轴分别与相面两边平行。
像素坐标系轴坐标得单位是像素。红色坐标系为像素坐标系u0v
在这里插入图片描述

图像坐标系
上图中蓝色区域为图像坐标系XOY,坐标轴的单位通常为毫米(mm),原点是相机光轴的相面的焦点(称为主点),即图像的中心点。
x轴,y轴分别u轴,v轴平行。故两个坐标系实际上平移关系,通过上图即可知,图像坐标系可平移到像素坐标系。

2.3图像坐标系转化为像素坐标系

[ u v 1 ] = [ 1 / d x 0 u 0 0 1 / d y v 0 0 0 1 ] [ X Y 1 ] \begin{gathered} \begin{bmatrix} u \\ v \\1 \end{bmatrix} =\begin{bmatrix} 1/dx&0&u0 \\0&1/dy&v0\\0&0&1\end{bmatrix}\begin{bmatrix} X\\ Y\\1 \end{bmatrix} \quad \end{gathered}
这其实可以化成一下形式更好理解

u = x / d x + u 0 v = y / d y + v 0 u=x/dx+u0\\ v=y/dy+v0 \\
其中,dx,dy分别为像素在X,Y轴方向上的物理尺度,u0,v0为主点(图像原点)坐标
个人理解:u等于 X除以一毫米中的有多少个像素(像素点的个数)+原点u0.

2.4三维相机坐标系与图像坐标系

说到三维相机坐标系就不得不提到小学二年级学到的小孔成像原理
我这里手画了一张小孔像二维原理原理图,如下:
在这里插入图片描述
上面这个图大家都有小学二年级的学历应该都看得懂,下面我们换成三维图:
在这里插入图片描述
空间任意一点P与其图像点p之间的关系,P与相机光心o的连线为oP,oP 与像面的交点p即为空
间点P在图像平面.上的投影。你可以想象着个过程类似于相似三角形
该过程为透视投影,如下矩阵表示:
s [ X Y Z ] = [ f 0 0 0 0 f 0 0 0 0 1 0 ] [ x y z 1 ] \begin{gathered} s\begin{bmatrix} X \\ Y \\Z \end{bmatrix} =\begin{bmatrix} f&0&0&0 \\0&f&0&0\\0&0&1&0\end{bmatrix}\begin{bmatrix} x\\ y\\z\\1 \end{bmatrix} \quad \end{gathered}
其中,s为比例因子(s不为0) ,f为有效焦距 (光心到图像平面的距离),(x, y, z, 1)T是空间点P在相机坐标系oxyz中的齐次坐标(X,Y, 1)T是像点p在图像坐标系OXY中的齐次坐标。

拆开为等式形式:
s X = f x s Y = f y sX=fx\\ sY=fy \\
可以在转化
X / x = f / s Y / y = f / s X/x=f/s\\ Y/y=f/s
这种关系就是刚开始我说的相似三角形关系(映射)

这里补充一个知识点:
齐次坐标:把在n维上表示的坐标,写在n+1维上的坐标来表示,从而从矩阵相加的表达式变成矩阵相乘

2.5.世界坐标系与像素坐标系

现在把上面步骤结合起来,这里列一下上面所求出的公式:

世界坐标系与相机坐标系 三维相机坐标系与图像坐标系 图像坐标系转化为像素坐标系
R T = [ x c y c z c 1 ] = [ R T 0 1 ] [ x w y w z w 1 ] s [ X Y Z ] = [ f 0 0 0 0 f 0 0 0 0 1 0 ] [ x y z 1 ] [ u v 1 ] = [ 1 / d x 0 u 0 0 1 / d y v 0 0 0 1 ] [ X Y 1 ] \begin{gathered} RT=\begin{bmatrix} xc \\ yc \\zc\\1 \end{bmatrix} =\begin{bmatrix} R&T \\0&1\end{bmatrix}\begin{bmatrix} xw\\ yw\\zw\\1 \end{bmatrix} s\begin{bmatrix} X \\ Y \\Z \end{bmatrix} =\begin{bmatrix} f&0&0&0 \\0&f&0&0\\0&0&1&0\end{bmatrix}\begin{bmatrix} x\\ y\\z\\1 \end{bmatrix} \begin{bmatrix} u \\ v \\1 \end{bmatrix} =\begin{bmatrix} 1/dx&0&u0 \\0&1/dy&v0\\0&0&1\end{bmatrix}\begin{bmatrix} X\\ Y\\1 \end{bmatrix} \quad \end{gathered}
接下来就可以求从世界坐标转换为像素坐标系:
s [ u v 1 ] = [ 1 / d x 0 u 0 0 1 / d y v 0 0 0 1 ] [ f 0 0 0 0 f 0 0 0 0 1 0 ] [ R T 0 1 ] [ x w y w z w 1 ] = [ f / d x 0 v 0 0 0 f / d y v 0 0 0 0 1 0 ] [ R T 0 1 ] [ x w y w z w 1 ] \begin{gathered} s\begin{bmatrix} u \\ v \\1 \end{bmatrix} =\begin{bmatrix} 1/dx&0&u0 \\0&1/dy&v0\\0&0&1\end{bmatrix} \begin{bmatrix} f&0&0&0 \\0&f&0&0\\0&0&1&0\end{bmatrix} \begin{bmatrix} R&T \\0&1\end{bmatrix} \begin{bmatrix} xw\\ yw\\zw\\1\end{bmatrix} =\begin{bmatrix} f/dx&0&v0&0 \\0&f/dy&v0&0\\0&0&1&0\end{bmatrix} \begin{bmatrix} R&T \\0&1\end{bmatrix} \begin{bmatrix} xw\\ yw\\zw\\1\end{bmatrix} \quad \end{gathered}
现在我们假设
a x = f / d x a y = f / d y M 1 = [ f / d x 0 v 0 0 0 f / d y v 0 0 0 0 1 0 ] M 2 = [ R T 0 1 ] X w = [ x w y w z w 1 ] ax=f/dx\\ ay=f/dy\\ \begin{gathered} M1=\begin{bmatrix} f/dx&0&v0&0 \\0&f/dy&v0&0\\0&0&1&0\end{bmatrix}\\ M2=\begin{bmatrix} R&T \\0&1\end{bmatrix}\\ Xw=\begin{bmatrix} xw\\ yw\\zw\\1\end{bmatrix} \quad \end{gathered}
则继续化简为:
s [ u v 1 ] = [ a x 0 v 0 0 0 a y v 0 0 0 0 1 0 ] [ R T 0 1 ] [ x w y w z w 1 ] = M 1 M 2 X w = M X w \begin{gathered} s\begin{bmatrix} u \\ v \\1 \end{bmatrix} =\begin{bmatrix} ax&0&v0&0 \\0&ay&v0&0\\0&0&1&0\end{bmatrix} \begin{bmatrix} R&T \\0&1\end{bmatrix} \begin{bmatrix} xw\\ yw\\zw\\1\end{bmatrix}=M1*M2*Xw=MXw \quad \end{gathered}
其中ax,ay称为u,v轴的尺度因子,M1称为相机的内部参数矩阵,M2称为相机的外部参数矩阵,M称为投影矩阵。

3.总结

这部分内容大多数都是关于线性代数课程的知识,重点就是一定要自己动手算过才算真正明白,此部分笔者觉得的难点就是对各个坐标轴认识和旋转矩阵这部分。

猜你喜欢

转载自blog.csdn.net/qq_44823010/article/details/107541221
今日推荐