# Stereo Calibration

Whenever working with stereoscopy, it is a necessity to calibrate the cameras and get the required intrinsic and extrinsic parameters.

This is a so-called pinhole camera model. A scene view is formed by projecting 3D points into the image plane using a perspective transformation.

### What is Camera Calibration?

In the manufacturing process of a camera, there might be some distortions invloved due to inaccurate positioning of the lens or inaccuracy in manufacturing parabolic lens. Read more about distortion on AI shack

Calibrating the camera gives us some specific values which can be used to measure distances in length units and not in pixels. Goal of calibration to find out intrinsic and extrinsic parameters of the camera.

### Intrinsic Parameters

Intrinsic parameters include focal length, image format and principal point. This is the intrinsic matrix.

α_{x} and α_{y} denote focal length in x direction and y direction respectively.
u_{0} and v_{0} denote the principal point which would be ideally in the center.

### Extrinsic Parameters

**R** and **T** are the extrinsic parameters which represent the coordinate transormation from 3D world to 3D camera model.

### Camera Calibration

One standard procedure to calibrate camera is to use a chessboard pattern. It is easier to calibrate using chessboard pattern because it is flat so no concerns of depth, it is easier to extract corner points as they are extensively defined. All the corners lie on the same line. Many different poses are used to get a better calibration.

#### Calibration using OpenCV

OpenCV has a great support for calibration and there’s is a very convinient way to do it.

Include necessary libraries

**Main function**

`imagePoints`

represent location of the detected corners in the image.

`objectPoints`

represent the real location of the corners in 3D.

Initialize Camera and define image containers.

objectPoints should contain physical location of each corners but since we don’t know that so we assign constant positions to all the corners and assume that camera is moving.

Get data for calibration

Draw Corners on chessboard and show

Chessboard Corners in Calibration

You found corners for one pose. You need to add a loop over this and get `numBoards`

poses.

Start Calibration

`CM`

is 3x3 floating-point camera matrix.`D`

is the vector of distortion coefficients.`rvecs`

is vector of rotation vectors estimated for each pattern view.`tvecs`

is vector of translation vectors estimated for each pattern view.

Undistort the image.

Save Calibration Data.

### Stereo Calibration

Stereo calibration is similar to single camera calibration but it invloves more steps and gives complete intrinsic and extrinsic parameters.

Follow the procedure for single camera calibration till `cameraCalibration`

method. You need to define two imagePoints vectors and need to find chessboard in both images.

Chessboard Corners in Stereo Images.

Start Stereo Calibration.

`CM1`

- Camera Matrix of first camera.`CM2`

- Camera Matrix of second camera.`D1`

- Distortion coeff matrix of first camera.`D2`

- Distortion coeff matrix of second camera.`R`

- Rotation Matrix between first and second camera coordinate systems.`T`

- Translation vector between the coordinate systems of the cameras.`E`

- Essential matrix.`F`

- Fundamental matrix.

Start Stereo Rectification.

`stereoRectify`

computes rectification transforms for each calibrated stereo camera.

`R1`

- 3x3 rectification transform (rotation matrix) for the first camera.`R2`

- 3x3 rectification transform (rotation matrix) for the second camera.`P1`

- 3x4 projection matrix in the new (rectified) coordinate systems for the first camera.`P2`

- 3x4 projection matrix in the new (rectified) coordinate systems for the second camera.`Q`

– 4x4 disparity-to-depth mapping matrix.

Store Calibration Data.

Undistort and remap image.

Once you have calibrated your camera, you can use the saved parameters in other applications where you need 3D data using Stereo Vision. eg. getting depth of any object.

You can find C++ source code on my GitHub.

Other important links

Image courtsey - OpenCV docs, stackoverflow.

P.S. Need to work on Machine Learning.