# C201 Lab: Calibrate Your Fisheye Stereo Rig

> The hands-on lab of the C201 camera calibration course: calibrate the intrinsics and extrinsics of your own fisheye stereo pair with Kalibr, check it with a repeat calibration and known geometry, and optionally judge it with mrcal's projection uncertainty and cross-validation. Every command is as the tools' documentation gives it, with checkpoints and what to do when a step fails.

Page: https://egocam.ai/university/c201/lab/

This lab calibrates the cameras of your rig: each camera's intrinsics and the transform between the two cameras of a stereo pair. Camera-to-IMU calibration is course C202; steps 6 to 9 of the [Kalibr workflow](https://egocam.ai/kalibr/kalibr-workflow/) cover it.

The main path is Kalibr, which the field guide documents end to end. Part B is an optional mrcal path for teams that want projection uncertainty and cross-validation. Every command below is copied from the tool's own documentation, or follows its documented form with your own file names; the source is linked at each step. Square brackets mark values you fill in.

Work through [Lessons 1 to 4](https://egocam.ai/university/c201/) first, and keep the [checklist](https://egocam.ai/university/c201/checklist/) next to you.

## Part A: Kalibr

### Step 1: Set up Kalibr

Kalibr needs ROS 1. The Kalibr wiki's [installation page](https://github.com/ethz-asl/kalibr/wiki/installation) gives a Docker image for systems without it. Build it:

```bash
git clone https://github.com/ethz-asl/kalibr.git
cd kalibr
docker build -t kalibr -f Dockerfile_ros1_20_04 . # change this to whatever ubuntu version you want
```

Then start the container with your data folder mounted at `/data`:

```bash
FOLDER=/path/to/your/data/on/host
xhost +local:root
docker run -it -e "DISPLAY" -e "QT_X11_NO_MITSHM=1" \
    -v "/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    -v "$FOLDER:/data" kalibr
```

Inside the container, load the ROS environment with `source devel/setup.bash`. The installation page's example then runs each tool as `rosrun kalibr <tool>`; the other wiki pages show the bare tool name. The commands below keep the form of the page they come from.

**You should see** a shell prompt inside the container, and your data folder's files under `/data`.

**If it fails:** every tool also lists its options with `--h` ([Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/#the-command-line-tools)). If plots or the report window do not open, the `DISPLAY` and X11 lines above are what pass the display into the container.

### Step 2: Lock the cameras

Fix everything the intrinsics depend on, in its final working state ([Lesson 1](https://egocam.ai/university/c201/lesson-1-what-calibration-estimates/), [Kalibr workflow, step 1](https://egocam.ai/kalibr/kalibr-workflow/#1-prepare-the-cameras)):

- resolution, crop and binning mode;
- focus, with autofocus off; lock a manual focus ring mechanically if you can;
- optical and electronic stabilisation off;
- a short, fixed exposure in good light, so the corners stay sharp without extra gain noise ([why bright, even light](https://egocam.ai/calibration/targets-and-calibration-sequences/#lighting-why-calibration-wants-bright-even-light)).

If the lenses have a manual focus ring and the cameras publish a live ROS image topic, Kalibr's [camera focus tool](https://github.com/ethz-asl/kalibr/wiki/camera-focus) helps set it the same way every time:

```bash
kalibr_camera_focus --topic [IMAGE_TOPIC]
```

**You should see** the live image with a focus measure. The wiki's instruction: point the camera at a detailed scene, such as a Siemens star or a checkerboard, "and adjust the focus while trying to minimize the calculated focus measure."

**Write down** the resolution, focus, exposure and each camera's serial number. They are part of the calibration.

### Step 3: Make and measure the target

Generate an Aprilgrid with the wiki's [target script](https://github.com/ethz-asl/kalibr/wiki/calibration-targets):

```bash
rosrun kalibr kalibr_create_target_pdf --type apriltag --nx [NUM_COLS] --ny [NUM_ROWS] --tsize [TAG_WIDTH_M] --tspace [TAG_SPACING_PERCENT]
```

Despite `PERCENT` in the wiki's placeholder, `--tspace` is a fraction such as 0.3, not 30; `--tsize` is in metres ([kalibr_create_target_pdf](https://egocam.ai/kalibr/kalibr-file-reference/#kalibr_create_target_pdf)). Size the board for your lenses: at the same range a bigger board is better, and mrcal's [choreography study](https://mrcal.secretsauce.net/tour-choreography.html) found that "we care about the size of the chessboard relative to its distance from the camera" ([targets page](https://egocam.ai/calibration/targets-and-calibration-sequences/#size-and-density)).

Then:

1. **Print it and glue it flat** to a rigid plate.
2. **Keep a white border** at least one grid element wide.
3. **Measure it.** Measure `tagSize` across one tag's outer black edges, and the gap between two neighbouring tags. Measuring across many tags and dividing reduces the error ([Measuring a printed target](https://egocam.ai/kalibr/kalibr-file-reference/#measuring-a-printed-target)).
4. **Write `target.yaml`** with the measured numbers, in the wiki's format:

```yaml
target_type: 'aprilgrid' #gridtype
tagCols: [NUM_COLS]      #number of apriltags
tagRows: [NUM_ROWS]      #number of apriltags
tagSize: [MEASURED_M]    #size of apriltag, edge to edge [m]
tagSpacing: [GAP/TAGSIZE] #ratio of space between tags to tagSize
```

**You should see** a `tagSpacing` between 0 and 1, and a `tagSize` with a decimal point.

**If it fails:** write lengths with a decimal point, such as `0.06`; a bare `1` is read as an integer and fails Kalibr's check. `tagSpacing` is a ratio: writing the gap in metres puts every corner in the wrong place ([target.yaml](https://egocam.ai/kalibr/kalibr-file-reference/#targetyaml-the-calibration-board)).

> **Diagram:** On an Aprilgrid, tagSize is the width of one tag across its black border, in metres. tagSpacing is not a length: it is the gap between tags divided by tagSize. The small black squares fill the gaps at the tag corners.

### Step 4: Record the camera sequence

The rig stays still and the target moves. Kalibr's [multiple-camera guide](https://github.com/ethz-asl/kalibr/wiki/multiple-camera-calibration) recommends lowering the camera rate "to around 4 Hz while capturing the calibration data", which it says "reduces redundant information in the dataset". Hide every other AprilTag in the room: a stray tag can carry the same ID as one of the grid's ([Lesson 2](https://egocam.ai/university/c201/lesson-2-targets-and-recording/#making-the-target)).

Plan the motion before you start ([Lesson 2](https://egocam.ai/university/c201/lesson-2-targets-and-recording/)):

- **Close-ups for each camera on its own**, filling as much of its image as you can, tilted forward, back, left and right.
- **The image corners and edges of both cameras.** Let the board run off the edge: an Aprilgrid still gives corners from a partial view.
- **Shared views**, with the board seen by both cameras at once, from just far enough out.
- **Slow, smooth motion**, so the corners stay sharp.

**You should see**, on a live view, the board reaching every part of both images over the recording.

**If it fails:** if you cannot see a live view, record more than you think you need and check coverage in the report (step 7). In the AprilCal study, novices following written instructions missed the image corners ([targets page](https://egocam.ai/calibration/targets-and-calibration-sequences/#what-happens-without-guidance-the-aprilcal-study)).

### Step 5: Get the data into a ROS 1 bag

Kalibr reads only ROS 1 bags. Use whichever fits your recorder ([Kalibr workflow, step 4](https://egocam.ai/kalibr/kalibr-workflow/#4-get-the-data-into-a-ros-1-bag)):

- **A ROS 2 bag:** convert it with the `rosbags` package, as the wiki's [ROS 2 guide](https://github.com/ethz-asl/kalibr/wiki/ROS2-Calibration-Using-Kalibr) shows. The wiki writes the version requirement without quotes; a shell reads the unquoted `>` as a redirect, so it is quoted here:

```bash
pip3 install "rosbags>=0.9.12" # might need -U
rosbags-convert <ros2_bag_folder> --dst calib_01.bag --exclude-topic <non_img_and_imu_topics>
```

- **Image files:** put each camera's images in its own folder, `cam0/` and `cam1/`, each file named by its timestamp in nanoseconds, such as `1385030208726607500.png`, and build a bag with the [bagcreater](https://github.com/ethz-asl/kalibr/wiki/bag-format):

```bash
kalibr_bagcreater --folder dataset-dir/. --output-bag awsome.bag
```

The topics come out as `/cam0/image_raw` and `/cam1/image_raw`. Kalibr uses the timestamps you put in; it does not correct them.

**Checkpoint:** inside the container, `rosbag info` gives a summary of a bag, including its topics, message counts and frequency ([ROS rosbag command line](https://wiki.ros.org/rosbag/Commandline)):

```bash
rosbag info [calib.bag]
```

**You should see** one image topic per camera, with similar message counts.

### Step 6: Calibrate the cameras

Choose a model for each camera ([Lesson 3](https://egocam.ai/university/c201/lesson-3-running-the-solver/)): `pinhole-equi` (Kannala–Brandt) for a fisheye short of 180°, or `ds-none` or `eucm-none`, which also suit lenses near or beyond 180°. Use the model your pipeline consumes. Then run, in the form the [installation page](https://github.com/ethz-asl/kalibr/wiki/installation) uses inside the container:

```bash
rosrun kalibr kalibr_calibrate_cameras \
    --bag /data/[calib.bag] --target /data/target.yaml \
    --models [MODEL_0] [MODEL_1] \
    --topics /cam0/image_raw /cam1/image_raw
```

The order of `--topics` and `--models` sets which camera is cam0.

**You should see** three files when it finishes: `<bag>-camchain.yaml`, `<bag>-results-cam.txt` and `<bag>-report-cam.pdf` ([Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/#the-files-at-a-glance)).

**If it fails:**

| Symptom | What the sources say to do |
|---|---|
| "Cameras are not connected through mutual observations" | record more frames with the target in both cameras; check that the cameras are synchronised; the message suggests "Maybe adjust the approx. sync. tolerance" (`--approx-sync`, default 0.02 s) |
| Optimisation diverges in the first images | "just try to restart the calibration as the initial guesses are based on a random pick of images" |
| Target never or rarely detected | add `--show-extraction` to watch detection; check `target.yaml`, the white border, other AprilTags in view and blur |
| Few frames used | a frame only counts with more tags than the grid's longer side: at least 7 on a 6 × 6 grid |
| "Omni with equidistant model not yet supported!" | use `omni-radtan` or another model |

Sources: [Kalibr workflow, common failures](https://egocam.ai/kalibr/kalibr-workflow/#common-failures) and [Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/#aprilgrid).

### Step 7: Read the result

Open `<bag>-results-cam.txt` and `<bag>-report-cam.pdf` and work through [Lesson 4](https://egocam.ai/university/c201/lesson-4-reading-the-result/):

1. **The target block** at the end of the results file holds the dimensions you measured.
2. **The reprojection error** for each camera: a mean close to zero in x and y, and a spread similar for both cameras.
3. **The parameter standard deviations** (the "+-" values): none large relative to its value.
4. **The corner plot** on each camera's reprojection-errors page: detections reach all four edges and corners.
5. **The polar error page:** error stays flat out to the widest angle.
6. **The Obs. graph page:** the two cameras are linked by shared observations.
7. **The baseline:** the translation in `T_cn_cnm1` for cam1 matches your caliper measurement of the lens spacing.

Pages and lines are described on the [Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/#report-campdf-camera-calibration).

**If a check fails:** error rising toward the edge, or no corners there, means record again with edge coverage or try a different fisheye model. A baseline off by the same percentage as every other translation means re-measure the target. Change one thing at a time and solve again.

### Step 8: Calibrate again and compare

Record a second, independent sequence under the same settings and calibrate it the same way. From our conversations with people in the industry, values that move a lot between two good recordings were not well determined by either ([Kalibr workflow, step 9](https://egocam.ai/kalibr/kalibr-workflow/#9-judge-the-results)). Compare the two calibrations in pixels, not parameter by parameter: high-order distortion coefficients can swing widely while the projections stay nearly identical ([Judging a Calibration, check 4](https://egocam.ai/calibration/judging-a-calibration/#check-4-repeatability-across-recordings)). The [prompts page](https://egocam.ai/university/c201/prompts/) has a prompt that writes the comparison script.

If your cameras publish live ROS topics, `kalibr_camera_validator` shows reprojection errors on live images, for each camera and between cameras ([calibration validator](https://github.com/ethz-asl/kalibr/wiki/calibration-validator)):

```bash
kalibr_camera_validator --cam camchain.yaml --target target.yaml
```

### Step 9: Check against known geometry

- **Straight lines:** undistort a frame of a scene with straight edges near the image border; they must come out straight ([Judging a Calibration, check 5](https://egocam.ai/calibration/judging-a-calibration/#check-5-known-physical-geometry)).
- **Depth on a known board:** if you run stereo depth, compare it with the board's pose solved from one camera. A wrong calibration gives depth maps that look sharp but are distorted in scale and shape ([check 6](https://egocam.ai/calibration/judging-a-calibration/#check-6-the-calibration-in-the-field)).

### Step 10: Sign off and store

Go through the sign-off section of the [checklist](https://egocam.ai/university/c201/checklist/). Then store the calibration with the data: versioned and tied to the unit's serial number, so a pipeline never processes footage with the wrong unit's numbers ([Camera Calibration](https://egocam.ai/calibration/camera-calibration/#what-this-means-for-a-capture-rig)). Keep `target.yaml`, the results file and the report with it.

## Part B (optional): judge the intrinsics with mrcal

mrcal reports projection uncertainty and supports cross-validation, the checks [Lesson 4](https://egocam.ai/university/c201/lesson-4-reading-the-result/) calls the strongest. A Kalibr fisheye result cannot be brought into mrcal for this: `mrcal-from-kalibr` supports only Kalibr's pinhole camera with radtan or no distortion ([its documentation](https://github.com/dkogan/mrcal/blob/master/mrcal-from-kalibr)). So Part B is a separate calibration in mrcal, with its own target and recording. Everything here is from mrcal's [how-to-calibrate guide](https://mrcal.secretsauce.net/how-to-calibrate.html) and [install page](https://mrcal.secretsauce.net/install.html).

### B1: Install

On Debian 12 or Ubuntu 22.04 and later, the install page gives:

```bash
apt install mrcal libmrcal-dev python3-mrcal
```

The chessboard corner finder, mrgingham, is a separate package: `apt install mrgingham`.

### B2: Target and recording

mrcal recommends a chessboard with the mrgingham detector; the mrgingham documentation provides a 14 × 14 chessboard PDF. Print it, mount it flat, and measure the corner spacing. mrgingham reports only complete boards, so watch a live view while you reach for the image edges.

The images must be synchronised across the cameras. Name them `frameFFF-cameraCCC.png`: images with the same `FFF` are taken as captured at the same instant, and `CCC` identifies the camera. Record two independent dances into two folders, for the cross-validation in B4.

### B3: Calibrate

Estimate a starting focal length with the guide's formula, f ≈ imager width in pixels ÷ (4 · tan(horizontal field of view ÷ 4)). Then, in each dance's folder, run the guide's command with your values and one glob per camera:

```bash
mrcal-calibrate-cameras                                                         \
  --corners-cache corners.vnl                                                   \
  --lensmodel LENSMODEL_SPLINED_STEREOGRAPHIC_order=3_Nx=30_Ny=18_fov_x_deg=[FOV] \
  --focal [FOCAL_PX]                                                            \
  --object-spacing [MEASURED_SPACING_M]                                         \
  --object-width-n 14                                                           \
  'frame*-camera0.png' 'frame*-camera1.png'
```

**You should see** `camera-0.cameramodel` and `camera-1.cameramodel` written. If `corners.vnl` does not exist, the tool runs mrgingham first and writes it.

**If it fails:** the guide notes that without usable joint views the extrinsics are undefined, and the solver complains about a "not positive definite" Hessian. Record more views both cameras share.

### B4: Uncertainty and cross-validation

For each camera, map the projection uncertainty:

```bash
mrcal-show-projection-uncertainty camera-0.cameramodel
```

Where it is poor, the guide's advice is that "you need more chessboard observations in that area." Then compare the two dances' models of the same camera:

```bash
mrcal-show-projection-diff [dance1]/camera-0.cameramodel [dance2]/camera-0.cameramodel
```

**You should see** a difference of about the sum of the two uncertainties. Far more than that points to a model error ([mrcal cross-validation](https://mrcal.secretsauce.net/tour-cross-validation.html)). To find the cause, look at the worst-fitting observations, as the guide shows:

```bash
mrcal-show-residuals-board-observation \
  --from-worst                         \
  --vectorscale 100                    \
  camera-0.cameramodel                 \
  0-5
```

The guide uses this to spot detector errors, rolling shutter, synchronisation errors and an unmodelled board shape by their residual patterns.

## Sources

- Kalibr wiki, ETH Zurich ASL: [installation](https://github.com/ethz-asl/kalibr/wiki/installation), [calibration targets](https://github.com/ethz-asl/kalibr/wiki/calibration-targets), [multiple-camera calibration](https://github.com/ethz-asl/kalibr/wiki/multiple-camera-calibration), [bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format), [ROS 2 calibration](https://github.com/ethz-asl/kalibr/wiki/ROS2-Calibration-Using-Kalibr), [camera focus](https://github.com/ethz-asl/kalibr/wiki/camera-focus), [calibration validator](https://github.com/ethz-asl/kalibr/wiki/calibration-validator).
- Kalibr source code, [github.com/ethz-asl/kalibr](https://github.com/ethz-asl/kalibr) (commit `1f60227`): `kalibr_calibrate_cameras` and `kalibr_create_target_pdf` options.
- ROS wiki: [rosbag command line](https://wiki.ros.org/rosbag/Commandline).
- Kogan, D. mrcal: [how to calibrate](https://mrcal.secretsauce.net/how-to-calibrate.html), [install](https://mrcal.secretsauce.net/install.html), [cross-validation](https://mrcal.secretsauce.net/tour-cross-validation.html), and the [mrcal-from-kalibr](https://github.com/dkogan/mrcal/blob/master/mrcal-from-kalibr) source.
- Field guide: [Kalibr Workflow](https://egocam.ai/kalibr/kalibr-workflow/), [Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/), [Calibration Targets and Calibration Sequences](https://egocam.ai/calibration/targets-and-calibration-sequences/), [Judging a Calibration](https://egocam.ai/calibration/judging-a-calibration/), [Camera Calibration](https://egocam.ai/calibration/camera-calibration/).
