跳转到内容

相机标定:内参、外参,以及如何把它自动化

没有标定过的相机只是拍照。标定过的相机可以测量。标定是一组数字,它准确说明每个像素对应世界中的哪个方向,以及每个传感器相对于其他传感器的位置。

第一人称数据处理流程所做的几乎每件事都依赖它。双目深度、手的 3D 位置、在房间里跟踪头部、把相机与它的 IMU(惯性测量单元)对齐,都假定标定是正确的。标定错了,什么都不会崩溃。数字只是悄悄地变错,这更糟。

标定分两半:内参和外参。然后是每个要造不止几台相机的团队都必须回答的问题:如何在每个工位都没有专家的情况下,可靠地把所有相机都标定好。

内参描述相机本身:世界中的一个方向如何变成传感器上的一个像素。出发点是针孔模型,它把相机看作所有光线都穿过的一个点。

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.
  • 焦距,以像素为单位。它决定每个像素覆盖多少世界。有两个数,每个轴一个,因为像素并不总是严格的正方形。
  • 主点。光轴与传感器相交的位置。它很少正好是图像的中心,因为镜头和传感器的装配从来不是完美的。
  • 畸变。真实镜头会把直线弄弯。OpenCV 标定教程描述了两种经典类型:径向畸变,它 “causes straight lines to appear curved”(意为:使直线看起来是弯的),并且越靠近图像边缘越大;以及切向畸变,来自与传感器不完全平行的镜头。
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.

第一人称相机需要广角和鱼眼镜头来把双手留在画面内,而这些镜头弯曲得太厉害,经典的畸变模型不再适用。它们需要专门为它们建立的模型。最有名的来自 Kannala 和 Brandt(2006),是一个适用于 “conventional, wide-angle, and fish-eye lenses”(意为:常规镜头、广角镜头和鱼眼镜头)的通用模型。实际工作中会遇到它:Build AI 的数据集卡片写明,它的头戴相机是用这个鱼眼模型标定的,有四个畸变系数。Meta 的 Aria Gen 2 标定文档为它的眼动追踪相机列出了 Kannala–Brandt 模型,为它的 RGB 相机和 SLAM 相机列出了带有额外切向项和薄棱镜项的鱼眼模型(SLAM:同时定位与建图,即推算设备所在位置的跟踪)。

由此得出一条规则:内参属于处在某一个对焦设置下的一只镜头。自动对焦、光学防抖和电子防抖都会让映射逐帧变化,所以用于测量的相机要么把它们锁定,要么不用它们。如果有人在标定之后转动了对焦环,标定就失效了。

外参描述一个传感器相对于另一个东西的位置:一个旋转和一个平移,通常写作 R 和 t。在采集设备上,重要的外参是:

  • 相机到相机。双目对的左相机和右相机。深度直接由它得出,所以它必须精确。
  • 相机到 IMU。IMU 在它自己的坐标系里测量运动。在它的读数能帮助跟踪相机之前,软件必须知道两者相对于彼此是怎样安装的。
  • 相机到世界或机器人。有时需要它来把采集设备与固定的装置、采集房间或机器人联系起来。
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.

在刚性的头戴单元上,这些外参由机械结构固定,并在工厂标定(之后还会细化,见下文)。腕戴相机则不同:佩戴者每次移动手臂,它相对于头部的位置都会变化。这根本不是一项标定;它必须逐帧跟踪。

还有第三个容易忘记的量:时间。每个传感器的时间戳相对于其他传感器都有少许偏移,而这个偏移必须和位置一样精确地知道。Kalibr 等工具把它当作标定的一部分:它的相机–IMU 标定在估计旋转和平移的同时估计时间偏移,依据的是 Furgale、Rehder 和 Siegwart(2013)的 “Unified temporal and spatial calibration for multi-sensor systems”(意为:多传感器系统的统一时间与空间标定)。让各时钟保持一致,在本系列中另有专文。

标准方法是 Zhang(2000)的方法。给相机看一个几何尺寸精确已知的平面图案,例如棋盘格,摆成若干个朝向。Zhang 的论文只需要 “a few (at least two) different orientations”(意为:几个(至少两个)不同的朝向)。方法分两步:

  1. 闭式解给出初始估计。平面图案的每个视图给出一组方程,合在一起就得到内参和每个标定板位姿的粗略估计。
  2. 细化。非线性优化同时调整所有参数,包括畸变,直到模型预测的角点尽可能接近图像中检测到的角点。

预测角点与检测角点之间的差距就是重投影误差。它是求解器最小化的量,也是每个标定工具都会报告的数字。

Reprojection error A close-up of four squares of a checkerboard. At each inner corner, a cross marks where the corner was detected in the image and a circle marks where the current calibration predicts it should appear. The small gap between them is the reprojection error that the solver minimises. Reprojection error + detected corner ○ predicted Covering the frame with target views The camera's image frame, overlaid with the outlines of the calibration target as seen in many views: near each corner and edge, in the centre, close and far, and tilted at different angles, so that every part of the image and every lens parameter is constrained. Cover the whole frame corners, edges, near, far, tilted
The solver adjusts the parameters until predicted corners land on detected ones. But a low error only means something if the views cover the whole frame, near and far, at many tilts.

Zhang 的方法最少需要两个视图;OpenCV 的教程说 “For better results, we need at least 10 test patterns.”(意为:要得到更好的结果,我们至少需要 10 个测试图案。)在实际中,光有数量是不够的:视图在哪里也很重要。mrcal 作者对采集动作编排的研究得出结论:“Closeups are extremely important”(意为:近距离拍摄极其重要),“Tilted views are good”(意为:倾斜的视图是好的)。图像的每个部分,尤其是畸变最强的四角,都应该在不同距离和倾斜角度下看到标定板。十个视图全在画面中央,标定的就是画面中央。

重投影误差低并不能证明标定好。一个模型可以把它自己的图像拟合得很漂亮,却在标定板从未到过的所有地方都是错的。mrcal 是一个标定工具包,“originally built at NASA/JPL to generate the high-accuracy calibrations demanded by long-range stereo”(意为:最初在 NASA/JPL 开发,用于生成远距离双目所要求的高精度标定),它甚至声明 “reprojection error is not used as a gauge of calibration quality”(意为:重投影误差不用作衡量标定质量的标准);它改为计算标定的不确定度并做交叉验证。

  • 棋盘格是经典的标定板,但整块板必须可见,而且对称的板可能被上下颠倒地读取:Kalibr 的相机–IMU 指南警告说,带对称性的标定板会造成 “flips in the target pose estimates”(意为:标定板位姿估计的翻转)。
  • 基准标记板(fiducial-marker board)在每个方格里放一个编码唯一的标记:OpenCV 中的 ChArUco 板,或 Kalibr 的 Aprilgrid,后者建立在 AprilTag 标记(Olson,2011)之上。每个角点都知道自己是哪个角点。Kalibr 的标定板文档列出了两个实际好处:“partially visible calibration boards can be used”(意为:可以使用只有部分可见的标定板),以及 “pose of the target is fully resolved (no flips)”(意为:标定板的位姿被完全确定(没有翻转))。对于广角镜头,标定板经常被画面边缘截断,这一点很重要。

这里标定板保持不动,采集设备运动。Kalibr 的相机–IMU 指南用几行字给出了做法:在标定板前移动采集设备,以 “excite all IMU axes (rotation and translation)”(意为:激励 IMU 的所有轴(旋转和平移)),用短的快门时间和良好的光照来 “keep the motion blur low”(意为:保持低运动模糊),在开始和结束时 “avoid shocks”(意为:避免冲击),并且 “ensure low jitter timestamps in same clock”(意为:确保时间戳抖动低且在同一个时钟下)。

一个工程师挥动一块板,可以把一台相机标定好。要标定生产线上下来的每一台相机,或者一批设备中的每一台采集设备,就需要把专业经验做进流程里。

第一步是不再依赖操作员对标定板该放在哪里的判断。AprilCal(Richardson、Strom 和 Olson,2013)从这样一个观察出发:好的标定 “usually requires an expert intuition”(意为:通常需要专家的直觉)。它利用到目前为止的标定来建议标定板下一步该放在哪里,并检查结果是否达到用户要求的精度。在作者的试验中,它 “consistently yields more accurate camera calibrations than standard tools”(意为:始终得到比标准工具更精确的相机标定)。

据我们所知,生产线使用机械臂或电动夹具,让设备或标定板对每一台相机都走过同一组按脚本设定的位姿。每台相机得到相同的覆盖,所以每次标定都可以按同一个标准来评判。由机器人来移动时,它自身已知的运动也有用处:经典的手眼(hand–eye)问题,即求解机器人夹爪与它所携带的相机之间的固定变换,可追溯到 Tsai 和 Lenz(1989),而 OpenCV 的 calibrateHandEye 实现了它以及四种后来的方法。

An automated calibration flow Five steps left to right: a robot or fixture moves the device through scripted poses in front of targets; the software detects the target corners; it solves for intrinsics and extrinsics; it checks the result's uncertainty and passes or fails the unit; and it stores the calibration against that unit's serial number. Below, a dashed arrow: in every recording session, online calibration refines the values from the recording itself. Scripted poses robot or fixture Detect corners every view Solve intrinsics, extrinsics, time Check uncertainty: pass or fail Store against the serial number Every session: online calibration refines it from the recording
Automated, every unit gets the same poses, the same checks and its own stored calibration, which is then refined in each recording session.

然后,评判也可以自动化。工作站不必由工程师目测重投影误差,而可以像 mrcal 那样计算标定的不确定度,并判定这台相机合格或不合格。每台相机的标定随后可以按序列号存储,并随这台相机的每一次录制一起流转。Meta 对它的每一副研究眼镜都做标定:它的标定文档说 “Each Aria Gen2 device is individually calibrated in the factory”(意为:每台 Aria Gen2 设备都在工厂单独标定)。开源硬件的 Ego-OSCAR 双目采集设备把每台设备的标定 “with every session it recorded, so that downstream users never fall back on nominal factory intrinsics.”(意为:随它录制的每个录制时段一起提供,使下游用户永远不必退回去使用标称的出厂内参。)

出厂值不会一直成立。Meta 为它的 Aria 眼镜写的文档对原因异常坦率:“Device calibration parameters can drift from their factory values”(意为:设备的标定参数可能偏离出厂值),它列出的第一个原因是 “mechanical deformation when worn by different users”(意为:由不同用户佩戴时的机械形变)。它列出的其他原因是温度、“accumulated wear from shocks or thermal cycles”(意为:冲击或热循环累积造成的损耗)和 IMU 零偏漂移:头戴的采集设备戴上时会弯曲,运行时会变热,还会受到磕碰。

所以自动化的最后一个阶段是在线标定:每个录制时段都从录制本身细化标定。Aria 的处理服务按录制时段重新优化其 SLAM 相机和 IMU 的内参与外参,Meta 的建议也很直接:“For any task requiring accurate geometric understanding of the world, use online calibration by default.”(意为:对任何需要对世界有准确几何理解的任务,默认使用在线标定。)OpenVINS(Geneva 等,2020)等开源估计器把相机内参、相机到 IMU 的变换和相机到 IMU 的时间偏移都纳入它们标定的内容。

在线标定并不取代工厂标定。它需要一个好的起点来细化,而这个起点由工厂提供。

  • 为标定而设计。相机与 IMU 之间用刚性框架,镜头不能重新对焦,这样标定能保持得更久。
  • 锁定会动的东西。关闭自动对焦,关闭防抖,曝光受控。
  • 标定每一台相机,采用按脚本设定的覆盖和自动的合格或不合格判定,而不是某人瞥一眼的重投影数字。同一型号的各台相机并不共用一组数字;原因见下文。
  • 让标定随数据一起交付,带版本并与序列号绑定,这样处理流程永远不会用另一台相机的数字去处理素材。
  • 预期会有漂移,并为按录制时段细化做好计划。

为什么每台相机都需要自己的标定

Section titled “为什么每台相机都需要自己的标定”

每台相机是在公差范围内装配的,而不是严格按图纸,标定测量的是装配的结果。针孔模型有三个部分取决于一台相机是怎样装起来的(教科书上的几何):

  • 焦距。以像素为单位的标定焦距,等于镜头到传感器的距离除以像素间距。像小型 M12 镜头那样用螺纹拧进镜座再手工锁紧的镜头,在每台相机上最终都会稍微靠里或靠外一点,而很小的行程就有影响:在本实用指南的 2.0 mm 示例镜头上,8 µm 的行程使焦距改变 0.4%,并使边缘上的一个点移动 2.6 px(对焦改变内参)。
  • 主点。这是镜头光轴与传感器相交的位置。镜头在传感器上方安装得稍微偏离中心,或者传感器在镜头下方放置得稍微偏离中心,都会使主点移动,移动量是这个偏移除以像素间距。传感器与光轴不完全垂直,会增加一个小的透视倾斜(传感器倾斜)。
  • 外参。每个相机和 IMU 在每台设备上所处的位置和角度都略有不同。Lee、Hanley 和 Bretl 说得很直白:“It is commonly the case that engineering drawings dictate a reasonable initial guess for the (nominal) position and orientation of a set of sensors, but manufacturing tolerances place the sensors at different locations and orientations”(意为:通常的情况是,工程图纸为一组传感器的(标称)位置和朝向给出了合理的初始估计,但制造公差使传感器处于不同的位置和朝向)(Lee、Hanley 和 Bretl,2022)。

已发表的采集设备正是因为这个原因而逐台标定。Ego-OSCAR:“Calibration is performed per device rather than per device model, which matters for a fleet of hand-assembled units whose optical alignment differs from unit to unit”(意为:标定是按每台设备而不是按设备型号进行的,这对一批手工装配、光学对准各不相同的设备很重要)(Palanisamy 等,2026)。RoboCap 在其整批设备上解出的焦距分布在 590–630 px 之间,没有通过其逐参数公差判定门限的设备会被返工而不是出货(Ali 等,2026)。论文把这一分布范围归因于用同一次标定板扫动来标定它的两种镜头,没有进一步拆分;这个数字,以及焦距误差对度量深度的影响,见标称标定与逐台标定。