Camera calibration with OpenCV
May 05, 2017
3 mins read
Optical systems are imperfect, that is why any camera prone to produce geometric distortions. There are Barrel and Pincushion distortions. In addiction, a mixture of both distortion types, sometimes referred to as mustache distortion.
Generally speaking, distortion is a deviation from rectilinear projection, hence, raw distorted images could not be used for photogrammetry.
In some cases the camera vendor provides the transformation matrix for undistortion. However, if it is not available, one can calculate all necessary coefficients with OpenCV to be able to perform such undistortional transformation. OpenCV takes into account both the radial and tangential factors.
To measure distortion of an optical systems it is possible to use photos of real world object with well-known shape. In the case, classical black-white chessboard pattern was used. It can be downloaded and printed on a well-sized paper. It is really important to attach the test sheet on a flat surface for proper camera calibration. It is enough to have 10-20 images for calibration.
Corners could be automatically found with the following command:
Resulted code for a bunch of calibration images:
If it is needed, found corners can be visualized with an OpenCV function like this:
Given the object points (an ideal model of desired points on a surface) and the image points (found chessboard corners), it is possible to calculate all calibration coefficients for the undistortion process:
OpenCV provides a useful function undistort:
Here is an example of its use:
The code was used in the Lane Lines Detection project.