-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
230 lines (187 loc) · 7.18 KB
/
utils.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import numpy as np
from numpy import linalg as LA
import plotly.graph_objects as go
import plotly.express as px
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
def compute_A(data):
npoints, _ = data.shape
A = np.zeros((npoints*2, 12)) #initialize A matrix
first = 0
sec = 2
for points in data:
spam = np.zeros((2,12))
spam[0,:3] = points[:3]
spam[0,3] = 1
spam[0,8:11] = points[:3]*-(points[3])
spam[0,11] = -points[3]
spam[1,4:7] = points[:3]
spam[1,7] = 1
spam[1,8:11] = points[:3]*-(points[4])
spam[1,11] = -points[4]
A[first:sec, :] = spam
first = sec
sec = sec+2
return A
def compute_projection_matrix(A):
U, s, VT = np.linalg.svd(A)
V = VT.T
mask = np.zeros((V.shape[0],1))
mask[-1] = 1
projection = np.dot(V,mask)
return projection
def compute_projection_matrix_hardcode(A):
AtA = np.dot(A.T,A)
eigenvalue, eigenvector = LA.eig(AtA)
pick = np.argmin(eigenvalue)
mask = np.zeros((eigenvector.shape[0],1))
mask[pick] = 1
projection = np.dot(eigenvector,mask)
return projection
def compute_parameters(projection_matrix):
Q = projection_matrix[:,:3]
b = projection_matrix[:,-1]
b = b.reshape(3,1)
translation = np.dot((LA.inv(-Q)),b)
Qinv = np.linalg.inv(Q)
Rt,Kinv = np.linalg.qr(Qinv)
intrinsic = np.linalg.inv(Kinv)
intrinsic_n = intrinsic/intrinsic[2,2]
#intrinsic_n[0,1] = 0
rotation = np.transpose(Rt)
return abs(intrinsic_n), rotation, translation
def reprojection_error(projection_matrix, rotation, translation, data):
Q = projection_matrix[:,:3]
Qinv = np.linalg.inv(Q)
Rt,Kinv = np.linalg.qr(Qinv)
intrinsic = np.linalg.inv(Kinv)
f=np.dot(intrinsic, rotation)
s = np.dot(-Q,translation)
pcon = np.hstack((f,s))
wp = data[:,:4].copy()
wp[:,-1] = 1
image_points = np.dot(pcon, wp.T).T
image_points = image_points/image_points[:,2].reshape(-1,1)
print("\nProjected points are:")
print(image_points[:,:2])
error = abs(image_points[:,:2] - data[:,3:])
return np.mean(error, axis=0)
def visualize_extrinsics(rotation, translation):
print("\nVisualizing extrinsics now!, computing camera location...")
xs,ys,zs = [],[],[]
count=0
for rotation,translation in zip(rotation, translation):
mag = 50
corners = np.array([
[-mag,mag,0],
[mag,mag,0],
[mag,-mag,0],
[-mag,-mag,0],
[-mag,mag,0],
[0,0,mag],
[-mag,-mag,0],
[0,0,mag],
[mag,mag,0],
[0,0,mag],
[mag,-mag,0],
[0,0,mag],
[0,0,0],
[mag,0,0],
[0,0,0],
[0,-mag,0],
[0,0,0],
])
#print(corners)
camera_points = np.dot(corners,rotation)+translation.reshape(1,3) #this give coordinates of camera in world coordinate
camera_points = np.vstack((camera_points, np.array([0,0,0])))
print("\ncamera corners in world coordinates are:\n",camera_points)
'''
print(np.dot(camera_points-translation.reshape(1,3),rotation.T)) #this is to check camera coordinates
'''
x = camera_points[:,0]
y = camera_points[:,1]
z = camera_points[:,2]
xs.append(x)
ys.append(y)
zs.append(z)
fig = go.Figure(data=go.Scatter3d(x=xs[count], y=ys[count], z=zs[count],
#mode='markers',
marker=dict(
size=4,
#color=1,
#colorscale='Viridis',
),
line=dict(
color='darkblue',
width=2
)
))
count +=1
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-10,500],),
yaxis = dict(nticks=4, range=[-10,500],),
zaxis = dict(nticks=4, range=[-10,500],),),
margin=dict(r=20, l=10, b=10, t=10))
fig.show()
def visualize_extrinsics_matplot(rotation, translation):
print("\nVisualizing extrinsics now!, computing camera location...")
fig = pyplot.figure()
ax = Axes3D(fig)
count=1
for rotation,translation in zip(rotation, translation):
mag = 50
'''
corners = np.array([[0,0,0],
[mag,0,0],
[-mag,0,0],
[0,mag,0],
[0,-mag,0],
[0,0,mag],
[mag,mag,0],
[-mag,mag,0],
[mag,-mag,0],
[-mag,-mag,0]])
'''
corners = np.array([
[-mag,mag,0],
[mag,mag,0],
[mag,-mag,0],
[-mag,-mag,0],
[-mag,mag,0],
[0,0,mag],
[-mag,-mag,0],
[0,0,mag],
[mag,mag,0],
[0,0,mag],
[mag,-mag,0],
[0,0,mag],
[0,0,0],
[mag,0,0],
[0,0,0],
[0,-mag,0],
[0,0,0],
])
#print(corners)
camera_points = np.dot(corners,rotation)+translation.reshape(1,3) #this give coordinates of camera in world coordinate
camera_points = np.vstack((camera_points, np.array([0,0,0])))
print("\ncamera corners in world coordinates are:\n",camera_points)
'''
print(np.dot(camera_points-translation.reshape(1,3),rotation.T)) #this is to check camera coordinates
'''
x = camera_points[:,0]
y = camera_points[:,1]
z = camera_points[:,2]
ax.scatter(x,y,z, color='black', depthshade=False, s=6)
ax.text(camera_points[5,0], camera_points[5,1], camera_points[5,2], str(count),fontsize=20, color='darkblue')
ax.set_xlim([-10,500])
ax.set_ylim([-10,500])
ax.set_zlim([-10,500])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_aspect('equal','box')
ax.plot(x, y, z, color='green')
ax.invert_yaxis()
count+=1
return