def calibrate_camera(calibration_images, nx, ny):
"""Get the camera calibration parameters
**Note:** this code is based on https://github.com/udacity/CarND-Camera-Calibration/blob/master/camera_calibration.ipynb
Parameters
----------
calibration_images : list
a list of images OR image filepaths
nx : int
the number of interior corners in the x direction
ny : int
the number of interior corners in the y direction
Returns
-------
mtx : numpy.ndarray
3x3 floating-point camera matrix
dist : numpy.ndarray
vector of distortion coefficients: ``(k_1, k_2, p_1, p_2, k_3)``
"""
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((nx*ny,3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
for i, img in enumerate(calibration_images):
# if necessary, load the image
if isinstance(img, str):
img = cv2.imread(img)
# get the shape of the images
if i == 0:
img_size = img.shape[1::-1]
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# get the camera calibration parameters
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)
return mtx, dist