Search⌘ K
AI Features

Calibrating Our Camera with OpenCV

Explore how to calibrate a camera with OpenCV by detecting chessboard patterns in images to compute camera parameters. Understand how to save calibration data and use it to undistort images, improving the accuracy of fiduciary marker detection in Python.

We'll cover the following...

To calibrate a camera using OpenCV, we need to perform the following steps:

  1. Read a series of images of a chessboard pattern in different orientations, taken with the camera, that needs to be calibrated.
  2. Identify a chessboard pattern in each of these images.
  3. Use this information to calculate the camera matrix and distortion coefficients of the chosen camera.
  4. Save the camera matrix and distortion coefficients for future use.

To subsequently undistort an image, we need to do the following:

  1. Load the camera matrix and distortion coefficients of our camera.
  2. Calculate a new optimal camera matrix.
  3. Undistort the image using the original camera matrix, the distortion coefficients, and the new camera matrix.

Calibrating a camera

Let’s look at an example of how to calibrate a camera:

Python 3.10.4
import cv2
import numpy as np
import glob
from datetime import datetime
num_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_IMAGE
ret, 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 ...