forked from pacogarcia3/hta0-horizontal-robot-arm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial_perspective_calibration.py
200 lines (149 loc) · 5.11 KB
/
initial_perspective_calibration.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#https://docs.opencv.org/3.3.0/dc/dbb/tutorial_py_calibration.html
import numpy as np
import cv2
import glob
import camera_realworldxyz
cameraXYZ=camera_realworldxyz.camera_realtimeXYZ()
calculatefromCam=True
imgdir="/home/pi/Desktop/Captures/"
writeValues=False
#test camera calibration against all points, calculating XYZ
#load camera calibration
savedir="camera_data/"
cam_mtx=np.load(savedir+'cam_mtx.npy')
dist=np.load(savedir+'dist.npy')
newcam_mtx=np.load(savedir+'newcam_mtx.npy')
roi=np.load(savedir+'roi.npy')
#load center points from New Camera matrix
cx=newcam_mtx[0,2]
cy=newcam_mtx[1,2]
fx=newcam_mtx[0,0]
print("cx: "+str(cx)+",cy "+str(cy)+",fx "+str(fx))
#MANUALLY INPUT YOUR MEASURED POINTS HERE
#ENTER (X,Y,d*)
#d* is the distance from your point to the camera lens. (d* = Z for the camera center)
#we will calculate Z in the next steps after extracting the new_cam matrix
#world center + 9 world points
total_points_used=10
X_center=10.9
Y_center=10.7
Z_center=43.4
worldPoints=np.array([[X_center,Y_center,Z_center],
[5.5,3.9,46.8],
[14.2,3.9,47.0],
[22.8,3.9,47.4],
[5.5,10.6,44.2],
[14.2,10.6,43.8],
[22.8,10.6,44.8],
[5.5,17.3,43],
[14.2,17.3,42.5],
[22.8,17.3,44.4]], dtype=np.float32)
#MANUALLY INPUT THE DETECTED IMAGE COORDINATES HERE
#[u,v] center + 9 Image points
imagePoints=np.array([[cx,cy],
[502,185],
[700,197],
[894,208],
[491,331],
[695,342],
[896,353],
[478,487],
[691,497],
[900,508]], dtype=np.float32)
#FOR REAL WORLD POINTS, CALCULATE Z from d*
for i in range(1,total_points_used):
#start from 1, given for center Z=d*
#to center of camera
wX=worldPoints[i,0]-X_center
wY=worldPoints[i,1]-Y_center
wd=worldPoints[i,2]
d1=np.sqrt(np.square(wX)+np.square(wY))
wZ=np.sqrt(np.square(wd)-np.square(d1))
worldPoints[i,2]=wZ
print(worldPoints)
#print(ret)
print("Camera Matrix")
print(cam_mtx)
print("Distortion Coeff")
print(dist)
print("Region of Interest")
print(roi)
print("New Camera Matrix")
print(newcam_mtx)
inverse_newcam_mtx = np.linalg.inv(newcam_mtx)
print("Inverse New Camera Matrix")
print(inverse_newcam_mtx)
if writeValues==True: np.save(savedir+'inverse_newcam_mtx.npy', inverse_newcam_mtx)
print(">==> Calibration Loaded")
print("solvePNP")
ret, rvec1, tvec1=cv2.solvePnP(worldPoints,imagePoints,newcam_mtx,dist)
print("pnp rvec1 - Rotation")
print(rvec1)
if writeValues==True: np.save(savedir+'rvec1.npy', rvec1)
print("pnp tvec1 - Translation")
print(tvec1)
if writeValues==True: np.save(savedir+'tvec1.npy', tvec1)
print("R - rodrigues vecs")
R_mtx, jac=cv2.Rodrigues(rvec1)
print(R_mtx)
if writeValues==True: np.save(savedir+'R_mtx.npy', R_mtx)
print("R|t - Extrinsic Matrix")
Rt=np.column_stack((R_mtx,tvec1))
print(Rt)
if writeValues==True: np.save(savedir+'Rt.npy', Rt)
print("newCamMtx*R|t - Projection Matrix")
P_mtx=newcam_mtx.dot(Rt)
print(P_mtx)
if writeValues==True: np.save(savedir+'P_mtx.npy', P_mtx)
#[XYZ1]
#LETS CHECK THE ACCURACY HERE
s_arr=np.array([0], dtype=np.float32)
s_describe=np.array([0,0,0,0,0,0,0,0,0,0],dtype=np.float32)
for i in range(0,total_points_used):
print("=======POINT # " + str(i) +" =========================")
print("Forward: From World Points, Find Image Pixel")
XYZ1=np.array([[worldPoints[i,0],worldPoints[i,1],worldPoints[i,2],1]], dtype=np.float32)
XYZ1=XYZ1.T
print("{{-- XYZ1")
print(XYZ1)
suv1=P_mtx.dot(XYZ1)
print("//-- suv1")
print(suv1)
s=suv1[2,0]
uv1=suv1/s
print(">==> uv1 - Image Points")
print(uv1)
print(">==> s - Scaling Factor")
print(s)
s_arr=np.array([s/total_points_used+s_arr[0]], dtype=np.float32)
s_describe[i]=s
if writeValues==True: np.save(savedir+'s_arr.npy', s_arr)
print("Solve: From Image Pixels, find World Points")
uv_1=np.array([[imagePoints[i,0],imagePoints[i,1],1]], dtype=np.float32)
uv_1=uv_1.T
print(">==> uv1")
print(uv_1)
suv_1=s*uv_1
print("//-- suv1")
print(suv_1)
print("get camera coordinates, multiply by inverse Camera Matrix, subtract tvec1")
xyz_c=inverse_newcam_mtx.dot(suv_1)
xyz_c=xyz_c-tvec1
print(" xyz_c")
inverse_R_mtx = np.linalg.inv(R_mtx)
XYZ=inverse_R_mtx.dot(xyz_c)
print("{{-- XYZ")
print(XYZ)
if calculatefromCam==True:
cXYZ=cameraXYZ.calculate_XYZ(imagePoints[i,0],imagePoints[i,1])
print("camXYZ")
print(cXYZ)
s_mean, s_std = np.mean(s_describe), np.std(s_describe)
print(">>>>>>>>>>>>>>>>>>>>> S RESULTS")
print("Mean: "+ str(s_mean))
#print("Average: " + str(s_arr[0]))
print("Std: " + str(s_std))
print(">>>>>> S Error by Point")
for i in range(0,total_points_used):
print("Point "+str(i))
print("S: " +str(s_describe[i])+" Mean: " +str(s_mean) + " Error: " + str(s_describe[i]-s_mean))