-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate.py
93 lines (77 loc) · 2.36 KB
/
calibrate.py
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
#
# To run:
#
# Install opencv modules in Anaconda environment:
#
# conda install opencv
# pip install --upgrade pip
# pip install opencv-contrib-python
#
# Run calibrate.py from the commandline:
#
# python calibrate.py
import pickle
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
# file names, modify as necessary
calibimgfiles = 'C:/Users/Alex/Desktop/CS117/project/calib_jpg_u/*.jpg'
resultfile = 'calibration.pickle'
# checkerboard coordinates in 3D
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = 2.8*np.mgrid[0:8, 0:6].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(calibimgfiles)
# Step through the list and search for chessboard corners
for idx, fname in enumerate(images):
img = cv2.imread(fname)
img_size = (img.shape[1], img.shape[0])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (8,6), None)
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# Display image with the corners overlayed
cv2.drawChessboardCorners(img, (8,6), corners, ret)
cv2.imshow('img', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
# now perform the calibration
ret, K, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size,None,None)
print("Estimated camera intrinsic parameter matrix K")
print(K)
print("Estimated radial distortion coefficients")
print(dist)
print("Individual intrinsic parameters")
print("fx = ",K[0][0])
print("fy = ",K[1][1])
print("cx = ",K[0][2])
print("cy = ",K[1][2])
# save the results out to a file for later use
calib = {}
calib["fx"] = K[0][0]
calib["fy"] = K[1][1]
calib["cx"] = K[0][2]
calib["cy"] = K[1][2]
calib["dist"] = dist
fid = open(resultfile, "wb" )
pickle.dump(calib,fid)
fid.close()
#
# optionally go through and remove radial distortion from a set of images
#
#images = glob.glob(calibimgfiles)
#for idx, fname in enumerate(images):
# img = cv2.imread(fname)
# img_size = (img.shape[1], img.shape[0])
#
# dst = cv2.undistort(img, K, dist, None, K)
# udfname = fname+'undistort.jpg'
# cv2.imwrite(udfname,dst)
#