视觉SLAM十四讲 第5讲 相机与图像
三维世界中的一个物体反射或发出的光线,穿过相机光心后,投影在相机的成像平面上。相机的感光器件接收到光线后,产生测量值,就得到了像素,形成了我们见到的照片。
相机将三维世界中的坐标点( 单位为米)映射到二维图像平面( 单位为像素)的过程能够用一个几何模型进行描述。我们最常用的就是针孔模型。由于透镜的存在(径向畸变)和制作工艺问题(切向畸变),会导致光线投影到成像平面的过程中产生畸变。
1. 关于针孔相机模型
回忆初中做的蜡烛投影实验即可。记住以下关键点:
- 对于相机坐标系,习惯上z轴指向相机正前方,x轴指向右方,y轴指向下方,xyz成右手法则
- 成像平面是相机中用于成像的感光区域,图像是倒着的,而归一化成像平面在相机外侧一米处,成像是正常的
- 相机坐标系转到成像平面坐标系,通过相似三角形的性质和焦距 f f f来计算,即 X ′ = f X Z X^{'}= f\frac{X}{Z} X′=fZX
- 成像平面坐标系转到像素坐标系,两者相差一个缩放和一个原点的平移
{ u = f x X Z + c x v = f y Y Z + c y = > { u = α f X Z + c x v = β f Y Z + c y (1.1) \begin{cases} u = f_x\frac{X}{Z}+c_x \\ v = f_y\frac{Y}{Z}+c_y \end{cases} => \begin{cases} u = \alpha f\frac{X}{Z}+c_x \\ v = \beta f\frac{Y}{Z}+c_y \end{cases} \tag{1.1} {u=fxZX+cxv=fyZY+cy=>{u=αfZX+cxv=βfZY+cy(1.1)
所以fx和fy的物理意义是缩放,cx和cy的物理意义是平移,fx,fy,cx,cy组成了相机的内参(camera intrinsic)矩阵K,有时需要自己确定相机的内参,也就是所谓的标定。标定算法业已成熟(如著名的单目棋盘格张正友标定法) - 一个世界坐标点转到相机坐标系再转到像素坐标系(图像坐标系),涉及到相机的外参(相机的位姿R,t)、相机的内参,完整的转化过程如下公式:
Z c P u v = Z [ u v 1 ] = K P c = [ f x 0 c x 0 f y c y 0 0 1 ] P c = K ( R P w + t ) = K T P w (1.2) Z_c P_{uv} = Z \begin{bmatrix} u\\v\\1 \end{bmatrix} = KP_c = \begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{bmatrix} P_c = K (RP_w + t) = KTP_w \tag{1.2} ZcPuv=Z⎣⎡uv1⎦⎤=KPc=⎣⎡fx000fy0cxcy1⎦⎤Pc=K(RPw+t)=KTPw(1.2)
其中K为内参,Pc为点的相机坐标系值,Pw为点的世界坐标系值,在等式最右边K是三阶的,而T是四阶的,所以等式最右边先进行齐次坐标系运算,再去掉一阶进行三阶运算。如果Zc为1,则等式为点P到归一化平面上的投影,然后再转图像坐标系。
2. 关于图像畸变
2.1 什么是畸变
畸变就是光线穿过透镜投影到成像平面时的位置发生变化。
常见的畸变有两种:
径向畸变
:由透镜形状引起的畸变,相关畸变参数: k 1 , k 2 , k 3 k_1,k_2,k_3 k1,k2,k3。径向畸变又分为桶形畸变和枕形畸变桶形畸变
:图像放大率随着与光轴之间的距离增加而减小,所以有图像中间突出来的感觉枕形畸变
:与桶形畸变相反,图像放大率随着与光轴之间的距离增加而增加,所以有图像中间凹进去,四周突出的感觉
切向畸变
:由于相机制作工艺,透镜和成像平面没有完全平行而造成的畸变,相关畸变参数: p 1 , p 2 p_1,p_2 p1,p2
2.2 畸变参数
必须弄清楚以下几点:
- 不正常的图像(原始图像)称为畸变图像(distorted image)
- 正常图像(去畸变后的图像)称为去畸变图像(undistorted image)
- 畸变参数的作用是:找到去畸变图像中像素点位置和与其对应的畸变图像中像素点位置的映射关系。一定要注意顺序,是去畸变图像映射到畸变图像,看以下公式:
首先,找到去畸变图像坐标点 ( u u n d i s t o r t e d , v u n d i s t o r t e d ) (u_{undistorted},v_{undistorted}) (uundistorted,vundistorted)在成像平面上的位置 ( x u n d i s t o r t e d , y u n d i s t o r t e d ) (x_{undistorted},y_{undistorted}) (xundistorted,yundistorted):
[ x u n d i s t o r t e d y u n d i s t o r t e d 1 ] = [ 1 f x 1 f y 1 ] [ u u n d i s t o r t e d − c x v u n d i s t o r t e d − c y 1 ] (2.1) \begin{bmatrix} x_{undistorted}\\y_{undistorted}\\1 \end{bmatrix} = \begin{bmatrix} \frac{1}{f_x} & \frac{1}{f_y} &1 \end{bmatrix} \begin{bmatrix} u_{undistorted}-c_x\\v_{undistorted}-c_y\\1 \end{bmatrix} \tag{2.1} ⎣⎡xundistortedyundistorted1⎦⎤=[fx1fy11]⎣⎡uundistorted−cxvundistorted−cy1⎦⎤(2.1)
然后计算该成像平面点对应的畸变点的位置:
{ x d i s t o r t e d = x u n d i s t o r t e d ( 1 + k 1 r 2 + k 2 r 4 + k 3 r 6 ) + 2 p 1 x y + p 2 ( r 2 + 2 x 2 ) y d i s t o r t e d = y u n d i s t o r t e d ( 1 + k 1 r 2 + k 2 r 4 + k 3 r 6 ) + p 1 ( r 2 + 2 x 2 ) + 2 p 2 x y (2.2) \begin{cases} x_{distorted}= x_{undistorted}(1+k_1 r^2 + k_2 r^4 + k_3 r^6) + 2p_1xy + p_2(r^2+2x^2) \\ y_{distorted}= y_{undistorted}(1+k_1 r^2 + k_2 r^4 + k_3 r^6) + p_1(r^2+2x^2) + 2p_2xy \end{cases} \tag{2.2} {xdistorted=xundistorted(1+k1r2+k2r4+k3r6)+2p1xy+p2(r2+2x2)ydistorted=yundistorted(1+k1r2+k2r4+k3r6)+p1(r2+2x2)+2p2xy(2.2)
得到畸变点的位置后,再用内参计算出其在畸变图像上的位置:
{ u d i s t o r t e d = f x x d i s t o r t e d + c x v d i s t o r t e d = f y y d i s t o r t e d + c y (2.3) \begin{cases} u_{distorted} = f_x x_{distorted} + c_x \\ v_{distorted} = f_y y_{distorted} + c_y \end{cases} \tag{2.3} {udistorted=fxxdistorted+cxvdistorted=fyydistorted+cy(2.3)
通过以上操作,就可以取出畸变图像中 ( u d i s t o r t e d , v d i s t o r t e d ) (u_{distorted},v_{distorted}) (udistorted,vdistorted)中的像素值,赋值到去畸变图像中的 ( u , v ) 上 (u,v)上 (u,v)上
2.3 关于双目相机模型
双目相机模型记住以下关键点:
原理
:通过同步采集左右相机的图像,计算图像间的视差(),再通过几何模型计算、估计处该点的深度基线b
:是双目相机光心之间的距离。基线越长,双目能测到的最大距离越远。反之,小型双目相机只能测量很近的距离。