-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_calibration.py
More file actions
114 lines (90 loc) · 3.83 KB
/
camera_calibration.py
File metadata and controls
114 lines (90 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import argparse
import glob
import os
import pickle
import cv2
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("Qt5Agg")
import numpy as np
IMAGE_SHAPE_X = 1200
IMAGE_SHAPE_Y = 720
NUM_X_CORNERS = 9
NUM_Y_CORNERS = 6
def calibrate_camera(visualize=False, save_examples=False):
# prepare object points, like (0,0,0), (1,0,0), ....,(6,5,0)
objp = np.zeros((NUM_X_CORNERS * NUM_Y_CORNERS, 3), np.float32)
objp[:,:2] = np.mgrid[:NUM_X_CORNERS, :NUM_Y_CORNERS].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.
# Make a list of calibration images
images = glob.glob('./camera_cal/calibration*.jpg')
# Step through the list and search for chessboard corners
for idx, fname in enumerate(images):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (NUM_X_CORNERS, NUM_Y_CORNERS), None)
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (NUM_X_CORNERS, NUM_Y_CORNERS), corners, ret)
if save_examples:
write_name = './output_images/chessboard' + str(idx) + '.jpg'
cv2.imwrite(write_name, img)
if visualize:
cv2.imshow('chessboard' + str(idx), img)
cv2.waitKey(0)
else:
print("Could not find chessboard corners for {}".format(os.path.basename(fname)))
if visualize:
cv2.destroyAllWindows()
img_size = (IMAGE_SHAPE_X, IMAGE_SHAPE_Y)
# Perform camera calibration given object points and image points
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)
return mtx, dist
def undistort_image(img, params):
mtx = params['mtx']
dist = params['dist']
image = cv2.undistort(img, mtx, dist, None, mtx)
return image
def save_calibration_data(mtx, dist):
dist_pickle = {}
dist_pickle["mtx"] = mtx
dist_pickle["dist"] = dist
pickle.dump(dist_pickle, open("./calibration.p", "wb"))
def load_calibration_data():
params = pickle.load(open("./calibration.p", mode="rb"))
return params
def show_undistort(image_file, visualize=False, save_examples=False):
# Test undistortion on an image
img = cv2.imread(image_file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
params = load_calibration_data()
dst = undistort_image(img, params)
# Visualize undistortion
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7))
ax1.imshow(img)
ax1.set_title('Original Image', fontsize=24)
ax2.imshow(dst)
ax2.set_title('Undistorted Image', fontsize=24)
if visualize:
plt.show(block=True)
if save_examples:
save_file_name = "undistorted_{}".format(os.path.basename(image_file.replace(".jpg", ".png")))
save_location = "./output_images/{}".format(save_file_name)
f.savefig(save_location, bbox_inches="tight")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Camera calibration")
parser.add_argument("-show", action="store_true", help="Visualize data calibration")
parser.add_argument("-save", action="store_true", help="Save calibration images")
results = parser.parse_args()
visualize = bool(results.show)
save_examples = bool(results.save)
mtx, dist = calibrate_camera(visualize, save_examples)
save_calibration_data(mtx, dist)
show_undistort("./camera_cal/calibration2.jpg", visualize, save_examples)
show_undistort("./test_images/signs_vehicles_xygrad.png", visualize, save_examples)