-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsfm.py
304 lines (254 loc) · 14.5 KB
/
sfm.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import cv2
import numpy as np
import os
from scipy.optimize import least_squares
from tomlkit import boolean
from tqdm import tqdm
import matplotlib.pyplot as plt
class Image_loader():
def __init__(self, img_dir:str, downscale_factor:float):
# loading the Camera intrinsic parameters K
with open(img_dir + '\\K.txt') as f:
self.K = np.array(list((map(lambda x:list(map(lambda x:float(x), x.strip().split(' '))),f.read().split('\n')))))
self.image_list = []
# Loading the set of images
for image in sorted(os.listdir(img_dir)):
if image[-4:].lower() == '.jpg' or image[-5:].lower() == '.png':
self.image_list.append(img_dir + '\\' + image)
self.path = os.getcwd()
self.factor = downscale_factor
self.downscale()
def downscale(self) -> None:
'''
Downscales the Image intrinsic parameter acc to the downscale factor
'''
self.K[0, 0] /= self.factor
self.K[1, 1] /= self.factor
self.K[0, 2] /= self.factor
self.K[1, 2] /= self.factor
def downscale_image(self, image):
for _ in range(1,int(self.factor / 2) + 1):
image = cv2.pyrDown(image)
return image
class Sfm():
def __init__(self, img_dir:str, downscale_factor:float = 2.0) -> None:
'''
Initialise and Sfm object.
'''
self.img_obj = Image_loader(img_dir,downscale_factor)
def triangulation(self, point_2d_1, point_2d_2, projection_matrix_1, projection_matrix_2) -> tuple:
'''
Triangulates 3d points from 2d vectors and projection matrices
returns projection matrix of first camera, projection matrix of second camera, point cloud
'''
pt_cloud = cv2.triangulatePoints(point_2d_1, point_2d_2, projection_matrix_1.T, projection_matrix_2.T)
return projection_matrix_1.T, projection_matrix_2.T, (pt_cloud / pt_cloud[3])
def PnP(self, obj_point, image_point , K, dist_coeff, rot_vector, initial) -> tuple:
'''
Finds an object pose from 3D-2D point correspondences using the RANSAC scheme.
returns rotational matrix, translational matrix, image points, object points, rotational vector
'''
if initial == 1:
obj_point = obj_point[:, 0 ,:]
image_point = image_point.T
rot_vector = rot_vector.T
_, rot_vector_calc, tran_vector, inlier = cv2.solvePnPRansac(obj_point, image_point, K, dist_coeff, cv2.SOLVEPNP_ITERATIVE)
# Converts a rotation matrix to a rotation vector or vice versa
rot_matrix, _ = cv2.Rodrigues(rot_vector_calc)
if inlier is not None:
image_point = image_point[inlier[:, 0]]
obj_point = obj_point[inlier[:, 0]]
rot_vector = rot_vector[inlier[:, 0]]
return rot_matrix, tran_vector, image_point, obj_point, rot_vector
def reprojection_error(self, obj_points, image_points, transform_matrix, K, homogenity) ->tuple:
'''
Calculates the reprojection error ie the distance between the projected points and the actual points.
returns total error, object points
'''
rot_matrix = transform_matrix[:3, :3]
tran_vector = transform_matrix[:3, 3]
rot_vector, _ = cv2.Rodrigues(rot_matrix)
if homogenity == 1:
obj_points = cv2.convertPointsFromHomogeneous(obj_points.T)
image_points_calc, _ = cv2.projectPoints(obj_points, rot_vector, tran_vector, K, None)
image_points_calc = np.float32(image_points_calc[:, 0, :])
total_error = cv2.norm(image_points_calc, np.float32(image_points.T) if homogenity == 1 else np.float32(image_points), cv2.NORM_L2)
return total_error / len(image_points_calc), obj_points
def optimal_reprojection_error(self, obj_points) -> np.array:
'''
calculates of the reprojection error during bundle adjustment
returns error
'''
transform_matrix = obj_points[0:12].reshape((3,4))
K = obj_points[12:21].reshape((3,3))
rest = int(len(obj_points[21:]) * 0.4)
p = obj_points[21:21 + rest].reshape((2, int(rest/2))).T
obj_points = obj_points[21 + rest:].reshape((int(len(obj_points[21 + rest:])/3), 3))
rot_matrix = transform_matrix[:3, :3]
tran_vector = transform_matrix[:3, 3]
rot_vector, _ = cv2.Rodrigues(rot_matrix)
image_points, _ = cv2.projectPoints(obj_points, rot_vector, tran_vector, K, None)
image_points = image_points[:, 0, :]
error = [ (p[idx] - image_points[idx])**2 for idx in range(len(p))]
return np.array(error).ravel()/len(p)
def bundle_adjustment(self, _3d_point, opt, transform_matrix_new, K, r_error) -> tuple:
'''
Bundle adjustment for the image and object points
returns object points, image points, transformation matrix
'''
opt_variables = np.hstack((transform_matrix_new.ravel(), K.ravel()))
opt_variables = np.hstack((opt_variables, opt.ravel()))
opt_variables = np.hstack((opt_variables, _3d_point.ravel()))
values_corrected = least_squares(self.optimal_reprojection_error, opt_variables, gtol = r_error).x
K = values_corrected[12:21].reshape((3,3))
rest = int(len(values_corrected[21:]) * 0.4)
return values_corrected[21 + rest:].reshape((int(len(values_corrected[21 + rest:])/3), 3)), values_corrected[21:21 + rest].reshape((2, int(rest/2))).T, values_corrected[0:12].reshape((3,4))
def to_ply(self, path, point_cloud, colors) -> None:
'''
Generates the .ply which can be used to open the point cloud
'''
out_points = point_cloud.reshape(-1, 3) * 200
out_colors = colors.reshape(-1, 3)
print(out_colors.shape, out_points.shape)
verts = np.hstack([out_points, out_colors])
mean = np.mean(verts[:, :3], axis=0)
scaled_verts = verts[:, :3] - mean
dist = np.sqrt(scaled_verts[:, 0] ** 2 + scaled_verts[:, 1] ** 2 + scaled_verts[:, 2] ** 2)
indx = np.where(dist < np.mean(dist) + 300)
verts = verts[indx]
ply_header = '''ply
format ascii 1.0
element vertex %(vert_num)d
property float x
property float y
property float z
property uchar blue
property uchar green
property uchar red
end_header
'''
with open(path + '\\res\\' + self.img_obj.image_list[0].split('\\')[-2] + '.ply', 'w') as f:
f.write(ply_header % dict(vert_num=len(verts)))
np.savetxt(f, verts, '%f %f %f %d %d %d')
def common_points(self, image_points_1, image_points_2, image_points_3) -> tuple:
'''
Finds the common points between image 1 and 2 , image 2 and 3
returns common points of image 1-2, common points of image 2-3, mask of common points 1-2 , mask for common points 2-3
'''
cm_points_1 = []
cm_points_2 = []
for i in range(image_points_1.shape[0]):
a = np.where(image_points_2 == image_points_1[i, :])
if a[0].size != 0:
cm_points_1.append(i)
cm_points_2.append(a[0][0])
mask_array_1 = np.ma.array(image_points_2, mask=False)
mask_array_1.mask[cm_points_2] = True
mask_array_1 = mask_array_1.compressed()
mask_array_1 = mask_array_1.reshape(int(mask_array_1.shape[0] / 2), 2)
mask_array_2 = np.ma.array(image_points_3, mask=False)
mask_array_2.mask[cm_points_2] = True
mask_array_2 = mask_array_2.compressed()
mask_array_2 = mask_array_2.reshape(int(mask_array_2.shape[0] / 2), 2)
print(" Shape New Array", mask_array_1.shape, mask_array_2.shape)
return np.array(cm_points_1), np.array(cm_points_2), mask_array_1, mask_array_2
def find_features(self, image_0, image_1) -> tuple:
'''
Feature detection using the sift algorithm and KNN
return keypoints(features) of image1 and image2
'''
sift = cv2.xfeatures2d.SIFT_create()
key_points_0, desc_0 = sift.detectAndCompute(cv2.cvtColor(image_0, cv2.COLOR_BGR2GRAY), None)
key_points_1, desc_1 = sift.detectAndCompute(cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY), None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(desc_0, desc_1, k=2)
feature = []
for m, n in matches:
if m.distance < 0.70 * n.distance:
feature.append(m)
return np.float32([key_points_0[m.queryIdx].pt for m in feature]), np.float32([key_points_1[m.trainIdx].pt for m in feature])
def __call__(self, enable_bundle_adjustment:boolean=False):
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
pose_array = self.img_obj.K.ravel()
transform_matrix_0 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
transform_matrix_1 = np.empty((3, 4))
pose_0 = np.matmul(self.img_obj.K, transform_matrix_0)
pose_1 = np.empty((3, 4))
total_points = np.zeros((1, 3))
total_colors = np.zeros((1, 3))
image_0 = self.img_obj.downscale_image(cv2.imread(self.img_obj.image_list[0]))
image_1 = self.img_obj.downscale_image(cv2.imread(self.img_obj.image_list[1]))
feature_0, feature_1 = self.find_features(image_0, image_1)
# Essential matrix
essential_matrix, em_mask = cv2.findEssentialMat(feature_0, feature_1, self.img_obj.K, method=cv2.RANSAC, prob=0.999, threshold=0.4, mask=None)
feature_0 = feature_0[em_mask.ravel() == 1]
feature_1 = feature_1[em_mask.ravel() == 1]
_, rot_matrix, tran_matrix, em_mask = cv2.recoverPose(essential_matrix, feature_0, feature_1, self.img_obj.K)
feature_0 = feature_0[em_mask.ravel() > 0]
feature_1 = feature_1[em_mask.ravel() > 0]
transform_matrix_1[:3, :3] = np.matmul(rot_matrix, transform_matrix_0[:3, :3])
transform_matrix_1[:3, 3] = transform_matrix_0[:3, 3] + np.matmul(transform_matrix_0[:3, :3], tran_matrix.ravel())
pose_1 = np.matmul(self.img_obj.K, transform_matrix_1)
feature_0, feature_1, points_3d = self.triangulation(pose_0, pose_1, feature_0, feature_1)
error, points_3d = self.reprojection_error(points_3d, feature_1, transform_matrix_1, self.img_obj.K, homogenity = 1)
#ideally error < 1
print("REPROJECTION ERROR: ", error)
_, _, feature_1, points_3d, _ = self.PnP(points_3d, feature_1, self.img_obj.K, np.zeros((5, 1), dtype=np.float32), feature_0, initial=1)
total_images = len(self.img_obj.image_list) - 2
pose_array = np.hstack((np.hstack((pose_array, pose_0.ravel())), pose_1.ravel()))
threshold = 0.5
for i in tqdm(range(total_images)):
image_2 = self.img_obj.downscale_image(cv2.imread(self.img_obj.image_list[i + 2]))
features_cur, features_2 = self.find_features(image_1, image_2)
if i != 0:
feature_0, feature_1, points_3d = self.triangulation(pose_0, pose_1, feature_0, feature_1)
feature_1 = feature_1.T
points_3d = cv2.convertPointsFromHomogeneous(points_3d.T)
points_3d = points_3d[:, 0, :]
cm_points_0, cm_points_1, cm_mask_0, cm_mask_1 = self.common_points(feature_1, features_cur, features_2)
cm_points_2 = features_2[cm_points_1]
cm_points_cur = features_cur[cm_points_1]
rot_matrix, tran_matrix, cm_points_2, points_3d, cm_points_cur = self.PnP(points_3d[cm_points_0], cm_points_2, self.img_obj.K, np.zeros((5, 1), dtype=np.float32), cm_points_cur, initial = 0)
transform_matrix_1 = np.hstack((rot_matrix, tran_matrix))
pose_2 = np.matmul(self.img_obj.K, transform_matrix_1)
error, points_3d = self.reprojection_error(points_3d, cm_points_2, transform_matrix_1, self.img_obj.K, homogenity = 0)
cm_mask_0, cm_mask_1, points_3d = self.triangulation(pose_1, pose_2, cm_mask_0, cm_mask_1)
error, points_3d = self.reprojection_error(points_3d, cm_mask_1, transform_matrix_1, self.img_obj.K, homogenity = 1)
print("Reprojection Error: ", error)
pose_array = np.hstack((pose_array, pose_2.ravel()))
# takes a long time to run
if enable_bundle_adjustment:
points_3d, cm_mask_1, transform_matrix_1 = self.bundle_adjustment(points_3d, cm_mask_1, transform_matrix_1, self.img_obj.K, threshold)
pose_2 = np.matmul(self.img_obj.K, transform_matrix_1)
error, points_3d = self.reprojection_error(points_3d, cm_mask_1, transform_matrix_1, self.img_obj.K, homogenity = 0)
print("Bundle Adjusted error: ",error)
total_points = np.vstack((total_points, points_3d))
points_left = np.array(cm_mask_1, dtype=np.int32)
color_vector = np.array([image_2[l[1], l[0]] for l in points_left])
total_colors = np.vstack((total_colors, color_vector))
else:
total_points = np.vstack((total_points, points_3d[:, 0, :]))
points_left = np.array(cm_mask_1, dtype=np.int32)
color_vector = np.array([image_2[l[1], l[0]] for l in points_left.T])
total_colors = np.vstack((total_colors, color_vector))
transform_matrix_0 = np.copy(transform_matrix_1)
pose_0 = np.copy(pose_1)
plt.scatter(i, error)
plt.pause(0.05)
image_0 = np.copy(image_1)
image_1 = np.copy(image_2)
feature_0 = np.copy(features_cur)
feature_1 = np.copy(features_2)
pose_1 = np.copy(pose_2)
cv2.imshow(self.img_obj.image_list[0].split('\\')[-2], image_2)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()
print("Printing to .ply file")
print(total_points.shape, total_colors.shape)
self.to_ply(self.img_obj.path, total_points, total_colors)
print("Completed Exiting ...")
np.savetxt(self.img_obj.path + '\\res\\' + self.img_obj.image_list[0].split('\\')[-2]+'_pose_array.csv', pose_array, delimiter = '\n')
if __name__ == '__main__':
sfm = Sfm("Datasets\\Herz-Jesus-P8")
sfm()