# C202 第 3 课：用 Kalibr 做相机–IMU 标定

> C202 IMU 课程的第三课：Kalibr 的相机–IMU 标定如何工作，它需要的录制（固定的标定板和在每个轴上都运动的采集设备），它的输入和 IMU 模型，Kalibr wiki 给出的命令，它写出的文件，以及如何评判残差、零偏、变换和时间偏移。

Page: https://egocam.ai/zh-cn/university/c202/lesson-3-kalibr-camera-imu/

相机已经标定（C201），IMU 的噪声值已经测出（第 2 课），还剩一项标定：IMU 相对每台相机在什么位置，以及它们的时钟之间是什么关系。Kalibr 的 `kalibr_calibrate_imu_camera` 同时估计这两者。本课讲录制、输入、命令和结果。分步骤的版本见 [Kalibr 工作流程](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)的第 6 到第 9 步。

## 本课用到的术语

另见[术语表的标定一节](https://egocam.ai/zh-cn/reference/glossary/)。

| 术语 | 含义 |
|---|---|
| 相机–IMU 外参（camera–IMU extrinsics） | IMU 坐标系与某台相机坐标系之间的旋转和平移 |
| `T_cam_imu` | Kalibr 中从 IMU 坐标到相机坐标的变换；它的平移量是从相机看 IMU 所在的位置 |
| `timeshift_cam_imu` | Kalibr 加到相机时间戳上、以得到 IMU 时钟上同一时刻的时间偏移 |
| 激励（excitation） | 使每个参数都可观测的运动：绕每个轴旋转，并沿每个轴平移 |
| 连续时间批量标定（continuous-time batch calibration） | 用一条样条拟合采集设备的轨迹，使 IMU 采样和相机帧可以联合优化外参和时间偏移 |
| 归一化残差（normalised residual） | 误差除以求解器被告知应当预期的噪声 |

## Kalibr 如何求解

Kalibr 遵循 [Furgale, Rehder and Siegwart (2013)](https://doi.org/10.1109/IROS.2013.6696514) 的方法。它把 IMU 的轨迹和零偏表示为 B 样条，即时间的连续函数。样条可以在任意时刻求值，所以把图像时间戳按一个候选偏移量平移时，不需要对 IMU 数据重新积分，于是这个偏移就成为单次批量估计中又一个未知量，与重力方向、相机–IMU 变换以及随时间变化的位姿和零偏一起求解（[用 Kalibr 离线估计](https://egocam.ai/zh-cn/calibration/camera-imu-time-offset/)）。

在一次估计中完成这件事很重要。在 40 个真实数据集上，该论文报告的 RMS 时序误差为 "0.054ms, 0.344ms, 0.165ms and 0.572ms"（意为：0.054 ms、0.344 ms、0.165 ms 和 0.572 ms），分别对应联合标定、单独的时间延迟估计、仅用陀螺仪和仅用加速度计。

> **Diagram:** Kalibr's two calibrations and the files that pass between them. The camera calibration uses a recording in which the cameras stay still and the target moves; the camera–IMU calibration uses one in which the target stays still and the rig moves on every axis, and it takes camchain.yaml from the first step plus the IMU noise values in imu.yaml. Rates are the Kalibr wiki's suggestions (about 4 Hz for the first recording; 20 Hz camera and 200 Hz IMU for the second). Each tool also writes a results file and a PDF report to judge before you accept the output.

## 录制：标定板不动，采集设备动

角色与 C201 相反。[相机–IMU 指南](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration)说："The calibration target is fixed in this calibration and the camera-imu system is moved in front of the target to excite all IMU axes."（意为：在这项标定中，标定板是固定的，相机–IMU 系统在标定板前移动，以激励 IMU 的所有轴。）它的提示：

- "try to excite all IMU axes (rotation and translation)"（意为：尽量激励 IMU 的所有轴（旋转和平移））
- "avoid shocks, especially at the beginning/end when you pick up the sensor"（意为：避免冲击，尤其是在开头和结尾拿起传感器的时候）
- "keep the motion blur low"（意为：保持运动模糊小），采用 "low shutter times"（意为：短快门时间）和 "good illumination"（意为：良好的照明）
- "ensure low jitter timestamps in same clock"（意为：确保时间戳抖动小，并且在同一个时钟上）

每条提示的理由，包括为什么冲击和没有标定板的帧有害，列在 [Kalibr 工作流程，第 7 步](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)中。该指南还说 "Good results have been obtained by using a camera rate of 20 Hz and an IMU rate of 200 Hz."（意为：使用 20 Hz 的相机帧率和 200 Hz 的 IMU 采样率取得了良好的结果。）Furgale 及其同事的录制时长 "typically 1–2 minutes"（意为：通常为 1 到 2 分钟）；在他们的实验中，每次录制 "about 90 seconds"（意为：约 90 秒），并且 "an average absolute angular velocity of about 55° s−1 and an average absolute acceleration of 1.1m/s2"（意为：平均绝对角速度约为 55 °/s，平均绝对加速度为 1.1 m/s²）（[Kalibr 工作流程，第 7 步](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)）。

同样这些页面上有两点警告。只左右摇摆的采集设备，在其他轴上什么也没给求解器。原因在于杆臂：IMU 相对相机的偏移只体现在加速度计中，通过一些需要旋转才出现的项，而绕某一个轴的旋转会掩盖沿该轴的任何偏移（[为什么每个轴都必须动](https://egocam.ai/zh-cn/calibration/imu-calibration/)）。另外，使用对称的标定板时，"movements which could lead to flips in the target pose estimates have to be avoided"（意为：必须避免可能导致标定板位姿估计翻转的运动）；用 Aprilgrid 可以避开这个问题。

录制时，IMU 的配置要与生产中完全一样：相同的输出速率，相同的滤波器设置。滤波器会改变时间偏移，第 4 课会说明。

## 输入

| 输入 | 参数 | 从哪里来 |
|---|---|---|
| 一个包含图像和 IMU 数据的 ROS 1 bag | `--bag` | 直接录制、由 ROS 2 转换，或者用 `kalibr_bagcreater` 由图像和一个 `imu0.csv` 生成（[bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format)） |
| 相机标定 | `--cam` | `kalibr_calibrate_cameras` 输出的 `<bag>-camchain.yaml`（[C201 实验](https://egocam.ai/zh-cn/university/c201/lab/)） |
| IMU 文件，每个 IMU 一个 | `--imu` | 带有第 2 课噪声值的 `imu.yaml` |
| 标定板 | `--target` | 带有实测标定板尺寸的 `target.yaml` |
| IMU 模型，每个 IMU 一个 | `--imu-models` | `calibrated`、`scale-misalignment` 或 `scale-misalignment-size-effect` |

源代码把相机参数写作 `--cams`；wiki 和工具的用法说明写的是 `--cam`（[kalibr_calibrate_imu_camera](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)）。该工具用 Python 的 `argparse` 构建选项，它在默认情况下 "allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous"（意为：允许把长选项缩写为一个前缀，只要这个缩写没有歧义）（[argparse 文档](https://docs.python.org/3/library/argparse.html)），所以 wiki 的 `--cam` 会对应到 `--cams`。

**IMU 模型**。默认的 `calibrated` 假定 IMU 的比例和失准已经修正。`scale-misalignment` 还会估计每个传感器的一个比例与失准矩阵、陀螺仪对加速度的敏感度，以及陀螺仪轴与加速度计轴之间的旋转。`scale-misalignment-size-effect` 还会确定加速度计每个轴的位置（[imu.yaml：输出文件](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)）。

## 命令

该指南给出了一般形式：

```bash
kalibr_calibrate_imu_camera --bag [filename.bag] --cam [camchain.yaml] --imu [imu.yaml] --target [target.yaml]
```

以及针对其示例 bag 的这个例子：

```bash
rosrun kalibr kalibr_calibrate_imu_camera \
	--target april_6x6.yaml \
	--imu imu_adis16448.yaml \
	--imu-models calibrated \
	--cam cam_april-camchain.yaml \
	--bag imu_april.bag
```

该指南说明时间标定默认开启；`--no-time-calibration` 将它关闭。相机之间的变换沿用相机标定的结果，除非你传入 `--recompute-camera-chain-extrinsics`，源代码称它 "exclusively recommended for debugging purposes"（意为：仅建议用于调试目的）（[Kalibr 工作流程，第 8 步](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)）。

## 输出什么

| 文件 | 内容 |
|---|---|
| `<bag>-camchain-imucam.yaml` | camchain，加上每台相机的 `T_cam_imu` 和 `timeshift_cam_imu` |
| `<bag>-imu.yaml` | 输入的 IMU 值，加上模型、`T_i_b`、`time_offset`，对于更丰富的模型还有估计出的矩阵 |
| `<bag>-results-imucam.txt` | 归一化残差和未归一化的残差、两个方向的变换、时间偏移、基线、重力向量 |
| `<bag>-report-imucam.pdf` | 各曲线图 |

这些文件名是源代码写出的名称；wiki 把它们的顺序反过来写，写作 `camchain-imucam-%BAGNAME%.yaml`（[文件一览](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)）。

Kalibr 把每个变换命名为 `T_a_b`，它把坐标系 b 中的点变到坐标系 a。所以，用 [YAML formats](https://github.com/ethz-asl/kalibr/wiki/Yaml-formats) 页面的话说，`T_cam_imu` 是 "transformation from IMU to camera coordinates (T_c_i)"（意为：从 IMU 坐标到相机坐标的变换（T_c_i）），它的平移量是 IMU 在相机坐标系中的位置。结果文件把两个方向都打印出来，写作 `T_ci` 和 `T_ic`。

> **Diagram:** 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).

## 评判结果

wiki 的 [RealSense 教程](https://github.com/ethz-asl/kalibr/wiki/Calibrating-Intel-RealSense-Cameras)用几行字给出了各项检查；实用指南列出了每一项在哪里（[Kalibr 工作流程，第 9 步](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)、[report-imucam.pdf](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)）：

| 检查 | 在哪里 | 应当看到什么 | 如果不是 |
|---|---|---|---|
| IMU 采样时序 | IMU 测量速率页 | 在预期速率处的一条平线 | "Inspect the IMU time dt plot carefully in the PDF report!"（意为：仔细检查 PDF 报告中的 IMU 时间 dt 图！）先修好时间戳 |
| IMU 残差 | 加速度误差页和角速度误差页 | 在 ±3σ 虚线之内 | "your IMU noise or the dataset are incorrect"（意为：你的 IMU 噪声或数据集不正确） |
| 零偏估计 | 加速度计零偏页和陀螺仪零偏页 | 在 ±3σ 边界之内 | "your trajectory was too dynamic, or your noise values are not good"（意为：你的轨迹太剧烈，或者你的噪声值不好） |
| 变换 | 结果文件中的 `T_ci` | 与采集设备相符 | "Sanity check your final rotation and translation with hand-measured values."（意为：用手工测量的值对最终的旋转和平移做合理性检查。） |
| 重投影 | 重投影页 | 点在 3σ 圆之内 | 减小运动模糊 |

±3σ 线来自你提供的噪声值。多 IMU 页面说，它们的作用是 "foster an intuition about the correctness of the noise parameters and models"（意为：帮助建立对噪声参数和模型是否正确的直觉）。第 2 课的放大倍数就是这样评判的：残差远在线外，说明假定的噪声太小或者数据不好。据我们所知，归一化残差远小于 1，说明假定的噪声比数据表现出来的大得多（[results-imucam.txt](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)）。

时间偏移有它自己的一课，就是下一课。

## 自测

1. 在相机–IMU 录制中，什么不动，什么动？这与 C201 有什么不同？
2. 你的采集设备在标定板前左右挥动了两分钟。这段录制有什么问题？
3. 你的 IMU 的比例因子从未标定过。你给 `--imu-models` 传什么值？
4. cam0 的 `T_cam_imu` 在 x 方向有约 3 cm 的平移量。这个数描述的是什么？
5. 角速度残差远在 ±3σ 虚线之外。说出两个可能的原因。

### 答案

1. 标定板不动，采集设备动，而且在每个轴上都动。在相机标定中，相机不动，标定板动。
2. 它只激励了一个旋转轴。求解器需要绕每个轴的旋转和沿每个轴的平移。
3. `scale-misalignment`（或 `scale-misalignment-size-effect`），它会估计它们；`calibrated` 假定它们已经修正。
4. IMU 在 cam0 坐标系中的位置，单位为米。用尺子或机械图纸核对它。
5. `imu.yaml` 中的噪声值太小（放大得不够），或者数据集不好，例如时间戳有问题。

## 参考来源

- 实用指南：[Kalibr 工作流程](https://egocam.ai/zh-cn/kalibr/kalibr-workflow/)、[Kalibr 文件参考](https://egocam.ai/zh-cn/kalibr/kalibr-file-reference/)、[相机–IMU 时间偏移](https://egocam.ai/zh-cn/calibration/camera-imu-time-offset/)、[采集设备的 IMU 标定](https://egocam.ai/zh-cn/calibration/imu-calibration/)、[术语表](https://egocam.ai/zh-cn/reference/glossary/)。
- Kalibr wiki，ETH Zurich ASL：[camera–IMU calibration](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration)、[Multi-IMU and IMU intrinsic calibration](https://github.com/ethz-asl/kalibr/wiki/Multi-IMU-and-IMU-intrinsic-calibration)、[YAML formats](https://github.com/ethz-asl/kalibr/wiki/Yaml-formats)、[bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format)、[Calibrating Intel RealSense cameras](https://github.com/ethz-asl/kalibr/wiki/Calibrating-Intel-RealSense-Cameras)。
- Kalibr 源代码，[github.com/ethz-asl/kalibr](https://github.com/ethz-asl/kalibr)（commit `1f60227`）：[kalibr_calibrate_imu_camera](https://github.com/ethz-asl/kalibr/blob/master/aslam_offline_calibration/kalibr/python/kalibr_calibrate_imu_camera)。
- Python Software Foundation. [argparse documentation](https://docs.python.org/3/library/argparse.html)（`allow_abbrev`）。
- Furgale, P., Rehder, J. and Siegwart, R. (2013). [Unified temporal and spatial calibration for multi-sensor systems](https://doi.org/10.1109/IROS.2013.6696514). *IEEE/RSJ IROS*.
