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.
These libraries can be easily installed using pip package manager.
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.
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:
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:
You will end up with a file called calibration_matrix.yaml that will contain the calibration matrix and distortion coefficients.
camera_matrix:
[935.1344941116828, 0.0, 639.5005137221706]
[0.0, 931.4115487638016, 338.0236559548098]
[0.0, 0.0, 1.0]
dist_coeff:
- [0.13770456510420725, -0.9787743886214878, -0.011250061708974173, 0.0018853619055972332, 1.358881887579156]
And that's it. Now you can use the matrix to calibrate your camera.