-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_semantic_icp.py
293 lines (217 loc) · 8.72 KB
/
simple_semantic_icp.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
import numpy as np
from sklearn.neighbors import NearestNeighbors
import torch
def best_fit_transform(A, B):
'''
Calculates the least-squares best-fit transform that maps corresponding points A to B in m spatial dimensions
Input:
A: Nxm numpy array of corresponding points
B: Nxm numpy array of corresponding points
Returns:
T: (m+1)x(m+1) homogeneous transformation matrix that maps A on to B
R: mxm rotation matrix
t: mx1 translation vector
'''
assert A.shape == B.shape
# get number of dimensions
m = A.shape[1]
# translate points to their centroids
centroid_A = np.mean(A, axis=0)
centroid_B = np.mean(B, axis=0)
AA = A - centroid_A
BB = B - centroid_B
# rotation matrix
H = np.dot(AA.T, BB)
U, S, Vt = np.linalg.svd(H)
R = np.dot(Vt.T, U.T)
# special reflection case
if np.linalg.det(R) < 0:
Vt[m-1,:] *= -1
R = np.dot(Vt.T, U.T)
# translation
t = centroid_B.T - np.dot(R,centroid_A.T)
# homogeneous transformation
T = np.identity(m+1)
T[:m, :m] = R
T[:m, m] = t
return T, R, t
def nearest_neighbor(src, dst):
'''
Find the nearest (Euclidean) neighbor in dst for each point in src
Input:
src: Nxm array of points
dst: Nxm array of points
Output:
distances: Euclidean distances of the nearest neighbor
indices: dst indices of the nearest neighbor
'''
assert src.shape == dst.shape
neigh = NearestNeighbors(n_neighbors=1)
neigh.fit(dst)
distances, indices = neigh.kneighbors(src, return_distance=True)
return distances.ravel(), indices.ravel()
def icp(A, B, init_pose=None, max_iterations=20, tolerance=0.001):
'''
The Iterative Closest Point method: finds best-fit transform that maps points A on to points B
Input:
A: Nxm numpy array of source mD points
B: Nxm numpy array of destination mD point
init_pose: (m+1)x(m+1) homogeneous transformation
max_iterations: exit algorithm after max_iterations
tolerance: convergence criteria
Output:
T: final homogeneous transformation that maps A on to B
distances: Euclidean distances (errors) of the nearest neighbor
i: number of iterations to converge
'''
assert A.shape == B.shape
# get number of dimensions
m = A.shape[1]
# make points homogeneous, copy them to maintain the originals
src = np.ones((m+1,A.shape[0]))
dst = np.ones((m+1,B.shape[0]))
src[:m,:] = np.copy(A.T)
dst[:m,:] = np.copy(B.T)
# apply the initial pose estimation
if init_pose is not None:
src = np.dot(init_pose, src)
prev_error = 0
for i in range(max_iterations):
# find the nearest neighbors between the current source and destination points
distances, indices = nearest_neighbor(src[:m,:].T, dst[:m,:].T)
print("distances : ", distances.shape)
print("indices : ", indices.shape)
# compute the transformation between the current source and nearest destination points
T,_,_ = best_fit_transform(src[:m,:].T, dst[:m,indices].T)
# update the current source
src = np.dot(T, src)
# check error
mean_error = np.mean(distances)
if np.abs(prev_error - mean_error) < tolerance:
break
prev_error = mean_error
# calculate final transformation
T,_,_ = best_fit_transform(A, src[:m,:].T)
return T, distances, i
"""
A: Nx3
B: Mx3
"""
def correspondences_given_labels(A, B, labels_A, labels_B):
with torch.no_grad():
high_value = 1e8
unique_labels_A = np.unique(labels_A)
unique_labels_B = np.unique(labels_B)
dist_matrix = torch.cdist(A, B, p=2.0) # NxM
mask = torch.tensor(labels_A[:, np.newaxis] != labels_B[np.newaxis, :]).bool()
# print(dist_matrix.shape, mask.shape)
dist_matrix.masked_fill_(mask, high_value)
# correspondences = torch.argmin(dist_matrix, axis=-1)
distance, correspondences = torch.min(dist_matrix, -1)
# print(dist_matrix) # AxB
# print(distance)
return correspondences, distance
def semantic_icp(A, B, labels_A, labels_B, init_pose=None, max_iterations=20, tolerance=0.001):
'''
The Iterative Closest Point method: finds best-fit transform that maps points A on to points B
Input:
A: (Nxm) numpy array of source mD points
B: (Nxm) numpy array of destination mD point
labels_A: (N) numpy array of source labels
labels_B: (N) numpy array of destination labels
init_pose: (m+1)x(m+1) homogeneous transformation
max_iterations: exit algorithm after max_iterations
tolerance: convergence criteria
Output:
T: final homogeneous transformation that maps A on to B
distances: Euclidean distances (errors) of the nearest neighbor
i: number of iterations to converge
'''
assert A.shape[0] == labels_A.shape[0]
assert B.shape[0] == labels_B.shape[0]
# assert len(np.unique(labels_A)) == len(np.unique(labels_B))
# get number of dimensions
m = A.shape[1]
# make points homogeneous, copy them to maintain the originals
src = np.ones((m+1,A.shape[0]))
dst = np.ones((m+1,B.shape[0]))
src[:m,:] = np.copy(A.T)
dst[:m,:] = np.copy(B.T)
# apply the initial pose estimation
if init_pose is not None:
src = np.dot(init_pose, src)
prev_error = 0
for i in range(max_iterations):
# find the nearest neighbors between the current source and destination points
# print("asdfsadfsdf: ", src.shape, dst.shape)
indices, distances = correspondences_given_labels(torch.tensor(src[:m,:].T), torch.tensor(dst[:m,:].T), labels_A, labels_B)
# print("indices : ", indices.shape)
# compute the transformation between the current source and nearest destination points
T,_,_ = best_fit_transform(src[:m,:].T, dst[:m,indices].T)
# update the current source
src = np.dot(T, src)
# check error
# print(distances.shape, distances)
mean_error = np.mean(np.array(distances))
if np.abs(prev_error - mean_error) < tolerance:
break
prev_error = mean_error
# calculate final transformation
T,_,_ = best_fit_transform(np.asarray(A), src[:m,:].T)
return T
import open3d as o3d
from scipy.spatial.transform import Rotation as R
if __name__ == '__main__':
# read and split pcd
pcdA = o3d.io.read_point_cloud('bunny.ply')
ptsA = np.asarray(pcdA.points)
A_left = ptsA[:len(ptsA)//2, :]
A_right = ptsA[len(ptsA)//2:, :]
A_right += [0.1,0,0]
labels = np.zeros(len(ptsA))
labels[len(ptsA)//2:] = 1
print(labels)
print(A_left.shape, A_right.shape, ptsA.shape)
# target pcd
pcdA_left = o3d.geometry.PointCloud()
pcdA_right = o3d.geometry.PointCloud()
pcdA_left.points = o3d.utility.Vector3dVector(A_left)
pcdA_right.points = o3d.utility.Vector3dVector(A_right)
pcdA_left.paint_uniform_color([1.,0.,0.])
pcdA_right.paint_uniform_color([1.,1.,0.])
pcdA_split = pcdA_left + pcdA_right
# source pcd
pcdB_left = o3d.geometry.PointCloud()
pcdB_right = o3d.geometry.PointCloud()
pcdB_left.points = o3d.utility.Vector3dVector(A_left)
pcdB_right.points = o3d.utility.Vector3dVector(A_right)
pcdB_left.paint_uniform_color([0.,1.,0.])
pcdB_right.paint_uniform_color([0.,0.,1.])
pcdB_split = pcdB_left + pcdB_right
# o3d.io.write_point_cloud('tx_split.ply', pcdA_left + pcdA_right)
k = correspondences_given_labels(torch.tensor(np.asarray(pcdA_split.points)), torch.tensor(np.asarray(pcdB_split.points)), torch.tensor(labels), torch.tensor(labels))
print("adsf: " ,k)
# transform
randomT = np.eye(4)
randomT[:3,:3] = R.from_euler('xyz', [0,0,0], degrees=True).as_matrix()
randomT[:3, 3] = [2.,0.,0.]
from copy import deepcopy as dc
pcdB_split_og = dc(pcdB_split)
pcdB_split.transform(randomT)
print(randomT)
a = np.asarray(pcdA_split.points)
b = np.asarray(pcdB_split.points)
T_semantic, _ = semantic_icp(a, b, labels, labels, tolerance=0.00001)
T_base_icp, _, _ = icp(a, b, tolerance=0.00001)
o3d.io.write_point_cloud('tx_gt.ply', pcdA_split + pcdB_split)
T = T_semantic
T_inv = np.eye(4)
T_inv[:3, :3] = np.linalg.inv(T[:3, :3])
T_inv[3, :3] = -T[3, :3]
o3d.io.write_point_cloud('tx_aligned.ply', pcdA_split + pcdB_split.transform(T_inv))
print("------------------------------------")
print(randomT)
print()
print("semantic: ", T_semantic)
print("base: ", T_base_icp)
print(f"Semantic is: {np.isclose(randomT, T_semantic).all()}, base icp is {np.isclose(randomT, T_base_icp).all()}")