摄像头Camera 标定Calibration原理Theory
摄像头Camera 标定Calibration原理Theory
cv2.cameraCalibration
Pinhole camera calibration calls camera vision from 3D objects in the real world and transforms them into a 2D image.
摄像头标定这里指利用常规(小孔成像)摄像头观察真实三位物体对二维图像的矫正转换。
We named points from objects as objects point and points in image as image points.
3D的点被叫做物体点,而2D的图像点被叫做图像点。
To calibrate camera, we need to undistort radial distortion and tangential distortion.
Radial Distortion: Radial Distortion is the most common type that affects the images, In which when a camera captured pictures of straight lines appeared slightly curved or bent.
Tangential distortion: Tangential distortion occurs mainly because the lens is not parallely aligned to the imaging plane, that makes the image to be extended a little while longer or tilted, it makes the objects appear farther away or even closer than they actually are.
摄像头标定矫正主要解决径向畸变和切向畸变。
径向畸变是图像像素点以畸变中心为中心点,沿着径向产生的位置偏差,从而导致图像中所成的像发生形变。
图像径向畸变是成像过程中最主要的畸变,同时也是对成像效果影响最大的畸变,广角或者鱼眼的畸变效果,
矫正算法采用多项式拟合:
切向畸变,这是由于透镜与成像平面不可能绝对平行造成的。
这种畸变会造成图像中的某些点看上去的位置会比我们认为的位置要近一些。
Five Distortion Coefficients 五个畸变系数:
In math, the Transformation from 3D object points, P of X, Y and Z to X and Y is done by a transformative matrix called the camera matrix(C), we’ll be using this to calibrate the camera.
max = 摄像机矩阵 = 摄像机的内部参数(焦距和光学中心)
内部参数是摄像机本身具有的,包括的信息有焦距(f x ,f y ),光学中心(c x ,c y )。
dist = 外部参数(旋转和变换向量)
外部参数与旋转和变换向量相对应,它可以将 3D 点的坐标转换到坐标系统中。
It’s recommended to use at least 20 images to get a reliable calibration, For this, we have a lot of images here, each chess board has eight by six corners to detect
于是必须要提供一些包含明显图案模式的样本棋盘图片,一般至少需要 10 个(部分中文资料)建议20个这样的图案模式。
or
where
(X, Y, Z) are the coordinates of a 3D point in the world coordinate space
(u, v) are the coordinates of the projection point in pixels
A is a camera matrix, or a matrix of intrinsic parameters
(cx, cy) is a principal point that is usually at the image center
fx, fy are the focal lengths expressed in pixel units.
A chessboard is great for calibration because it's regular, high contrast pattern makes it easy to detect automatically. And we know how an undistorted flat chessboard looks like. So, if we use our camera to take pictures of Chessboard at different angles
棋盘主要是通过角点来计算畸变,
import numpy as np
import cv2, glob
calibrate_source_path = './data/camera_cal/*.jpg'
calibrate_test_path = './data/test_image.jpg'
# define
objpoints = []
imgpoints = []
nx = 8
ny = 6
objp = np.zeros((ny*nx,3),np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2)
# chessboard
for path in glob.glob(calibrate_source_path):
gray = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2GRAY)
ret,corners = cv2.findChessboardCorners(gray,(nx,ny))
if ret:
objpoints.append(objp)
imgpoints.append(corners);
# gray = cv2.drawChessboardCorners(gray, (nx,ny), corners, ret)
# execute
img = cv2.imread(calibrate_test_path)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img.shape[1:], None, None)
dst = cv2.undistort(img, mtx, dist, None, mtx)
# display
cv2.imshow(None, cv2.pyrDown(np.hstack((img,dst))))
cv2.waitKey(0); cv2.destroyAllWindows()