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 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. u0 and v0 denote the principal point which would be ideally in the center.
R and T are the extrinsic parameters which represent the coordinate transormation from 3D world to 3D camera model.
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
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
CMis 3x3 floating-point camera matrix.
Dis the vector of distortion coefficients.
rvecsis vector of rotation vectors estimated for each pattern view.
tvecsis vector of translation vectors estimated for each pattern view.
Undistort the image.
Save Calibration Data.
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.