2323# % data1を移動させてdata2を作る
2424# data2=t+A*data1;
2525
26- # % ICPアルゴリズム data2とdata1のMatching
27- # % R:回転行列 t:併進ベクトル
28- # % [R,T]=icp(data1,data2)
29- # [R,T] = ICPMatching(data2,data1);
30-
3126# function [R, t]=ICPMatching(data1, data2)
3227# % ICPアルゴリズムによる、並進ベクトルと回転行列の計算を実施する関数
3328# % data1 = [x(t)1 x(t)2 x(t)3 ...]
3429# % data2 = [x(t+1)1 x(t+1)2 x(t+1)3 ...]
3530# % x=[x y z]'
3631
37- # R=eye(2);%回転行列
38- # t=zeros(2,1);%並進ベクトル
3932
4033# while ~(dError < EPS)
4134# count=count+1;
9386
9487
9588def 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
9891
9992 # 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
102110
103111 # preError = 0
104- # loopcount = 0
105- # dError = 1000.0
106112
107113 return R , T
108114
109115
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+
110133def main ():
111134 print (__file__ + " start!!" )
112135
@@ -126,6 +149,16 @@ def main():
126149 cy = [math .sin (motion [2 ]) * x + math .cos (motion [2 ]) * y + motion [1 ]
127150 for (x , y ) in zip (px , py )]
128151
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+
129162 plt .plot (px , py , ".b" )
130163 plt .plot (cx , cy , ".r" )
131164 plt .plot (0.0 , 0.0 , "xr" )
0 commit comments