[파이썬] opencv-python 카메라 캘리브레이션

OpenCV-Python is a popular computer vision library that provides various functions for image and video processing. One of the useful features of OpenCV-Python is camera calibration, which helps in improving the accuracy of camera measurements and removing distortion from images.

In this blog post, we will explore how to perform camera calibration using OpenCV-Python.

What is camera calibration?

Camera calibration is the process of estimating the parameters of a camera that are necessary to map 3D world points to their corresponding 2D image coordinates. These parameters include:

Camera calibration is important in various computer vision tasks, such as object tracking, 3D reconstruction, and augmented reality.

Camera calibration setup

To perform camera calibration, you will need:

  1. A chessboard pattern or a calibration target with known dimensions.
  2. A camera to capture images of the calibration target.
  3. OpenCV-Python installed in your Python environment.

Installing OpenCV-Python

You can install OpenCV-Python using pip, by running the following command:

pip install opencv-python

Capturing calibration images

The first step in camera calibration is capturing images of the calibration target from different orientations and distances. Make sure to cover the entire field of view and include a wide range of angles.

Here’s an example code snippet to capture calibration images using OpenCV-Python:

import cv2

# Capture images from camera
cap = cv2.VideoCapture(0)

# Set video frame resolution
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Number of calibration images to capture
num_images = 10
calibration_images = []

# Capture calibration images
for i in range(num_images):
    ret, frame = cap.read()
    calibration_images.append(frame)
    cv2.imshow("Calibration Image", frame)
    cv2.waitKey(0)

# Release video capture
cap.release()
cv2.destroyAllWindows()

In the above code, we use cv2.VideoCapture to capture frames from the default camera (index 0). We set the frame resolution using cap.set to ensure consistent image sizes. We capture a specified number of calibration images and store them in a list.

Performing camera calibration

Once you have captured the calibration images, the next step is to perform camera calibration using OpenCV-Python. OpenCV provides a function called cv2.calibrateCamera() to estimate the camera parameters.

Here’s an example code snippet to perform camera calibration using OpenCV-Python:

import cv2
import numpy as np

# Chessboard dimensions
rows = 6
cols = 9

# Chessboard square size (in meters)
square_size = 0.025

# Prepare object points (0,0,0), (1,0,0), ...
object_points = np.zeros((rows * cols, 3), np.float32)
object_points[:, :2] = np.mgrid[0:cols, 0:rows].T.reshape(-1, 2)
object_points *= square_size

# Arrays to store object points and image points from all the calibration images
obj_points = []
img_points = []

# Load calibration images
for image in calibration_images:
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(gray, (cols, rows), None)

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

# Perform camera calibration
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, gray.shape[::-1], None, None)

# Print camera matrix and distortion coefficients
print("Camera Matrix:")
print(mtx)
print("\nDistortion Coefficients:")
print(dist)

In the above code, we define the chessboard dimensions and the size of each square. We then prepare object points representing the coordinates of the corners of the chessboard. We iterate over the captured calibration images, convert them to grayscale, and use cv2.findChessboardCorners() to find the corners of the chessboard in the image.

If the corners are successfully found, we append the object points and image points to their respective arrays. Finally, we call cv2.calibrateCamera() to estimate the camera matrix, distortion coefficients, rotation vectors, and translation vectors.

Conclusion

Camera calibration is a crucial step in computer vision applications that involve camera measurements. OpenCV-Python provides convenient functions for camera calibration, making it easier to remove distortion and improve the accuracy of camera imaging.

In this blog post, we explored the basics of camera calibration using OpenCV-Python. Remember to capture a sufficient number of calibration images and provide accurate calibration target dimensions to obtain accurate results.

Using the code examples provided, you can now perform camera calibration using OpenCV-Python and enhance the accuracy of your computer vision applications.