-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
203 lines (168 loc) · 5.87 KB
/
helper.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
"""
Homework4.
Helper functions.
Written by Dinesh Reddy, 2020.
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
import submission as sub
from mpl_toolkits.mplot3d import Axes3D
connections_3d = [[0,1], [1,3], [2,3], [2,0], [4,5], [6,7], [8,9], [9,11],
[10,11], [10,8], [0,4], [4,8],
[1,5], [5,9], [2,6], [6,10], [3,7], [7,11]]
color_links = [(255,0,0),(255,0,0),(255,0,0),(255,0,0),(0,0,255),(255,0,255),
(0,255,0),(0,255,0),(0,255,0),(0,255,0),(0,0,255),(0,0,255),
(0,0,255),(0,0,255),(255,0,255),(255,0,255),(255,0,255),
(255,0,255)]
colors = ['blue','blue','blue','blue','red','magenta','green','green','green',
'green','red','red','red','red','magenta','magenta','magenta',
'magenta']
def visualize_keypoints(image, pts, Threshold=None):
'''
plot 2d keypoint
:param image: image
:param car_points: np.array points * 3
'''
import cv2
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
for i in range(12):
cx, cy = pts[i][0:2]
if pts[i][2]>Threshold:
cv2.circle(image,(int(cx),int(cy)),5,(0,255,255),5)
for i in range(len(connections_3d)):
idx0, idx1 = connections_3d[i]
if pts[idx0][2]>Threshold and pts[idx1][2]>Threshold:
x0, y0 = pts[idx0][0:2]
x1, y1 = pts[idx1][0:2]
cv2.line(image, (int(x0), int(y0)), (int(x1), int(y1)),
color_links[i], 2)
while True:
cv2.imshow("sample", image)
if cv2.waitKey(0) == 27:
break
cv2.destroyAllWindows()
return (image)
def plot_3d_keypoint(pts_3d):
'''
plot 3d keypoint
:param car_points: np.array points * 3
'''
fig = plt.figure()
num_points = pts_3d.shape[0]
ax = fig.add_subplot(111, projection='3d')
for j in range(len(connections_3d)):
index0, index1 = connections_3d[j]
xline = [pts_3d[index0,0], pts_3d[index1,0]]
yline = [pts_3d[index0,1], pts_3d[index1,1]]
zline = [pts_3d[index0,2], pts_3d[index1,2]]
ax.plot(xline, yline, zline, color=colors[j])
np.set_printoptions(threshold=1e6, suppress=True)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
def _epipoles(E):
U, S, V = np.linalg.svd(E)
e1 = V[-1, :]
U, S, V = np.linalg.svd(E.T)
e2 = V[-1, :]
return e1, e2
def displayEpipolarF(I1, I2, F):
e1, e2 = _epipoles(F)
sy, sx, _ = I2.shape
f, [ax1, ax2] = plt.subplots(1, 2, figsize=(12, 9))
ax1.imshow(I1)
ax1.set_title('Select a point in this image')
ax1.set_axis_off()
ax2.imshow(I2)
ax2.set_title('Verify that the corresponding point \n is on the epipolar line in this image')
ax2.set_axis_off()
while True:
plt.sca(ax1)
x, y = plt.ginput(1, timeout=3600, mouse_stop=2)[0]
xc = x
yc = y
v = np.array([xc, yc, 1])
l = F.dot(v)
s = np.sqrt(l[0]**2+l[1]**2)
if s==0:
print('Zero line vector in displayEpipolar')
l = l/s
if l[0] != 0:
ye = sy-1
ys = 0
xe = -(l[1] * ye + l[2])/l[0]
xs = -(l[1] * ys + l[2])/l[0]
else:
xe = sx-1
xs = 0
ye = -(l[0] * xe + l[2])/l[1]
ys = -(l[0] * xs + l[2])/l[1]
# plt.plot(x,y, '*', 'MarkerSize', 6, 'LineWidth', 2);
ax1.plot(x, y, '*', markersize=6, linewidth=2)
ax2.plot([xs, xe], [ys, ye], linewidth=2)
plt.draw()
def camera2(E):
U,S,V = np.linalg.svd(E)
m = S[:2].mean()
E = U.dot(np.array([[m,0,0], [0,m,0], [0,0,0]])).dot(V)
U,S,V = np.linalg.svd(E)
W = np.array([[0,-1,0], [1,0,0], [0,0,1]])
if np.linalg.det(U.dot(W).dot(V))<0:
W = -W
M2s = np.zeros([3,4,4])
M2s[:,:,0] = np.concatenate([U.dot(W).dot(V),
U[:,2].reshape([-1, 1])/abs(U[:,2]).max()], axis=1)
M2s[:,:,1] = np.concatenate([U.dot(W).dot(V),
-U[:,2].reshape([-1, 1])/abs(U[:,2]).max()], axis=1)
M2s[:,:,2] = np.concatenate([U.dot(W.T).dot(V),
U[:,2].reshape([-1, 1])/abs(U[:,2]).max()], axis=1)
M2s[:,:,3] = np.concatenate([U.dot(W.T).dot(V),
-U[:,2].reshape([-1, 1])/abs(U[:,2]).max()], axis=1)
return M2s
def epipolarMatchGUI(I1, I2, F):
e1, e2 = _epipoles(F)
sy, sx, _ = I2.shape
f, [ax1, ax2] = plt.subplots(1, 2, figsize=(12, 9))
ax1.imshow(I1)
ax1.set_title('Select a point in this image')
ax1.set_axis_off()
ax2.imshow(I2)
ax2.set_title('Verify that the corresponding point \n is on the epipolar'
' line in this image')
ax2.set_axis_off()
#pts1 = np.zeros((0,2))
#pts2 = np.zeros((0,2))
while True:
plt.sca(ax1)
x, y = plt.ginput(1, mouse_stop=2)[0]
#np.append(pts1, np.array([[x, y]]), axis = 0)
print("x, y: ", x, y)
xc = int(x)
yc = int(y)
v = np.array([xc, yc, 1])
l = F.dot(v)
s = np.sqrt(l[0]**2+l[1]**2)
if s==0:
print('Zero line vector in displayEpipolar')
l = l/s;
if l[0] != 0:
ye = sy-1
ys = 0
xe = -(l[1] * ye + l[2])/l[0]
xs = -(l[1] * ys + l[2])/l[0]
else:
xe = sx-1
xs = 0
ye = -(l[0] * xe + l[2])/l[1]
ys = -(l[0] * xs + l[2])/l[1]
# plt.plot(x,y, '*', 'MarkerSize', 6, 'LineWidth', 2);
ax1.plot(x, y, '*', markersize=6, linewidth=2)
ax2.plot([xs, xe], [ys, ye], linewidth=2)
# draw points
x2, y2 = sub.epipolarCorrespondence(I1, I2, F, xc, yc)
#np.append(pts2, np.array([[x2, y2]]), axis = 0)
print("x2, y2: ", x2, y2)
ax2.plot(x2, y2, 'ro', markersize=8, linewidth=2)
plt.draw()