|
| 1 | +"""calibration.py: Calibration the cameras and save the calibration results.""" |
| 2 | + |
| 3 | +__author__ = "Junsheng Fu" |
| 4 | + |
| 5 | +__date__ = "March 2017" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import cv2 |
| 9 | +import glob |
| 10 | +import pickle |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from os import path |
| 13 | + |
| 14 | + |
| 15 | +def calibrate_camera(nx, ny, basepath): |
| 16 | + """ |
| 17 | +
|
| 18 | + :param nx: number of grids in x axis |
| 19 | + :param ny: number of grids in y axis |
| 20 | + :param basepath: path contains the calibration images |
| 21 | + :return: write calibration file into basepath as calibration_pickle.p |
| 22 | + """ |
| 23 | + |
| 24 | + objp = np.zeros((nx*ny,3), np.float32) |
| 25 | + objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2) |
| 26 | + |
| 27 | + # Arrays to store object points and image points from all the images. |
| 28 | + objpoints = [] # 3d points in real world space |
| 29 | + imgpoints = [] # 2d points in image plane. |
| 30 | + |
| 31 | + # Make a list of calibration images |
| 32 | + images = glob.glob(path.join(basepath, 'calibration*.jpg')) |
| 33 | + |
| 34 | + # Step through the list and search for chessboard corners |
| 35 | + for fname in images: |
| 36 | + img = cv2.imread(fname) |
| 37 | + gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) |
| 38 | + |
| 39 | + # Find the chessboard corners |
| 40 | + ret, corners = cv2.findChessboardCorners(gray, (nx,ny),None) |
| 41 | + |
| 42 | + # If found, add object points, image points |
| 43 | + if ret == True: |
| 44 | + objpoints.append(objp) |
| 45 | + imgpoints.append(corners) |
| 46 | + |
| 47 | + # Draw and display the corners |
| 48 | + img = cv2.drawChessboardCorners(img, (nx,ny), corners, ret) |
| 49 | + cv2.imshow('input image',img) |
| 50 | + cv2.waitKey(500) |
| 51 | + |
| 52 | + cv2.destroyAllWindows() |
| 53 | + |
| 54 | + |
| 55 | + # calibrate the camera |
| 56 | + img_size = (img.shape[1], img.shape[0]) |
| 57 | + ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None) |
| 58 | + |
| 59 | + # Save the camera calibration result for later use (we don't use rvecs / tvecs) |
| 60 | + dist_pickle = {} |
| 61 | + dist_pickle["mtx"] = mtx |
| 62 | + dist_pickle["dist"] = dist |
| 63 | + destnation = path.join(basepath,'calibration_pickle.p') |
| 64 | + pickle.dump( dist_pickle, open( destnation, "wb" ) ) |
| 65 | + print("calibration data is written into: {}".format(destnation)) |
| 66 | + |
| 67 | + return mtx, dist |
| 68 | + |
| 69 | + |
| 70 | +def load_calibration(calib_file): |
| 71 | + """ |
| 72 | +
|
| 73 | + :param calib_file: |
| 74 | + :return: mtx and dist |
| 75 | + """ |
| 76 | + with open(calib_file, 'rb') as file: |
| 77 | + # print('load calibration data') |
| 78 | + data= pickle.load(file) |
| 79 | + mtx = data['mtx'] # calibration matrix |
| 80 | + dist = data['dist'] # distortion coefficients |
| 81 | + |
| 82 | + return mtx, dist |
| 83 | + |
| 84 | + |
| 85 | +def undistort_image(imagepath, calib_file, visulization_flag): |
| 86 | + """ undistort the image and visualization |
| 87 | +
|
| 88 | + :param imagepath: image path |
| 89 | + :param calib_file: includes calibration matrix and distortion coefficients |
| 90 | + :param visulization_flag: flag to plot the image |
| 91 | + :return: none |
| 92 | + """ |
| 93 | + mtx, dist = load_calibration(calib_file) |
| 94 | + |
| 95 | + img = cv2.imread(imagepath) |
| 96 | + |
| 97 | + # undistort the image |
| 98 | + img_undist = cv2.undistort(img, mtx, dist, None, mtx) |
| 99 | + img_undistRGB = cv2.cvtColor(img_undist, cv2.COLOR_BGR2RGB) |
| 100 | + |
| 101 | + if visulization_flag: |
| 102 | + imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 103 | + f, (ax1, ax2) = plt.subplots(1, 2) |
| 104 | + ax1.imshow(imgRGB) |
| 105 | + ax1.set_title('Original Image', fontsize=30) |
| 106 | + ax1.axis('off') |
| 107 | + ax2.imshow(img_undistRGB) |
| 108 | + ax2.set_title('Undistorted Image', fontsize=30) |
| 109 | + ax2.axis('off') |
| 110 | + plt.show() |
| 111 | + |
| 112 | + return img_undistRGB |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + |
| 117 | + nx, ny = 9, 6 # number of grids along x and y axis in the chessboard pattern |
| 118 | + basepath = 'camera_cal/' # path contain the calibration images |
| 119 | + |
| 120 | + # calibrate the camera and save the calibration data |
| 121 | + calibrate_camera(nx, ny, basepath) |
0 commit comments