跳转到内容

C201 第 1 课:标定估计的是什么

标定把一台只会拍照的相机变成一台能测量的相机。对一套双目采集设备,它包含两类数字:每台相机怎样把一个方向变成一个像素,以及每台相机相对于另一台位于哪里。这一课给每个部分起名,并说明对当前头戴采集设备上的鱼眼镜头来说哪些选择是要紧的。完整的讲解在相机标定。

这些术语也都在术语表的标定部分里。

术语 含义
内参(Intrinsics) 把相机坐标系中的一个方向映射到一个像素的相机模型及其参数(焦距、主点、畸变)
焦距(Focal length) 以像素为单位,每个图像轴一个值;它决定每个像素覆盖多大的一块世界
主点(Principal point) 光轴与传感器相交的位置,以像素为单位;靠近图像中心,但很少正好在中心
投影模型(Projection model) 把一条光线与光轴的夹角映射为到主点的距离的公式
畸变(Distortion) 真实镜头偏离理想投影的方式;它移动像点,但不使它们变模糊
外参(Extrinsics) 把点从一台相机的坐标系变换到另一台相机坐标系的旋转 R 和平移 t
基线(Baseline) 两台相机光心之间的距离,即 t 的长度
重投影误差(Reprojection error) 检测到的标定板角点与标定后的模型预测的位置之间的距离,以像素为单位

出发点是针孔模型,它把相机看作所有光线都通过的一个点(相机标定)。

The pinhole model behind the intrinsics Side view of the pinhole camera model. A ray from a 3D point in the world passes through the camera centre and lands on the image plane at the image point. The optical axis meets the image plane at the principal point. The distance from the camera centre to the image plane is the focal length, measured in pixels. 3D point in the world image point (u, v) principal point (cx, cy) focal length f, in pixels camera centre image plane (the sensor) optical axis
The pinhole model. A ray from a point in the world passes through the camera centre and lands on the sensor. Focal length and principal point, in pixels, are the core intrinsics.

针孔内参是四个数:两个焦距和主点的两个坐标。Kalibr 把它们写成 [fu, fv, pu, pv](Kalibr 文件参考)。在像素为正方形的传感器上,fu 和 fv 应当非常接近。

由内参的含义可以得出两条规则:

  • 它们属于一种图像尺寸。Kalibr 文件参考说得很直白:内参只在拟合时所用的 resolution 下有效。换一种分辨率、裁切或像素合并(binning)模式,就需要重新标定(resolution)。
  • 它们属于一种镜头状态。自动对焦、光学防抖和电子防抖都会逐帧改变这个映射,所以用于测量的相机要把它们锁住,或者干脆不用。如果标定之后有人转动了对焦环,这次标定就作废了(相机标定)。原因在光学上:针孔模型的“焦距”其实是镜头到传感器的距离,对焦到更近处会把镜头向外移,所以以像素为单位的焦距变大(对焦改变内参)。

投影与畸变:为什么鱼眼镜头需要自己的模型

Section titled “投影与畸变:为什么鱼眼镜头需要自己的模型”

真实镜头会把直线弄弯,在图像边缘最严重。畸变系数描述这种弯曲,这样就可以把它校正掉。

Lens distortion: an ideal grid and a wide-angle lens Left: a square grid as an ideal pinhole camera images it, every line straight. Right: the same grid through a wide-angle lens with barrel distortion. Lines bow outward and the effect grows toward the edges of the image, which calibration measures and corrects. Ideal pinholeWide-angle lensstraight lines stay straightlines bow, most at the edges
Real lenses bend straight lines, most at the edges of the image. The distortion coefficients describe that bending so it can be corrected.

广角镜头是有意把线弄弯的。每只镜头都把与光轴成 θ 角的光线映射到传感器上的一个半径 r,而镜头是按少数几种投影函数之一设计的。透视(针孔)投影 r = f · tan(θ) 在 90° 处趋于无穷大,所以没有哪只直线投影镜头能达到 180° 的视角;鱼眼镜头遵循别的投影,例如等距投影 r = f · θ(镜头投影与畸变)。

Projection functions: image radius against ray angle A plot of image radius r, in units of the focal length f, against the angle theta between an incoming ray and the optical axis, from 0 to 100 degrees. The perspective curve r = f tan theta climbs steeply and goes to infinity at 90 degrees. The fisheye curves stay finite: stereographic r = 2f tan(theta/2) reaches 2f at 90 degrees, equidistant r = f theta is a straight line reaching about 1.57f, equisolid r = 2f sin(theta/2) reaches about 1.41f, and orthographic r = f sin theta flattens to f at 90 degrees. All five agree near the axis. 0°20°40°60°80°100°0123 angle of the ray off the optical axis, θ image radius r, in units of f perspectivestereographicequidistantequisolidorthographic
How far from the image centre a ray lands (r, in units of f) for a ray arriving θ off the axis. The perspective (pinhole) curve r = f·tan θ runs to infinity at 90°, so no rectilinear lens can reach a 180° view. The fisheye projections stay finite. All five agree near the axis, which is why a narrow lens looks like a pinhole.

这就是经典的“针孔加多项式”模型在鱼眼镜头上不够用的原因。Double Sphere 论文(Usenko、Demmel 和 Cremers,2018)发现 “in practice, even when distortion model is added the pinhole camera demonstrates suboptimal performance for a field-of-view greater than 120°”(意为:在实践中,即使加上畸变模型,针孔相机在视场角大于 120° 时的表现也不理想)。为广角镜头建立的模型,以及它们在本课程所用工具中的名字:

模型 它的假设 Kalibr OpenCV mrcal
针孔 + 径向–切向 接近直线投影的镜头 pinhole-radtan calibrateCamera LENSMODEL_OPENCV4/5/8/12
Kannala–Brandt(等距) 图像半径是 θ 的奇次多项式 pinhole-equi cv::fisheye 文档未提及
扩展统一模型(EUCM) 通过一个椭球投影;有闭式逆 eucm-none 文档未提及 文档未提及
Double Sphere(双球)模型 通过两个单位球投影;有闭式逆 ds-none 文档未提及 文档未提及
样条 拟合到镜头上的一个灵活曲面 无 无 LENSMODEL_SPLINED_STEREOGRAPHIC

这张表取自标定工具对比;公式在镜头投影与畸变。

为鱼眼双目采集设备做选择时,有三条结论可以参照:

  • Kannala–Brandt 在 180° 附近有极限。Kalibr 的 pinhole-equi 和 OpenCV 的 fisheye 模块是在针孔投影之后再应用它的,所以它在偏离光轴 90° 处有一个奇异点,“which makes it unsuitable for fisheye lenses with field-of-view close to 180°”(意为:这使它不适合视场角接近 180° 的鱼眼镜头)(Double Sphere 论文)。
  • Double Sphere 和 EUCM 拟合得几乎一样好,而且更快。在 122° 到 195° 的镜头上,Double Sphere 模型的 “less than 1% greater reprojection error compared to KB 8 on all sequences”(意为:在所有序列上,重投影误差比 KB 8 大不到 1%),而它的投影计算大约快 5.6 倍(标定双目或多相机采集设备)。
  • 使用你的处理流程要用的模型。一个期望 Kannala–Brandt 参数的 SLAM 系统或数据集格式,从一个它读不了的模型的更好拟合中得不到任何好处(镜头投影与畸变)。

还有一项检查适用于任何模型:真实的径向畸变是单调的。OpenCV 的文档把非单调的拟合结果当作 “a calibration failure”(意为:一次标定失败),出现这种情况的一种原因是标定板从未到达图像的四角(镜头投影与畸变)。

外参是两台相机坐标系之间的旋转 R 和平移 t。在刚性的头戴设备上,它们由机械结构固定;腕戴相机相对于头部运动,改为逐帧跟踪(相机标定)。

Extrinsics: where each sensor sits relative to the others A head seen from above with a stereo camera pair at the front and an IMU behind them. Each sensor has its own coordinate frame. The rotation and translation from the left camera to the right camera, and from the camera to the IMU, are fixed by the rig and are calibrated once. A wrist camera's position relative to the head changes every frame, so it is tracked, not calibrated. Every sensor also needs its time offset. left to right: R, t Fixed by the rig: calibrate once camera to IMU: R, t IMU wrist camera Moves every frame: tracked, not calibrated plus, for every sensor: its time offset
Extrinsics are the rotation R and translation t between sensors. On a rigid head unit they are fixed and calibrated once; a wrist camera moves relative to the head, so its pose is tracked frame by frame instead.

对一个双目对,外参决定深度。双目深度是 z = f·b / d,所以基线错 1%,每个深度都错 1%(标定双目或多相机采集设备)。

变换的方向是最经典的错误,而且没有任何东西会提醒你:

  • Kalibr 把每个变换写成 T_a_b,它接收坐标系 b 中的一个点,返回它在坐标系 a 中的坐标。对 cam1,T_cn_cnm1 就是 T_c1_c0,用 Kalibr wiki 的话说,它 “takes cam0 to cam1 coordinates”(意为:把 cam0 坐标变换为 cam1 坐标)。它的平移以米为单位,长度就是基线(Kalibr 如何命名变换)。
  • OpenCV 的 stereoCalibrate 返回 R 和 T,其中 “this matrix brings points given in the first camera’s coordinate system to points in the second camera’s coordinate system”(意为:这个矩阵把第一台相机坐标系中给出的点变换为第二台相机坐标系中的点)(OpenCV calib3d 参考文档)。
The transforms in a Kalibr camchain-imucam.yaml Three coordinate frames: the IMU, cam0 and cam1. An arrow from the IMU to cam0 is labelled T_cam_imu stored under cam0, meaning it takes a point in IMU coordinates to cam0 coordinates. An arrow from cam0 to cam1 is labelled T_cn_cnm1 stored under cam1, which equals T_c1_c0 and takes a point in cam0 coordinates to cam1 coordinates. A third arrow from the IMU to cam1 is labelled T_cam_imu stored under cam1. The rule printed below: T_a_b takes a point expressed in frame b and returns it expressed in frame a. IMU (imu0) cam0 cam1 cam1: T_cn_cnm1 = T_c1_c0 cam0 coordinates to cam1 cam0: T_cam_imu cam1: T_cam_imu T_a_b takes a point in frame b and returns it in frame a
How Kalibr names its transforms. T_a_b takes a point given in frame b and returns it in frame a. Each camera block in camchain-imucam.yaml stores T_cam_imu (IMU to that camera), and every camera after cam0 stores T_cn_cnm1 (the previous camera to this one, so cam1 holds T_c1_c0).

T_a_b 的平移量是从坐标系 a 看到的坐标系 b 的原点。把它的长度与尺子的测量值比较,是确认你没有把方向读反的最快检查。

标定求解器让相机看一块几何已知的标定板,按当前的参数预测每个角点应当落在哪里,然后调整每个参数,直到预测的角点尽可能靠近检测到的角点。这个差距就是重投影误差,每个工具都报告这个数字(相机标定)。第 2 到第 4 课讲它需要的视图、求解它的工具,以及为什么单凭低误差说明不了多少问题。

  1. 你的团队把双目相机从全分辨率切换到 2 × 2 像素合并模式。他们还能继续用原来的标定吗?
  2. 你的镜头视场角接近 180°。实用指南指向哪些 Kalibr 模型,又提醒不要用哪一个?
  3. 在 Kalibr 的 camchain 中,cam1 下面的 T_cn_cnm1 从哪里映射到哪里?它的平移量的长度是什么?
  4. 既然 Double Sphere 拟合得一样好、投影还更快,一个团队为什么仍可能选 Kannala–Brandt?
  1. 不能。内参只在拟合时所用的分辨率下有效;新的像素合并模式需要重新标定。
  2. 定义在 3D 光线上的模型:Double Sphere(ds-none)、EUCM(eucm-none)或带 radtan 的统一模型(omni-radtan)。Kalibr 的 pinhole-equi 在偏离光轴 90° 处有一个奇异点,使它不适合 180° 附近。
  3. 它把 cam0 坐标映射为 cam1 坐标(T_c1_c0)。它的平移量的长度是基线,以米为单位。
  4. 因为处理流程的其余部分读的是 Kannala–Brandt。一个 SLAM 系统或数据集格式用不了的模型,拟合得再好也没有用。