# Camera Calibration in OpenCV

A step by step guide to calibrating your camera with OpenCV. This complicated topic made simple and easy.

Updated March 25, 2023

Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Welcome to this tutorial on camera calibration in OpenCV! As a computer vision enthusiast, you might already know that camera calibration is an essential step to obtain accurate measurements from real-world images. In this tutorial, we will explore camera calibration in depth, understand the theory behind it, and learn how to perform it using OpenCV.

So, let’s dive in!

## What is Camera Calibration?

Camera calibration is the process of estimating the intrinsic and extrinsic parameters of a camera. These parameters help us model the relationship between a 3D point in the real world and its 2D projection in the image. By calibrating the camera, we can correct distortions and obtain more accurate measurements from images.

Intrinsic parameters are specific to the camera and include focal length, optical center, and lens distortion coefficients. Extrinsic parameters define the camera’s position and orientation in the 3D world.

Check out the camera calibration Wikipedia page for a more detailed explanation of the concept.

## Why is Camera Calibration Important?

Camera calibration is crucial for many computer vision tasks, such as 3D reconstruction, structure from motion, and object tracking. By obtaining the correct camera parameters, we can accurately map 3D points to 2D image coordinates, which helps in measuring real-world dimensions from images and improves the performance of computer vision algorithms.

## The Theory Behind Camera Calibration

The process of camera calibration involves finding a set of points in the 3D world and their corresponding 2D projections in the image. These points are usually obtained from a calibration object, such as a chessboard pattern.

We use the pinhole camera model to represent the relationship between 3D points and their 2D projections. The model includes intrinsic and extrinsic parameters, as well as distortion coefficients that account for radial and tangential distortions caused by the camera lens.

In OpenCV, the function `calibrateCamera()` estimates the camera parameters using a set of 3D-2D point correspondences.

## Camera Calibration with OpenCV: A Step-by-Step Guide

Now that we have a basic understanding of camera calibration, let’s see how to perform it using OpenCV. We’ll be using Python for our examples, but you can also use the OpenCV C++ API.

### Step 1: Install OpenCV

First, let’s install OpenCV. You can do this by running the following command:

``````pip install opencv-python opencv-python-headless
``````

### Step 2: Capture Images of the Calibration Pattern

Capture a series of images of the chessboard pattern from different angles and distances. Make sure the chessboard is well-lit and covers a wide area of the image. Save these images to a folder for processing.

### Step 3: Find Chessboard Corners

Now, let’s write a function to detect chessboard corners in the images. We’ll use OpenCV’s `findChessboardCorners()` function for this purpose.

``````import cv2
import numpy as np

def find_chessboard_corners(images, pattern_size):
obj_points = []
img_points = []

# Prepare the 3D points of the chessboard corners (0,0,0), (1,0,0), (2,0,0) ..., (6,5,0)
objp = np.zeros((pattern_size[0] * pattern_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:pattern_size[0], 0:pattern_size[1]].T.reshape(-1, 2)

for img in images:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)

if ret:
obj_points.append(objp)
img_points.append(corners)

return obj_points, img_points
``````

### Step 4: Calibrate the Camera

Next, we’ll use the `calibrateCamera()` function from OpenCV to estimate the camera parameters using the object points and image points obtained in the previous step.

``````def calibrate_camera(obj_points, img_points, img_size):
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, img_size, None, None)
return ret, mtx, dist, rvecs, tvecs
``````

### Step 5: Undistort Images

With the camera parameters estimated, we can now undistort images using OpenCV’s undistort() function. This function corrects radial and tangential distortions, improving the accuracy of the images.

``````def undistort_image(img, mtx, dist):
undistorted_img = cv2.undistort(img, mtx, dist, None, mtx)
return undistorted_img
``````

### Step 6: Putting It All Together

Now, let’s put all the pieces together and calibrate our camera!

``````import glob

# Load the images
image_files = glob.glob('path/to/chessboard/images/*.jpg')
images = [cv2.imread(file) for file in image_files]

# Find the chessboard corners
pattern_size = (9, 6)
obj_points, img_points = find_chessboard_corners(images, pattern_size)

# Calibrate the camera
img_size = (images[0].shape[1], images[0].shape[0])
ret, mtx, dist, rvecs, tvecs = calibrate_camera(obj_points, img_points, img_size)

# Undistort an example image
undistorted_example = undistort_image(images[0], mtx, dist)

# Show the original and undistorted images
cv2.imshow('Original Image', images[0])
cv2.imshow('Undistorted Image', undistorted_example)

cv2.waitKey(0)
cv2.destroyAllWindows()
``````

This script will load the chessboard images, find the corners, calibrate the camera, and undistort an example image. You’ll see the original and undistorted images side by side, showcasing the improvements achieved by camera calibration.

And that’s it! You now know how to perform camera calibration in OpenCV. This process can greatly improve the accuracy of your computer vision applications and open up new possibilities in your projects.

Remember to keep experimenting and learning, and happy coding!