3 Ways To Calibrate Your Camera Using OpenCV and Python
Fix camera distortions in an easy way.

When a camera takes a photograph, we often see the image not quite the same as we see it in our brain. This is caused by the camera lens and it happens more than we think.
This alteration of the image is what we call distortion.
Generally speaking, distortion is when a straight lines appear bent or curvy in an image.
There are different types of distortion, depending essentially on the model of the camera lens you used.

Although you sometimes want to create a nice effect on your images, a distortion can be bad for computer vision systems. Since the coordinates of the image are deviated from its original position, you might create some errors or fail to detect an object.
When you calibrate a camera, you discover some specific parameters that will fix these distortions.
Camera Model

A camera model describes a relationship between a point in a 3D space and its projection into a 2D space (an image). The pinhole camera model is often used in computer vision as a reasonable approximation of a camera.
Duration the calibration process, some parameters are discovered to fix most of the distortions caused by a camera:
- Intrinsic parameters: focal length, optical center, and skew coefficients. Specific to each camera.
- Extrinsic parameters: rotation and translation vectors that translate a 3D scene into a 2D coordinate.
- Radial distortion coefficients: it models the radial distortion, that causes straight line to appear curved. It occurs when light rays bend more near the edges of a lens than they do at its optical center.
- Tangential distortion coefficients: it models the tangential distortion, that occurs when the lens and the image plane are not parallel.
To find these parameters, we need some images that contains a well defined pattern. The algorithm then find some specific points, whose coordinates we know from a real object. With the coordinates in the image, we then can solve the equations for the coefficients that we want.
There are different objects that we can use to calibrate that we are going to see bellow.
OpenCV
First of all, let’s create our environment. To calibrate our camera and get the coefficients, we are going to use the OpenCV library.
OpenCV is an open-source library that contains a lot of computer vision algorithms, from image processing to object detection. It is a powerful library that can be used on different platforms.
To install it:
pip install opencv-python
pip install opencv-python-contribNote: in this tutorial it was used OpenCV version 4.5.1 and Python 3.8.7, in a Windows 10 PC.
Chessboard
We can use a chessboard as the reference object. Since it is well defined and we know all the coordinates, it is a great object to use. In fact, it is the standard method to calibrate a camera.
First, let’s take some pictures of a chessboard using the camera we want to calibrate. You can use a chessboard image or use a real one. At least 10 images are necessary for better results.

Note: try to variate the position of the chessboard to get better results. Remember to put the chessboard on a flat surface.
To find the chessboard inside an image, we are going to use the function findChessboardCorners, passing the image and the pattern (number of rows and columns of the chessboard).
Once we find the corners, we improve their accuracy using the function cornerSubPix.
We do the same for every image we have, recording the corners we have found. After that, we call the function calibrateCamera that will return the camera matrix, distortion coefficients, rotation and translation vectors.










