23
23
# % data1を移動させてdata2を作る
24
24
# data2=t+A*data1;
25
25
26
- # % ICPアルゴリズム data2とdata1のMatching
27
- # % R:回転行列 t:併進ベクトル
28
- # % [R,T]=icp(data1,data2)
29
- # [R,T] = ICPMatching(data2,data1);
30
-
31
26
# function [R, t]=ICPMatching(data1, data2)
32
27
# % ICPアルゴリズムによる、並進ベクトルと回転行列の計算を実施する関数
33
28
# % data1 = [x(t)1 x(t)2 x(t)3 ...]
34
29
# % data2 = [x(t+1)1 x(t+1)2 x(t+1)3 ...]
35
30
# % x=[x y z]'
36
31
37
- # R=eye(2);%回転行列
38
- # t=zeros(2,1);%並進ベクトル
39
32
40
33
# while ~(dError < EPS)
41
34
# count=count+1;
93
86
94
87
95
88
def ICP_matching (pdata , data ):
96
- R = 0.0 # rotation matrix
97
- T = 0.0 # translation vector
89
+ R = np . eye ( 2 ) # rotation matrix
90
+ T = np . zeros (( 2 , 1 )) # translation vector
98
91
99
92
# ICP
100
- # EPS = 0.0001
101
- # maxIter = 100
93
+ EPS = 0.0001
94
+ maxIter = 100
95
+
96
+ dError = 1000.0
97
+ count = 0
98
+
99
+ while dError >= EPS :
100
+ count += 1
101
+
102
+ Rt , Tt = SVD_motion_estimation (pdata , data )
103
+ # print(count)
104
+
105
+ R = R * Rt
106
+ T = R * T + Tt
107
+
108
+ if maxIter <= count :
109
+ break
102
110
103
111
# preError = 0
104
- # loopcount = 0
105
- # dError = 1000.0
106
112
107
113
return R , T
108
114
109
115
116
+ def SVD_motion_estimation (pdata , data ):
117
+
118
+ pm = np .mean (pdata , axis = 1 )
119
+ cm = np .mean (data , axis = 1 )
120
+
121
+ pshift = pdata - pm
122
+ cshift = data - cm
123
+
124
+ W = pshift * cshift .T
125
+ u , s , vh = np .linalg .svd (W )
126
+
127
+ R = (u * vh .T ).T
128
+ t = pm - R * cm
129
+
130
+ return R , t
131
+
132
+
110
133
def main ():
111
134
print (__file__ + " start!!" )
112
135
@@ -126,6 +149,16 @@ def main():
126
149
cy = [math .sin (motion [2 ]) * x + math .cos (motion [2 ]) * y + motion [1 ]
127
150
for (x , y ) in zip (px , py )]
128
151
152
+ pdata = np .matrix (np .vstack ((px , py )))
153
+ # print(pdata)
154
+ data = np .matrix (np .vstack ((cx , cy )))
155
+ # print(data)
156
+
157
+ R , T = ICP_matching (pdata , data )
158
+ print (motion )
159
+ print (R )
160
+ print (T )
161
+
129
162
plt .plot (px , py , ".b" )
130
163
plt .plot (cx , cy , ".r" )
131
164
plt .plot (0.0 , 0.0 , "xr" )
0 commit comments