# Camera calibrate by openCV >There have two kinds of distortion that offen occurs. First is radio distortion and the other is tangential distortion. The distortion occurs when the camera is not aligned parallel to the image plane. We have to find five parameters. ### Distortion coefficients: k~1~, k~2~, p~1~, p~2~, k~3~ >And we also have to get the intrinsic and extrinsic parameters of camera. ### Intrinsic parameters: focal lengh(f~x~, f~y~) and potical centers(c~x~, c~y~) ### Extrinsic parameters corresponds to rotation and translation vectors which translates a corrdinates of a 3D point to a corrdinate system. --- These parameters can be used to create a camera matrix which can be used to remove distortion due to the lenses of a specific cameara. Prepare at least 10 test patterns which can use chessboard. --- ### **camera matrix =** \begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \\ \end{bmatrix} --- The important input data needed for calibration of the camera is the set of 3D real world points and the corresponding 2D coordinates of these points in the image. 2D is easy to get but 3D we need to know(X, Y, Z) values. We can consider Z=0 always because chessboard image keep stationary at XY plane. We can easily point **(0,0),(1,0),(2,0)**, ...which denote the location of points. If the **size of block of chessboard = 20mm**, we can pass the values as **(0,0),(20,0),(40,0)**, ... . If we don't know the square size, we pass in terms of square size. 3D points are called **object points** and 2D points are called **image points**. --- Use follow function to get the chessboard corners `cv2.findChessboardCorners()` Once we find the corners, we can increase their accuracy using `cv2.cornerSubPix()` ```clike= import numpy as np import cv2 import glob # termination criteria criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001) # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) objp = np.zeros((6*7,3), np.float32) objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) # Arrays to store object points and image points from all the images. objpoints = [] # 3d point in real world space imgpoints = [] # 2d points in image plane. images = glob.glob('*.jpg') for fname in images: img = cv.imread(fname) gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Find the chess board corners ret, corners = cv.findChessboardCorners(gray, (10,7), None) # If found, add object points, image points (after refining them) if ret == True: objpoints.append(objp) corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria) imgpoints.append(corners) # Draw and display the corners cv.drawChessboardCorners(img, (7,6), corners2, ret) cv.imshow('img', img) cv.waitKey(500) cv.destroyAllWindows() ``` --- We can get such like this pattern. >Note: this chessboard is 6*9 ![](https://i.imgur.com/K7F9hik.png) --- ### Calibration Now we can use follow function to calibrate camera `cv2.calibrateCamera()` `ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)`