-
Notifications
You must be signed in to change notification settings - Fork 28
/
MyICP.py
271 lines (202 loc) · 9.7 KB
/
MyICP.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 16:21:56 2019
@author: rain
"""
import numpy as np
from time import time
import mayavi.mlab as mlab
from numpy import linalg as LA
from scipy.spatial.distance import cdist
import copy
from sklearn.neighbors import NearestNeighbors
from Match import *
from Transformations import *
def RandomDownSample4PC(PC, ratio):
RandIdxes = np.random.random((int(PC.shape[0]*ratio),))*PC.shape[0]
RandIdxes = np.array(RandIdxes, dtype=np.int32)
PC_ = PC[RandIdxes,:]
return PC_
# refered to https://stackoverflow.com/questions/20120384/iterative-closest-point-icp-implementation-on-python
def ICP(PC0, PC1, maxIterTimes=50, minIterTimes=20-1, inlierThreshold=0.5, smallShiftThreshold=0.05, decay_rate = 0.9, ep=0.001):
R_star = np.eye(3, dtype=np.float64)
T_star = np.zeros((3,1), dtype=np.float64)
for iIter in range(maxIterTimes):
nbrs = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(PC0)
distances, indices = nbrs.kneighbors(PC1)
# extract the inlier pairs
idx1 = (distances < inlierThreshold).flatten()
fullIdx0 = np.arange(PC0.shape[0])
idx0 = fullIdx0[indices[idx1]].flatten()
if idx0.shape[0] < 100:
print('ICP iters:', iIter+1, ', inliers:', idx0.shape[0], ', inlierThreshold:', round(inlierThreshold, 5))
return R_star, T_star, False
inliers0 = PC0[idx0,:]
inliers1 = PC1[idx1,:]
# solve RT
R, T, isCredible = SolveRT(inliers0, inliers1)
# update pairs1 and RT
PC1 = (np.dot(R, PC1.T) + T).T
R_star = np.dot(R, R_star)
T_star = np.dot(R, T_star) + T
# check if need to break
eulers = RotateMat2EulerAngle_XYZ(R)
normEulers = LA.norm(eulers)
normT = LA.norm(T)
if iIter >= minIterTimes:
if normEulers < ep and normT < ep:
break
# print(normEulers, normT, inlierThreshold)
# update threshold
if normEulers < smallShiftThreshold and normT < smallShiftThreshold:
inlierThreshold *= decay_rate
# inlierThreshold *= decay_rate
isSuccess = True
print('ICP iters:', iIter+1, ', inliers:', idx0.shape[0], ', inlierThreshold:', round(inlierThreshold, 5))
return R_star, T_star, isSuccess
def GetPtsInliners(PC0, PC1, inlierThreshold):
nbrs = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(PC0)
distances, indices = nbrs.kneighbors(PC1)
# extract the inlier pairs
idx1 = (distances < inlierThreshold).flatten()
fullIdx0 = np.arange(PC0.shape[0])
idx0 = fullIdx0[indices[idx1]].flatten()
inliers0 = PC0[idx0,:]
inliers1 = PC1[idx1,:]
return inliers0, inliers1
def GetPlanarPtsInliners(PtsWithNorm0, PtsWithNorm1, inlierThreshold0, inlierThreshold1):
PC0 = PtsWithNorm0[:,0:3]
Norms0 = PtsWithNorm0[:,3:6]
PC1 = PtsWithNorm1[:,0:3]
Norms1 = PtsWithNorm1[:,3:6]
nbrs = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(PC0)
distances, indices = nbrs.kneighbors(PC1)
# extract the inlier pairs
idx1 = (distances < inlierThreshold1).flatten()
fullIdx0 = np.arange(PC0.shape[0])
idx0 = fullIdx0[indices[idx1]].flatten()
inliers0 = PC0[idx0,:]
inliers1 = PC1[idx1,:]
norms1 = Norms1[idx1,:]
vetors = inliers0 - inliers1
dist2Planes = np.sum(norms1*vetors, axis=1)
pedals = inliers1 + norms1*np.tile(dist2Planes.reshape(dist2Planes.shape[0],1),[1,3])
distances = LA.norm((pedals-inliers1), axis=1)
idx = (distances < inlierThreshold0).flatten()
pedals = pedals[idx,:]
inliers1 = inliers1[idx,:]
return pedals, inliers1
def ShowPts(fig, pts, ptSize, color):
colors = color*np.ones((pts.shape[0],1), dtype=np.float32)
if ptSize <= 0:
node = mlab.points3d(pts[:,0], pts[:,1], pts[:,2], mode="point", figure=fig)
else:
node = mlab.points3d(pts[:,0], pts[:,1], pts[:,2], scale_factor=ptSize, figure=fig)
node.glyph.scale_mode = 'scale_by_vector'
node.mlab_source.dataset.point_data.scalars = colors
return 0
def ICP_Pt2PtAndPt2Plane(PC0, PC1, PtsWithNorm0, PtsWithNorm1, maxIterTimes=50, minIterTimes=20-1,
inlierThreshold0=0.5, decay_rate0 = 0.9,
inlierThreshold1=2.0, decay_rate1 = 0.5,
smallShiftThreshold=0.1, ep=0.01):
R_star = np.eye(3, dtype=np.float64)
T_star = np.zeros((3,1), dtype=np.float64)
# sample planarPts
nMaxPts = 2000
if PtsWithNorm1.shape[0] > nMaxPts:
RandIdxes = np.random.random((nMaxPts,))
RandIdxes = RandIdxes*(PtsWithNorm1.shape[0])
RandIdxes = np.array(RandIdxes, dtype=np.int32)
PtsWithNorm1 = PtsWithNorm1[RandIdxes,:]
isSuccess = True
minNumOfInputPts = 200
for iIter in range(maxIterTimes):
inliers0_pts, inliers1_pts = GetPtsInliners(PC0, PC1, inlierThreshold0)
if iIter < 100:
inliers0_planarPts, inliers1_planarPts = GetPlanarPtsInliners(PtsWithNorm0, PtsWithNorm1, inlierThreshold0, inlierThreshold1)
inliers0 = np.r_[inliers0_pts, inliers0_planarPts]
inliers1 = np.r_[inliers1_pts, inliers1_planarPts]
else:
inliers0 = inliers0_planarPts
inliers1 = inliers1_planarPts
# print('inliers0_pts', inliers0_pts.shape[0], 'inlierThreshold0', inlierThreshold0)
# print('inliers0_planarPts', inliers0_planarPts.shape[0], 'inlierThreshold1', inlierThreshold1)
# # visualization
# fig = mlab.figure(bgcolor=(0, 0, 0), size=(1640, 1500))
# ShowPts(fig, inliers0_pts, ptSize=0.1, color=1.0)
# ShowPts(fig, inliers1_pts, ptSize=0.1, color=1.0)
# ShowPts(fig, inliers0_planarPts, ptSize=0.01, color=0.1)
# ShowPts(fig, inliers1_planarPts, ptSize=0.01, color=0.1)
#
# vectors = inliers0 - inliers1
# mlab.quiver3d(inliers1[:,0], inliers1[:,1], inliers1[:,2], \
# vectors[:,0], vectors[:,1], vectors[:,2], \
# figure=fig, line_width=0.5, scale_factor=1)
# mlab.show()
if inliers0.shape[0] < minNumOfInputPts:
if iIter < 1:
isSuccess = False
break
# solve RT
R, T, isCredible = SolveRT(inliers0, inliers1)
# update pairs1 and RT
PC1 = (np.dot(R, PC1.T) + T).T
PtsWithNorm1[:,0:3] = (np.dot(R, PtsWithNorm1[:,0:3].T) + T).T
R_star = np.dot(R, R_star)
T_star = np.dot(R, T_star) + T
# check if need to break
eulers = RotateMat2EulerAngle_XYZ(R)
normEulers = LA.norm(eulers)
normT = LA.norm(T)
if iIter >= minIterTimes:
if normEulers < ep and normT < ep:
break
# update threshold
if normEulers < smallShiftThreshold and normT < smallShiftThreshold:
inlierThreshold0 *= decay_rate0
inlierThreshold1 *= decay_rate1
print('ICP iters:', iIter+1, ', inliers0:', inliers0_pts.shape[0], ', inliers1:', inliers0_planarPts.shape[0],
', th0:', round(inlierThreshold0, 5), ', th1:', round(inlierThreshold1, 5))
return R_star, T_star, isSuccess
if __name__ == "__main__":
strSequence = '01'
iFrame0 = 0
iFrameStep = 1
iFrame1 = iFrame0 + iFrameStep
DataDir = '/media/rain/Win10_F/KITTI_odometry/velodyne/sequences/'+strSequence+'/velodyne/'
FileName0 = DataDir + str(iFrame0).zfill(6)+'.bin'
FileName1 = DataDir + str(iFrame1).zfill(6)+'.bin'
PC0 = np.fromfile(FileName0, dtype=np.float32, count=-1).reshape([-1,4])[:,0:3]
PC1 = np.fromfile(FileName1, dtype=np.float32, count=-1).reshape([-1,4])[:,0:3]
t0 = time()
downSampleRatio = 0.5
PC0_ = RandomDownSample4PC(PC0, downSampleRatio)
PC1_ = RandomDownSample4PC(PC1, downSampleRatio)
t1 = time()
print(round(t1-t0, 2), 's, for RandomDownSample4PC')
R_ICP, T_ICP, isSuccess = ICP(PC0_, PC1_)
t2 = time()
print(round(t2-t1, 2), 's, for ICP')
PC1_ = (np.dot(R_ICP, PC1.T) + T_ICP.reshape(3,1)).T
FusedPC = np.r_[PC0, PC1_]
Colors0=np.ones((PC0.shape[0],1), dtype=np.float32)*1.0
Colors1=np.ones((PC1.shape[0],1), dtype=np.float32)*0.0
Colors4FusedPC=np.r_[Colors0, Colors1]
Colors4FusedPC=Colors4FusedPC.flatten()
shift4Show = 0
fig = mlab.figure(bgcolor=(0, 0, 0), size=(1500, 900))
node = mlab.points3d(PC0[:,0], PC0[:,1], PC0[:,2], mode="point", figure=fig)
node.glyph.scale_mode = 'scale_by_vector'
node.mlab_source.dataset.point_data.scalars = Colors0
node = mlab.points3d(PC1[:,0], PC1[:,1], PC1[:,2]+shift4Show, mode="point", figure=fig)
node.glyph.scale_mode = 'scale_by_vector'
node.mlab_source.dataset.point_data.scalars = Colors1
mlab.title('Feature Matching')
mlab.axes(xlabel='X', ylabel='Y', zlabel='Z')
fig = mlab.figure(bgcolor=(0, 0, 0), size=(1200, 800))
mlab.points3d(FusedPC[:,0], FusedPC[:,1], FusedPC[:,2],
Colors4FusedPC, mode="point", figure=fig)
mlab.title('Fused PC')
mlab.axes(xlabel='X', ylabel='Y', zlabel='Z')
mlab.show()