Skip to content

Commit bff8170

Browse files
Code 1.0
1 parent 13a0f45 commit bff8170

34 files changed

+982
-0
lines changed
3.08 KB
Binary file not shown.

__pycache__/lane.cpython-35.pyc

14.3 KB
Binary file not shown.
13.1 KB
Binary file not shown.
2.62 KB
Binary file not shown.
9.48 KB
Binary file not shown.

calibration.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""calibration.py: Calibration the cameras and save the calibration results."""
2+
3+
__author__ = "Junsheng Fu"
4+
__email__ = "[email protected]"
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)

calibration_pickle.p

333 Bytes
Binary file not shown.

clf_pickle_all_v1.p

266 KB
Binary file not shown.

examples/.DS_Store

6 KB
Binary file not shown.

examples/000275.png

814 KB
Loading

examples/25.png

9.32 KB
Loading

examples/31.png

8.02 KB
Loading

examples/53.png

7.77 KB
Loading

examples/car_not_car.png

60.7 KB
Loading

examples/car_sample.png

9.32 KB
Loading

examples/demo1.gif

15.3 MB
Loading

examples/demo2.gif

12.3 MB
Loading

examples/hog_2.png

54.3 KB
Loading

examples/project_YOLO.mp4

632 KB
Binary file not shown.

examples/project_video.mp4

24.1 MB
Binary file not shown.

examples/test1.jpg

212 KB
Loading

examples/test2.jpg

170 KB
Loading

examples/test3.jpg

144 KB
Loading

examples/test4.jpg

196 KB
Loading

examples/test5.jpg

238 KB
Loading

examples/test6.jpg

227 KB
Loading

examples/yolo1.png

646 KB
Loading

examples/yolo2.png

495 KB
Loading

examples/yolo_1.png

1.25 MB
Loading

0 commit comments

Comments
 (0)