[PYTHON] Visually understand camera calibration

Camera calibration is used to correct lens distortion, but it seems difficult to understand how each coefficient affects it.

So I will try to make it intuitive.

What is camera calibration?

Unless it is an ideal pinhole camera model, the image will be distorted as shown on the left. By finding the distortion coefficient unique to the camera, it is possible to correct such a distorted image.

image.png

How camera calibration works

When you calibrate your camera, you print something called a checkerboard. image.png

Take dozens of photos from all angles with the camera whose parameters you want to measure.

image.png

Now you can match the 2D coordinate $ x $ of the camera with the 3D coordinate $ X $ of space.

x = P X

This $ P $ is the camera matrix. It can be calculated by simultaneous equations with 3 rows and 4 columns.

P = K [R|t]

$ P $ can be decomposed into the upper triangular matrix $ K $ and the orthonormal matrix $ R $,

$ K $ ...... Internal Parameter, Intrinsic Parameter (focal length, optical center, shear coefficient) $ [R | t] $ ... External parameter, Extrinsic Parameter (Camera rotation and conversion)

It will be.

Internal parameters

This internal parameter $ K $

K = \begin{pmatrix}
f_x & s & c_x \\
0 & f_y & c_y \\
0 & 0 & 1 \\
\end{pmatrix}

It represents the focal length $ (f_x, f_y) $, the optical center $ (c_x, c_y) $, and the shear coefficient $ s $.

Implementation

In Python, you can use OpenCV to find $ K $ (mtx) as follows.

#Detect points from checkerboard
ret, corners = cv2.findChessboardCorners(gray, (8,6), None)

#Calibration
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size,None,None)

#Distortion correction
dst = cv2.undistort(img, mtx, dist, None, newMtx)

Intuitive understanding

Let's animate parameters such as focal length, optical center, and shear coefficient, respectively. Only the target parameters are changed from the obtained camera matrix.

The original image

test_image.jpg

Focal length

Infinity → Appropriate focal length → Small

(1) f_x, f_y cxcy.gif

(2) f_x fx.gif

(3) f_y fy.gif

Optical center

Negative → proper position → positive

(1) c_x cx.gif

(1) c_y cy.gif

Shear coefficient

Negative → proper value → positive s.gif In English it is Skew.

Summary

How was that. It turns out that camera calibration is used to correct distortion.

Future work

I also want to do the strain coefficient in the radial direction and the strain coefficient in the circumferential direction.

Image quote

https://github.com/DavidWangWood/Camera-Calibration-Python https://jp.mathworks.com/help/vision/examples/evaluating-the-accuracy-of-single-camera-calibration.html

Recommended Posts

Visually understand camera calibration
Camera calibration
Use camera calibration file with OpenCvSharp4
Make a chessboard pattern for camera calibration