Calibrating Our Camera with OpenCV
Learn how to calibrate a camera and undistort an image with Python and OpenCV.
We'll cover the following...
To calibrate a camera using OpenCV, we need to perform the following steps:
- Read a series of images of a chessboard pattern in different orientations, taken with the camera, that needs to be calibrated.
- Identify a chessboard pattern in each of these images.
- Use this information to calculate the camera matrix and distortion coefficients of the chosen camera.
- Save the camera matrix and distortion coefficients for future use.
To subsequently undistort an image, we need to do the following:
- Load the camera matrix and distortion coefficients of our camera.
- Calculate a new optimal camera matrix.
- Undistort the image using the original camera matrix, the distortion coefficients, and the new camera matrix.
Press + to interact
Calibrating a camera
Let’s look at an example of how to calibrate a camera:
Press + to interact
import cv2import numpy as npimport globfrom datetime import datetimenum_corners = (10, 7)objp = np.zeros((num_corners[1] * num_corners[0], 3), np.float32)objp[:, :2] = np.mgrid[0:num_corners[0], 0:num_corners[1]].T.reshape(-1, 2)object_points = []image_points = []images = glob.glob('./resources/calibration_images/*.png')print("nr. of calibration images found:", len(images))for image_file in images:image = cv2.imread(image_file)gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)chessboard_flags = cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGEret, corners = cv2.findChessboardCorners(gray_image, num_corners, chessboard_flags)if ret:object_points.append(objp)image_points.append(corners)image_corners = cv2.drawChessboardCorners(image, num_corners, corners, ret)#cv2.imwrite(f"./output/chessboard_corners_{datetime.now().strftime('%Y%m%d_%H%M%S.%f')}.png", image_corners)image_size = gray_image.shape[::-1]ret, camera_matrix, distortion_coeffs, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points, image_size, None, None)np.savez("./output/calibration_data.npz", camera_matrix=camera_matrix, distortion_coeffs=distortion_coeffs)
Lines 1–4: First, we import the required libraries. These libraries are cv2
...