Stereo camera calibration and epipolar rectification implemented with OpenCV. It supports two calibration board types — chessboard and AprilGrid — and three lens distortion models — pinhole, fisheye, and Mei omnidirectional.
| File / Directory | Description |
|---|---|
| calib.py | Entry point: calibration + epipolar rectification |
| headless_calib.py | Entry point for headless (no GUI) environments |
| calib/ | Calibration library: base class + 3 model subclasses + 2 board strategies |
| sgbm.py | SGBM disparity / depth / point cloud |
| utils/ | Helper utilities (point cloud, pseudo color, string) |
| data/ | Raw images (*_ori/raw) and generated results (*_ori/) |
Raw images must be combined stereo images: the top half is the left camera and the bottom half is the right camera by default (see --left_down).
| Argument | Meaning |
|---|---|
--board_type chessboard |
Select the chessboard board |
--row / --col |
Number of inner corners per row / column |
--block_size |
Side length of one square (mm) |
| Argument | Meaning |
|---|---|
--board_type aprilgrid |
Select the AprilGrid board |
--row / --col |
Number of tags per row / column |
--tag_size |
Side length of one AprilTag (mm) |
--tag_space |
Gap between adjacent tags (mm) |
--dictionary |
AprilTag family, e.g. t36h11 (default) |
Instead of passing --board_type / --row / --col / --tag_size / --tag_space (or --block_size) on the command line, you can describe the board once in a kalibr-style target yaml and pass it with --target:
python calib.py --raw_dir=./data/aprilgrid_ori/raw --target=./target_new.yaml --model=pinholeExample target_new.yaml (aprilgrid):
target_type: 'aprilgrid' #gridtype
tagCols: 6 #number of apriltags
tagRows: 6 #number of apriltags
tagSize: 0.055 #size of apriltag, edge to edge [m]
tagSpacing: 0.3 #ratio of space between tags to tagSizeSupported keys:
| target_type | kalibr-style keys (lengths in meters) | repo-style keys (lengths in mm) |
|---|---|---|
aprilgrid |
tagCols / tagRows / tagSize / tagSpacing (ratio) / optional tagFamily |
row / col / tag_size / tag_space / dictionary |
checkerboard |
boardCols / boardRows / spacingMeters |
row / col / block_size |
Notes:
tagCols(horizontal) maps to--row,tagRows(vertical) maps to--col— both count tags for aprilgrid and inner corners for checkerboard.- Precedence: an explicit CLI flag overrides the target file, and the target file overrides the built-in defaults.
--targetworks the same withheadless_calib.py(arguments are forwarded unchanged).
| Model | Description |
|---|---|
pinhole |
Standard pinhole model (cv2.calibrateCamera + cv2.stereoCalibrateExtended) |
fish |
Fisheye model (cv2.fisheye), for wide-angle / fisheye lenses |
mei |
Mei omnidirectional model (cv2.omnidir), for ultra-wide / fisheye lenses |
For a heavily distorted lens, prefer fish or mei. It is recommended to try several models and pick the one with the lowest stereo reprojection error.
Install the dependencies inside a virtual environment. First create and activate one (pick either venv or conda), then install from requirements.txt.
Using venv:
python3 -m venv .venv
source .venv/bin/activateUsing conda:
conda create -n stereo_calib python=3.10 -y
conda activate stereo_calibpip install -r requirements.txtFor headless (no display) environments, replace
opencv-contrib-pythonwithopencv-contrib-python-headlessinrequirements.txt(see the note inside the file).
- Capture at least 20 stereo image pairs.
- Put the raw combined images into
./data/chessboard_ori/raw(chessboard) or./data/aprilgrid_ori/raw(AprilGrid), or use a custom directory via--raw_dir. - Keep the camera and the board stationary during each capture. The board should occupy roughly 30% ~ 80% of the image, be mostly centered, and include some frames at the image edges and some tilted within about ±30°.
- Alternatively, supply already-split left/right images via
--left_dir/--right_dir(see 6.5).
# pinhole model
python calib.py --raw_dir=./data/chessboard_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --model=pinhole
# fisheye model
python calib.py --raw_dir=./data/chessboard_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --model=fish
# Mei omnidirectional model
python calib.py --raw_dir=./data/chessboard_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --model=meiFor a vertical stereo rig (cameras stacked top/bottom instead of side by side), add --rig=top_bottom to switch to vertical (y-direction) epipolar alignment. The --rig flag (from camera rig, i.e. how the stereo camera pair is mounted) selects the baseline orientation: left_right = side-by-side (default, horizontal epipolar lines), top_bottom = stacked vertically (vertical epipolar lines). The example dataset ./data/chessboard_vertical_ori/raw is such a rig: in its combined images the top camera sits at the bottom and the bottom camera sits at the top, so --left_down is also required (left = bottom half).
# pinhole model
python calib.py --raw_dir=./data/chessboard_vertical_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --left_down --rig=top_bottom --model=pinhole
# fisheye model
python calib.py --raw_dir=./data/chessboard_vertical_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --left_down --rig=top_bottom --model=fish
# Mei omnidirectional model
python calib.py --raw_dir=./data/chessboard_vertical_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --left_down --rig=top_bottom --model=meiAll three lens models (pinhole, fish, mei) support vertical alignment. In top_bottom mode the epipolar alignment error is reported along dx (column difference) instead of dy.
# pinhole model
python calib.py --raw_dir=./data/aprilgrid_ori/raw --board_type=aprilgrid --row=11 --col=8 --tag_size=55 --tag_space=16.5 --model=pinhole
# fisheye model
python calib.py --raw_dir=./data/aprilgrid_ori/raw --board_type=aprilgrid --row=11 --col=8 --tag_size=55 --tag_space=16.5 --model=fish
# Mei omnidirectional model
python calib.py --raw_dir=./data/aprilgrid_ori/raw --board_type=aprilgrid --row=11 --col=8 --tag_size=55 --tag_space=16.5 --model=meiFor headless (no display) environments, replace
calib.pywithheadless_calib.py; the arguments are identical:python headless_calib.py --raw_dir=./data/chessboard_ori/raw --board_type=chessboard --row=20 --col=11 --block_size=60 --model=pinhole
| Argument | Default | Description |
|---|---|---|
--raw_dir |
./data/chessboard_ori/raw |
Directory of raw combined stereo images |
--left_dir / --right_dir |
(none) | Pre-split left / right image dirs; when both set, take priority over --raw_dir |
--target |
(none) | Kalibr-style board yaml (aprilgrid / checkerboard), see section 2.3; explicit CLI board flags override it |
--board_type |
chessboard |
chessboard or aprilgrid |
--row |
20 |
Chessboard: inner corners per row; AprilGrid: tags per row |
--col |
11 |
Chessboard: inner corners per column; AprilGrid: tags per column |
--block_size |
60 |
Chessboard square side length (mm) |
--left_down |
(off) | Off: left up / right down; On: left down / right up |
--rig |
left_right |
Baseline orientation (camera rig, i.e. how the stereo pair is mounted): left_right = horizontal epipolar; top_bottom = vertical (y-direction) epipolar |
--tag_size |
70 |
AprilGrid tag side length (mm) |
--tag_space |
21 |
AprilGrid tag gap (mm) |
--dictionary |
t36h11 |
AprilGrid tag family, e.g. t36h11 or t36h11b1 |
--rect_w / --rect_h |
-1 |
Rectified image size; -1 means keep the original size |
--model |
pinhole |
pinhole, fish, or mei |
--load_calib |
(none) | Load calibration from a JSON/YAML file and only rectify (skip calibration) |
--pose_view |
camera |
camera: draw camera poses in board frame; board: draw board poses in camera frame |
--fish_fill_duplicate_points |
(off) | Experimental: pad AprilGrid points so every frame has the same point count |
Instead of combined (top/bottom) images, you can point directly at two directories that already hold the left and right images separately. The two directories must contain the same number of images, with matching file names in the same sorted order.
# pre-split left/right dirs (skip the combine split step)
python calib.py --left_dir=./data/chessboard_ori/left --right_dir=./data/chessboard_ori/right \
--board_type=chessboard --row=20 --col=11 --block_size=60 --model=pinhole
python calib.py --left_dir=./data/aprilgrid_ori/left --right_dir=./data/aprilgrid_ori/right \
--board_type=aprilgrid --row=11 --col=8 --tag_size=55 --tag_space=16.5 --model=meiWhen --left_dir and --right_dir are both given, --raw_dir and --left_down are ignored. Generated results are written under the parent of --left_dir.
To rectify images with an existing calibration instead of re-calibrating, pass the saved parameter file via --load_calib. It accepts the files produced by this tool: calib_{model}.json, stereo_{model}.yaml, or stereo_opencv_{model}.yaml.
The model is read automatically from the file, and no board arguments are required — rectification only needs the input images and the parameter file. The input can be either pre-split left/right directories (--left_dir / --right_dir) or a single directory of combined top/bottom images (--raw_dir):
# pre-split left/right dirs (JSON, model auto-detected)
python calib.py --left_dir=./data/chessboard_ori/left --right_dir=./data/chessboard_ori/right \
--load_calib=./data/chessboard_ori/calib_pinhole.json
# single dir of combined top/bottom images (YAML, model auto-detected)
python calib.py --raw_dir=./data/aprilgrid_ori/raw \
--load_calib=./data/aprilgrid_ori/stereo_mei.yamlWhen --load_calib is set, calibration is skipped and only rectification is performed (the epipolar-alignment / size checks that depend on a board are also skipped).
After a successful run, the following are produced under the parent of raw_dir (e.g. ./data/chessboard_ori):
| Item | Description |
|---|---|
left/, right/ |
Split left / right raw images |
chessboard/ or aprilgrid/ |
Detected corner / tag visualization |
rectify_{model}/ |
Rectified left / right images |
pose_distribution_{model}/ |
Board / camera pose distribution |
reproject_distribution_{model}/ |
Reprojection error distribution |
reproject_overlay_{model}/ |
Reprojected points overlaid on the images |
epipolar_alignment_{model}/ |
Epipolar alignment check images + summary |
rectified_size_check_{model}/ |
Rectified board physical-size check |
calib_{model}.json |
Calibration result (JSON) |
stereo_{model}.yaml |
Calibration result (YAML) |
stereo_opencv_{model}.yaml |
Calibration result (OpenCV FileStorage YAML) |
- Check that the stereo reprojection error printed at the end of the log is < 0.5.
- Check that the images in
rectify_{model}/are correctly rectified.
If both checks pass, the calibration is successful. Otherwise, see the failure cases below and recollect the images.
-
Reprojection error greater than 0.5.
-
Reprojection error less than 0.5, but the calibration parameters are abnormal and the
rectifyimages are completely wrong. -
Reprojection error less than 0.5, but the
rectifyimages have anomalies. The parameters are not completely unusable, but they will degrade depth accuracy. For example:
If any of the above occurs, recollect the images and calibrate again.
Two flag sets are configurable in the calibration library:
pinhole:cv2.CALIB_USE_INTRINSIC_GUESS | cv2.CALIB_RATIONAL_MODELfish:cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC | cv2.fisheye.CALIB_CHECK_COND | cv2.fisheye.CALIB_FIX_SKEW
A typical output log looks like:
-- ====================== Stereo Calib Start ======================
...
-- Left image reprojection error: 0.12949030109517565, Right image reprojection error: 0.12460417066817898
-- left camera matrix:
[[658.1741247 0. 646.05353639]
[ 0. 658.30601712 548.53666377]
[ 0. 0. 1. ]]
-- Stereo reprojection error: 0.12937212606379386
-- F B cx cy doffs fov_scale: 612.5314060500974, 79.6253762710031, 648.7790623361407, 550.2822714476908, -0.0 0.8
-- ======================= Stereo Calib End =======================
...
=> The reprojection error is less than 0.5, and the calibration is successful.The rectification code is in calib.py (via the calib/ library). When cv2.CALIB_ZERO_DISPARITY is set, the principal points of the left and right images are forced to be equal; when it is 0, the principal points may differ, keeping a larger field of view (FOV), in which case doffs is not 0.
The depth formula is: Depth = F * B / (Disparity + Doffs).
Given the rectified images and rectified intrinsics, disparity, depth, and point cloud can be computed with sgbm.py.








