Skip to content

About

No description, website, or topics provided.

Resources

Stars

7 stars

Watchers

4 watching

Forks

Latest commit

 

History

47 Commits

Folders and files

Repository files navigation

Stereo Calibration

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.

1. File Description

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/)

2. Calibration Boards

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

2.1 Chessboard

chessboard board

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)

2.2 AprilGrid

aprilgrid board

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)

2.3 Board Parameters via Target File (--target)

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=pinhole

Example 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 tagSize

Supported 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.
  • --target works the same with headless_calib.py (arguments are forwarded unchanged).

3. Lens Models

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.

4. Install Dependencies

Install the dependencies inside a virtual environment. First create and activate one (pick either venv or conda), then install from requirements.txt.

4.1 Create and activate a virtual environment

Using venv:

python3 -m venv .venv
source .venv/bin/activate

Using conda:

conda create -n stereo_calib python=3.10 -y
conda activate stereo_calib

4.2 Install dependencies

pip install -r requirements.txt

For headless (no display) environments, replace opencv-contrib-python with opencv-contrib-python-headless in requirements.txt (see the note inside the file).

5. Data Preparation

  1. Capture at least 20 stereo image pairs.
  2. 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.
  3. 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°.
  4. Alternatively, supply already-split left/right images via --left_dir / --right_dir (see 6.5).

6. Run Calibration

6.1 Chessboard (20 × 11 inner corners, 60 mm square)

# 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=mei

6.2 Chessboard — Vertical (Top/Bottom) Rig

For 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=mei

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

6.3 AprilGrid (11 × 8 tags, 55 mm tag, 16.5 mm gap)

# 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=mei

For headless (no display) environments, replace calib.py with headless_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

6.4 Full Command-line Arguments

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

6.5 Pre-split Left/Right Images

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=mei

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

6.6 Rectify-only Mode (Load Calibration Parameters)

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

When --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).

7. Output

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)

8. Verify Results

  1. Check that the stereo reprojection error printed at the end of the log is < 0.5.
  2. 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.

9. Calibration Failure Cases

  • Reprojection error greater than 0.5.

    calib_error1.png

  • Reprojection error less than 0.5, but the calibration parameters are abnormal and the rectify images are completely wrong.

    calib_error2.png

  • Reprojection error less than 0.5, but the rectify images have anomalies. The parameters are not completely unusable, but they will degrade depth accuracy. For example:

    calib_error3.png calib_error4.png calib_error5.png calib_error6.png calib_error7.png

If any of the above occurs, recollect the images and calibrate again.

10. Calibration Configuration

Two flag sets are configurable in the calibration library:

  • pinhole: cv2.CALIB_USE_INTRINSIC_GUESS | cv2.CALIB_RATIONAL_MODEL
  • fish: 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.

11. Epipolar Rectification

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

12. SGBM Depth

Given the rectified images and rectified intrinsics, disparity, depth, and point cloud can be computed with sgbm.py.

About

No description, website, or topics provided.

Resources

Stars

7 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages