C++ Python CMake
Switch branches/tags
Nothing to show
Clone or download
Latest commit 4358b67 Jan 9, 2018


OpenCV C++ Stereo Camera Calibration

This repository contains some sources to calibrate the intrinsics of individual cameras and also the extrinsics of a stereo pair.


  • OpenCV
  • popt


Compile all the files using the following commands.

mkdir build && cd build
cmake ..

Make sure your are in the build folder to run the executables.

Get images from webcams

This is a small helper tool to grab frames from two webcams operating as a stereo pair. Run the following command to use it.

./read -w [img_width] -h [img_height] -d [imgs_directory] -e [file_extension]

Once it is running, hit any key to grab frames. Images are saved with prefixes left and right in the desired directory.

Intrinsic calibration of a single camera

This is only for lenses which follow the pinhole model. If you have fisheye lenses with a very wide field of view then see this repository. The calibration saves the camera matrix and the distortion coefficients in a YAML file. The datatype for these matrices is Mat.

Once you have compiled the sources run the following command to calibrate the intrinsics.

./calibrate -w [board_width] -h [board_height] -n [num_imgs] -s [square_size] -d [imgs_directory] -i [imgs_filename] -o [file_extension] -e [output_filename]

For example, the command for the test images in calib_imgs/1/ would be

./calibrate -w 9 -h 6 -n 27 -s 0.02423 -d "../calib_imgs/1/" -i "left" -o "cam_left.yml" -e "jpg"

Stereo calibration for extrinisics

Once you have the intrinsics calibrated for both the left and the right cameras, you can use their intrinsics to calibrate the extrinsics between them.

./calibrate_stereo -n [num_imgs] -u [left_cam_calib] -v [right_cam_calib] -L [left_img_dir] -R [right_img_dir] -l [left_img_prefix] -r [right_img_prefix] -o [output_calib_file]

For example, if you calibrated the left and the right cameras using the images in the calib_imgs/1/ directory, the following command to compute the extrinsics.

./calibrate_stereo -n 27 -u cam_left.yml -v cam_right.yml -L ../calib_imgs/1/ -R ../calib_imgs/1/ -l left -r right -o cam_stereo.yml

Undistortion and Rectification

Once you have the stereo calibration data, you can remove the distortion and rectify any pair of images so that the resultant epipolar lines become scan lines.

./undistort_rectify -l [left_img_path] -r [right_img_path] -c [stereo_calib_file] -L [output_left_img] -R [output_right_img]

For example

./undistort_rectify -l ../calib_imgs/1/left1.jpg -r ../calib_imgs/1/right1.jpg -c cam_stereo.yml -L left.jpg -R right.jpg