-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp2_4.py
134 lines (107 loc) · 3.66 KB
/
temp2_4.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import cv2
import numpy as np
import glob
'''left camera calibration'''
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
objp_s =[]
imgpl_s=[]
images = glob.glob('.//left//*.jpg')
#print(images)
flag = 0
cnt = 0
for fname in images:
img = cv2.imread(fname)
#print(img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#print(gray)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
#print(corners)
# If found, add object points, image points (after refining them)
if ret == True:
cnt = cnt+1
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
if flag < 7:
objp_s.append(objp)
flag = flag+1
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)
# cv2.imshow('img',img)
# cv2.waitKey()
#cv2.destroyAllWindows()
#prepare image points of left folder for Stereo Calibration
imgpl_s.append(imgpoints[0]) #left01
imgpl_s.append(imgpoints[2]) #left03
imgpl_s.append(imgpoints[3]) #left04
imgpl_s.append(imgpoints[5]) #left06
imgpl_s.append(imgpoints[6]) #left07
imgpl_s.append(imgpoints[8]) #left12
imgpl_s.append(imgpoints[9]) #left13
#imgpl_s.append(imgpoints[10]) #left14
l_ret, l_mtx, l_dist, l_rvecs, l_tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
#print(gray.shape[::-1])
print("Left Camera Matrix:\n")
print(l_mtx)
print("Left Distortion Coefficients:\n")
print(l_dist)
'''right camera calibration'''
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints_r = [] # 2d points in image plane.
imgpr_s = []
images = glob.glob('.//right//*.jpg')
#print(images)
flag = 0
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints_r.append(corners2)
img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)
# cv2.imshow('img',img)
# cv2.waitKey()
#cv2.destroyAllWindows()
#prepare image points of right folder for Stereo Calibration
imgpr_s.append(imgpoints_r[0]) #right01
imgpr_s.append(imgpoints_r[1]) #right03
imgpr_s.append(imgpoints_r[2]) #right04
imgpr_s.append(imgpoints_r[3]) #right06
imgpr_s.append(imgpoints_r[4]) #right07
imgpr_s.append(imgpoints_r[7]) #right12
imgpr_s.append(imgpoints_r[8]) #right13
#imgpr_s.append(imgpoints_r[9]) #right14
r_ret, r_mtx, r_dist, r_rvecs, r_tvecs = cv2.calibrateCamera(objpoints, imgpoints_r, gray.shape[::-1],None,None)
print("Right Camera Matrix:\n")
print(r_mtx)
print("Right Distortion Coefficients:\n")
print(r_dist)
#Stereo Calibrate
ANS=cv2.stereoCalibrate(objp_s,imgpl_s,imgpr_s,l_mtx,l_dist,r_mtx,r_dist,gray.shape[::-1])
R = ANS[5]
T = ANS[6]
E = ANS[7]
F = ANS[8]
#print(ANS)
print("R:\n")
print(R)
print("T:\n")
print(T)
print("E:\n")
print(E)
print("F:\n")
print(F)