Camera calibration using OpenCV

Being not ideal, all cameras distort their images. Usually, it’s not so important and can be even funny. But if your image analysis requires quite precise images, you should take care about calibrating your camera to remove distortions.

Luckily, OpenCV provides everything we need for that. I don’t want to repeat the theory and description of that process – you can easily find it on the OpenCV website. I just created a simple Python application calibrationlab, calibrating cameras:


The application was written on Python 3.6 using opencv v3.4.1 and wxpython v4.0.3. So you’ll need to install them first, using pip or conda. Also, you will need to print out a chessboard.docx.

Start the application running If you have a few cameras connected to your computer, you can select one using the “Camera” menu. Place the chessboard image in front of your camera and press the “Calibrate” button. Make a few tries to get minimal Mean Error. Resulting calibration parameters can be saved in a file. They contain camera matrix and distortion coefficients. Later you can use them loading from the file without the need to recalibrate your camera again. As an example, I included such a file (camera.json) into the source repository.

When the calibration process is done, you can check the”Undistort” checkbox to apply calibration parameters. The undistorted image is a bit smaller than the original one. I intentionally left a black border around the image. But you can crop the image, using roi (region of image) parameter, returned by the cv2.getOptimalNewCameraMatrix() method.

If you are using a good camera you hardly spot the difference between original and undistorted images. Otherwise, you will notice that straight lines in the real world become straight in the image.

The source code: GitHub download