# C202 Lesson 3: Camera-IMU Calibration with Kalibr

> The third lesson of the C202 IMU course: how Kalibr's camera-IMU calibration works, the recording it needs (a fixed target and a rig moved on every axis), its inputs and IMU models, the command as the Kalibr wiki gives it, the files it writes, and how to judge the residuals, biases, transform and time shift.

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

With the cameras calibrated (C201) and the IMU's noise values measured (Lesson 2), one calibration remains: where the IMU sits relative to each camera, and how their clocks relate. Kalibr's `kalibr_calibrate_imu_camera` estimates both at once. This lesson covers the recording, the inputs, the command and the result. The step-by-step version is on the [Kalibr Workflow](https://egocam.ai/kalibr/kalibr-workflow/), steps 6 to 9.

## Terms used in this lesson

See also the [glossary's calibration section](https://egocam.ai/reference/glossary/#calibration).

| Term | Meaning |
|---|---|
| Camera–IMU extrinsics | The rotation and translation between the IMU's frame and a camera's frame |
| `T_cam_imu` | Kalibr's transform from IMU coordinates to camera coordinates; its translation is the IMU's position seen from the camera |
| `timeshift_cam_imu` | The time offset Kalibr adds to a camera timestamp to get the same instant on the IMU clock |
| Excitation | Motion that makes every parameter observable: rotation about, and translation along, every axis |
| Continuous-time batch calibration | Fitting a spline to the rig's trajectory so IMU samples and camera frames can be optimised together for the extrinsics and the time offset |
| Normalised residual | An error divided by the noise the solver was told to expect |

## How Kalibr solves it

Kalibr follows [Furgale, Rehder and Siegwart (2013)](https://doi.org/10.1109/IROS.2013.6696514). It represents the IMU's trajectory and biases as B-splines, continuous functions of time. A spline can be evaluated at any instant, so shifting the image timestamps by a candidate offset needs no re-integration of the IMU data, and the offset becomes one more unknown in a single batch estimate, with the gravity direction, the camera–IMU transform and the time-varying pose and biases ([Offline estimation with Kalibr](https://egocam.ai/calibration/camera-imu-time-offset/#offline-estimation-with-kalibr)).

Doing it in one estimate matters. On 40 real datasets, the paper reports RMS timing errors of "0.054ms, 0.344ms, 0.165ms and 0.572ms" for the joint calibration, a separate time-delay estimate, gyroscope only and accelerometer only.

> **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.

## The recording: the target stays still, the rig moves

The roles swap from C201. The [camera–IMU guide](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration) says: "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." Its tips:

- "try to excite all IMU axes (rotation and translation)"
- "avoid shocks, especially at the beginning/end when you pick up the sensor"
- "keep the motion blur low", with "low shutter times" and "good illumination"
- "ensure low jitter timestamps in same clock"

The reasons for each tip, including why shocks and frames without the target hurt, are listed in the [Kalibr workflow, step 7](https://egocam.ai/kalibr/kalibr-workflow/#7-record-the-cameraimu-dataset). The guide adds that "Good results have been obtained by using a camera rate of 20 Hz and an IMU rate of 200 Hz." Furgale and colleagues recorded for "typically 1–2 minutes"; in their experiments each recording was "about 90 seconds", with "an average absolute angular velocity of about 55° s−1 and an average absolute acceleration of 1.1m/s2" ([Kalibr workflow, step 7](https://egocam.ai/kalibr/kalibr-workflow/#7-record-the-cameraimu-dataset)).

Two warnings from the same pages. A rig that only pans left and right gives the solver nothing on the other axes. The reason is the lever arm: the IMU's offset from the camera shows up only in the accelerometer, through terms that need rotation, and rotation about one axis hides any offset along that axis ([Why every axis has to move](https://egocam.ai/calibration/imu-calibration/#why-every-axis-has-to-move)). And with a symmetric target, "movements which could lead to flips in the target pose estimates have to be avoided"; an Aprilgrid avoids the problem.

Record with the IMU configured exactly as it will be in production: same output rate, same filter settings. The filter changes the time offset, as Lesson 4 shows.

## The inputs

| Input | Flag | Where it comes from |
|---|---|---|
| A ROS 1 bag with images and IMU data | `--bag` | recorded directly, converted from ROS 2, or built with `kalibr_bagcreater` from images and an `imu0.csv` ([bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format)) |
| The camera calibration | `--cam` | `<bag>-camchain.yaml` from `kalibr_calibrate_cameras` ([C201 lab](https://egocam.ai/university/c201/lab/)) |
| The IMU file, one per IMU | `--imu` | `imu.yaml` with the noise values from Lesson 2 |
| The target | `--target` | `target.yaml` with the measured board dimensions |
| The IMU model, one per IMU | `--imu-models` | `calibrated`, `scale-misalignment` or `scale-misalignment-size-effect` |

The source spells the camera flag `--cams`; the wiki and the tool's usage text write `--cam` ([kalibr_calibrate_imu_camera](https://egocam.ai/kalibr/kalibr-file-reference/#kalibr_calibrate_imu_camera)). The tool builds its options with Python's `argparse`, which by default "allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous" ([argparse documentation](https://docs.python.org/3/library/argparse.html)), so the wiki's `--cam` reaches `--cams`.

**The IMU model.** The default, `calibrated`, assumes the IMU's scale and misalignment are already corrected. `scale-misalignment` also estimates a scale-and-misalignment matrix for each sensor, the gyroscope's sensitivity to acceleration, and the rotation between the gyroscope and accelerometer axes. `scale-misalignment-size-effect` also locates each accelerometer axis ([imu.yaml: the output file](https://egocam.ai/kalibr/kalibr-file-reference/#imuyaml-the-output-file)).

## The command

The guide gives the general form:

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

and this example for its sample 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
```

The guide states that temporal calibration is on by default; `--no-time-calibration` turns it off. The camera-to-camera transforms are kept from the camera calibration unless you pass `--recompute-camera-chain-extrinsics`, which the source calls "exclusively recommended for debugging purposes" ([Kalibr workflow, step 8](https://egocam.ai/kalibr/kalibr-workflow/#8-calibrate-camera-to-imu)).

## What comes out

| File | What it holds |
|---|---|
| `<bag>-camchain-imucam.yaml` | the camchain plus `T_cam_imu` and `timeshift_cam_imu` for each camera |
| `<bag>-imu.yaml` | the input IMU values plus the model, `T_i_b`, `time_offset` and, for the richer models, the estimated matrices |
| `<bag>-results-imucam.txt` | normalised and plain residuals, the transforms in both directions, the time shift, baselines, the gravity vector |
| `<bag>-report-imucam.pdf` | the plots |

The file names are the ones the source writes; the wiki writes them the other way round, as `camchain-imucam-%BAGNAME%.yaml` ([The files at a glance](https://egocam.ai/kalibr/kalibr-file-reference/#the-files-at-a-glance)).

Kalibr names every transform `T_a_b`, which takes a point in frame b into frame a. So `T_cam_imu` is, in the [YAML formats](https://github.com/ethz-asl/kalibr/wiki/Yaml-formats) page's words, the "transformation from IMU to camera coordinates (T_c_i)", and its translation is where the IMU sits in the camera's frame. The results file prints both directions, as `T_ci` and `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).

## Judging the result

The wiki's [RealSense tutorial](https://github.com/ethz-asl/kalibr/wiki/Calibrating-Intel-RealSense-Cameras) gives the checks in a few lines; the field guide lays out where each one is ([Kalibr workflow, step 9](https://egocam.ai/kalibr/kalibr-workflow/#9-judge-the-results), [report-imucam.pdf](https://egocam.ai/kalibr/kalibr-file-reference/#report-imucampdf-cameraimu-calibration)):

| Check | Where | What to see | If not |
|---|---|---|---|
| IMU sample timing | the IMU measurement-rates page | a flat line at the expected rate | "Inspect the IMU time dt plot carefully in the PDF report!" Fix timestamps first |
| IMU residuals | acceleration and angular velocity error pages | inside the dashed ±3σ lines | "your IMU noise or the dataset are incorrect" |
| Bias estimates | accelerometer and gyroscope bias pages | inside the ±3σ bounds | "your trajectory was too dynamic, or your noise values are not good" |
| Transform | `T_ci` in the results file | matches the rig | "Sanity check your final rotation and translation with hand-measured values." |
| Reprojection | reprojection pages | points inside the 3σ circle | reduce motion blur |

The ±3σ lines come from the noise values you supplied. The multi-IMU page says they are there to "foster an intuition about the correctness of the noise parameters and models". That is how you judge the inflation factor from Lesson 2: residuals far outside the lines mean the assumed noise is too small or the data is bad. To the best of our knowledge, normalised residuals far below one mean the assumed noise is much larger than the data shows ([results-imucam.txt](https://egocam.ai/kalibr/kalibr-file-reference/#results-imucamtxt-cameraimu-calibration)).

The time shift has its own lesson, next.

## Self-check

1. In the camera–IMU recording, what stays still and what moves? How is that different from C201?
2. Your rig was waved left and right in front of the board for two minutes. What is wrong with the recording?
3. Your IMU's scale factors have never been calibrated. Which `--imu-models` value do you pass?
4. `T_cam_imu` for cam0 has a translation of about 3 cm in x. What does that number describe?
5. The angular-velocity residuals lie far outside the dashed ±3σ lines. Name two possible causes.

### Answers

1. The target stays still and the rig moves, on every axis. In camera calibration the cameras stay still and the target moves.
2. It excites only one rotation axis. The solver needs rotation about, and translation along, every axis.
3. `scale-misalignment` (or `scale-misalignment-size-effect`), which estimates them; `calibrated` assumes they are already corrected.
4. Where the IMU sits in cam0's frame, in metres. Check it against a ruler or the mechanical drawing.
5. The noise values in `imu.yaml` are too small (not inflated enough), or the dataset is bad, for example with broken timestamps.

## Sources

- Field guide: [Kalibr Workflow](https://egocam.ai/kalibr/kalibr-workflow/), [Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/), [Camera-IMU Time Offset](https://egocam.ai/calibration/camera-imu-time-offset/), [IMU Calibration for Camera Rigs](https://egocam.ai/calibration/imu-calibration/), [glossary](https://egocam.ai/reference/glossary/#calibration).
- 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 source code, [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*.
