Camera Calibration Using OpenCV and Python

There seems to be a lot of confusing on camera calibration in OpenCV, there is an official tutorial on how to calibrate a camera, (Camera Calibration) which doesn't seem to work for many people.

Here is a working version of Camera Calibration based on the official tutorial.

Install Necessary Libraries:

These libraries can be easily installed using pip package manager.

Download Calibration Pattern

The default chessboard calibration pattern doesn't seem to work. So here is little bit different one for which we will have to change some parameters to make it work.

8X8

Download Full Resolution

Print The Pattern

Print the chessboard 8x8 pattern, attach it to a flat surface and take a lot of pictures of the pattern from different angels and distances like these:

1 2 3

Change Parameters

I'm not get into the details what these variables do, for that you can read the full tutorial on the official website.

Change: Line 10: objp = np.zeros((7*7,3), np.float32)

Line 11: objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)

Line 28: ret, corners = cv2.findChessboardCorners(gray, (7,7), None)

Line 35: img = cv2.drawChessboardCorners(img, (7,7), corners2, ret)

And that's it. Here is the full code:

Results:

1 2

Run The Script

You will end up with a file called calibration_matrix.yaml that will contain the calibration matrix and distortion coefficients.

camera_matrix:

dist_coeff:

And that's it. Now you can use the matrix to calibrate your camera.