# Estimating Camera Parameters and Distortion Correction Using OpenCV and Python

## Introduction

In computer vision and image processing, estimating camera parameters and correcting distortion are crucial steps in creating accurate and realistic visual representations. OpenCV, widely recognized as a powerful computer vision library, provides robust tools for performing these tasks efficiently using the Python programming language. In this article, we will explore how to estimate camera parameters and correct distortion in images using OpenCV.

## Camera Calibration

Camera calibration is the process of estimating the intrinsic and extrinsic parameters of a camera. The intrinsic parameters include the focal length, principal point, and skew coefficient, which are specific to the camera itself. The extrinsic parameters define the camera's position and orientation in the world. By calibrating the camera, we can obtain these parameters and achieve accurate measurements in images.

### Chessboard Pattern

To calibrate a camera, we require images of a known pattern. One commonly used pattern is a chessboard. OpenCV provides convenient functions to detect and locate the corners of a chessboard in images.

To start, we need a set of images of the chessboard taken from different viewpoints with the same camera. Using the `cv2.findChessboardCorners()` function, we can find the corners of the chessboard in each image. By gathering the detected corners from multiple images, we can then perform camera calibration using `cv2.calibrateCamera()`.

``````# Sample code for chessboard corner detection

import cv2
import numpy as np

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Define chessboard size
pattern_size = (9, 6)

# Find chessboard corners
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)

if ret:
# Draw corners on the image
cv2.drawChessboardCorners(image, pattern_size, corners, ret)
cv2.imshow("Chessboard Corners", image)
cv2.waitKey(0)

cv2.destroyAllWindows()``````

### Camera Calibration

After obtaining the corners of the chessboard in multiple images, we can proceed with camera calibration using `cv2.calibrateCamera()`. This function estimates the camera matrix, distortion coefficients, rotation, and translation vectors.

``````# Sample code for camera calibration

images = [...]  # List of chessboard images

# Define chessboard size
pattern_size = (9, 6)

# Arrays to store object points and image points
object_points = []  # 3D points of chessboard corners in real world
image_points = []  # 2D points of chessboard corners in image

# Generate object points
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) * square_size

for image in images:
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Find chessboard corners
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)

if ret:
# Store object points and image points
object_points.append(objp)
image_points.append(corners)

# Calibrate camera
ret, camera_matrix, distortion_coeffs, rvecs, tvecs = cv2.calibrateCamera(
object_points, image_points, gray.shape[::-1], None, None
)``````

The output of the `cv2.calibrateCamera()` function provides the calibrated camera matrix (`camera_matrix`), distortion coefficients (`distortion_coeffs`), and rotation and translation vectors (`rvecs`, `tvecs`) necessary for various computer vision tasks.

## Distortion Correction

Camera lenses often introduce distortion in the captured images, deviating from the ideal pinhole camera model. OpenCV enables us to remove this distortion and rectify the image using the estimated camera parameters.

### Distortion Models

OpenCV offers two types of distortion models: radial distortion and tangential distortion. Radial distortion causes the image to appear stretched or compressed towards the edges, while tangential distortion leads to image skew.

To correct these distortions, we can use the `cv2.undistort()` function with the camera matrix and distortion coefficients obtained from camera calibration.

``````# Sample code for image distortion correction

# Distortion correction
undistorted = cv2.undistort(image, camera_matrix, distortion_coeffs)

# Display original and undistorted images
cv2.imshow("Original", image)
cv2.imshow("Undistorted", undistorted)
cv2.waitKey(0)
cv2.destroyAllWindows()``````

### Applying Distortion Correction on Video Streams

To apply distortion correction on video streams, we need to calibrate the camera once and then perform undistortion in each frame of the stream. By continuously feeding the frames through the undistortion process, we can visualize a corrected video stream.

``````# Sample code for video stream distortion correction

import cv2

# Calibrated camera parameters
camera_matrix = [...]  # Calibrated camera matrix
distortion_coeffs = [...]  # Distortion coefficients

# Open video stream
cap = cv2.VideoCapture(0)

while True:

# Distortion correction
undistorted = cv2.undistort(frame, camera_matrix, distortion_coeffs)

# Display original and undistorted frames
cv2.imshow("Original", frame)
cv2.imshow("Undistorted", undistorted)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()``````

## Conclusion

Estimating camera parameters and correcting distortion are essential processes in computer vision applications. With OpenCV and Python, camera calibration and distortion correction become straightforward tasks. By utilizing functions like `findChessboardCorners()`, `calibrateCamera()`, and `undistort()`, we can accurately estimate camera parameters, correct distortion, and produce visually accurate images and video streams. OpenCV's comprehensive documentation and ease of use make it an excellent choice for camera calibration and distortion correction in Python-based computer vision projects.