1010import  numpy  as  np 
1111import  matplotlib .pyplot  as  plt 
1212
13- #  % 点をランダムでばら撒く(t-1の時の点群) 
14- #  data1=fieldLength*rand(2,nPoint)-fieldLength/2; 
15- 
16- #  % data2= data1を移動させる & ノイズ付加 
17- #  % 回転方向 & ノイズ付加 
18- #  theta=toRadian(motion(3))+toRadian(thetaSigma)*rand(1); 
19- #  % 並進ベクトル & ノイズ付加 
20- #  t=repmat(motion(1:2)',1,nPoint)+transitionSigma*randn(2,nPoint); 
21- #  % 回転行列の作成 
22- #  A=[cos(theta) sin(theta);-sin(theta) cos(theta)]; 
23- #  % data1を移動させてdata2を作る 
24- #  data2=t+A*data1; 
25- 
26- #  function [R, t]=ICPMatching(data1, data2) 
27- #  % ICPアルゴリズムによる、並進ベクトルと回転行列の計算を実施する関数 
28- #  % data1 = [x(t)1 x(t)2 x(t)3 ...] 
29- #  % data2 = [x(t+1)1 x(t+1)2 x(t+1)3 ...] 
30- #  % x=[x y z]' 
31- 
32- 
33- #  while ~(dError < EPS) 
34- #  count=count+1; 
35- 
36- #  [ii, error]=FindNearestPoint(data1, data2);%最近傍点探索 
37- #  [R1, t1]=SVDMotionEstimation(data1, data2, ii);%特異値分解による移動量推定 
38- #  %計算したRとtで点群とRとtの値を更新 
39- #  data2=R1*data2; 
40- #  data2=[data2(1,:)+t1(1) ; data2(2,:)+t1(2)]; 
41- #  R = R1*R; 
42- #  t = R1*t + t1; 
43- 
44- #  dError=abs(preError-error);%エラーの改善量 
45- #  preError=error;%一つ前のエラーの総和値を保存 
46- 
47- #  if count > maxIter %収束しなかった 
48- #  disp('Max Iteration');return; 
49- #  end 
50- #  end 
51- #  disp(['Convergence:',num2str(count)]); 
52- 
53- #  function [index, error]=FindNearestPoint(data1, data2) 
54- #  %data2に対するdata1の最近傍点のインデックスを計算する関数 
55- #  m1=size(data1,2); 
56- #  m2=size(data2,2); 
57- #  index=[]; 
58- #  error=0; 
59- 
60- #  for i=1:m1 
61- #  dx=(data2-repmat(data1(:,i),1,m2)); 
62- #  dist=sqrt(dx(1,:).^2+dx(2,:).^2); 
63- #  [dist, ii]=min(dist); 
64- #  index=[index; ii]; 
65- #  error=error+dist; 
66- #  end 
67- 
68- #  function [R, t]=SVDMotionEstimation(data1, data2, index) 
69- #  %特異値分解法による並進ベクトルと、回転行列の計算 
70- 
71- #  %各点群の重心の計算 
72- #  M = data1; 
73- #  mm = mean(M,2); 
74- #  S = data2(:,index); 
75- #  ms = mean(S,2); 
76- 
77- #  %各点群を重心中心の座標系に変換 
78- #  Sshifted = [S(1,:)-ms(1); S(2,:)-ms(2);]; 
79- #  Mshifted = [M(1,:)-mm(1); M(2,:)-mm(2);]; 
80- 
81- #  W = Sshifted*Mshifted'; 
82- #  [U,A,V] = svd(W);%特異値分解 
83- 
84- #  R = (U*V')';%回転行列の計算 
85- #  t = mm - R*ms;%並進ベクトルの計算 
8613
8714def  update_homogenerous_matrix (Hin , R , T ):
88-     print (R )
8915
9016    H  =  np .matrix (np .zeros ((3 , 3 )))  # translation vector 
9117
@@ -105,9 +31,7 @@ def update_homogenerous_matrix(Hin, R, T):
10531
10632
10733def  ICP_matching (pdata , data ):
108-     R  =  np .eye (2 )  # rotation matrix 
109-     T  =  np .zeros ((2 , 1 ))  # translation vector 
110-     H  =  None   # translation vector 
34+     H  =  None   # homogeneraous transformation matrix 
11135
11236    #  ICP 
11337    EPS  =  0.0001 
@@ -131,30 +55,29 @@ def ICP_matching(pdata, data):
13155        preError  =  error 
13256
13357        if  dError  <=  EPS :
134-             print ("Converge" , dError )
135-             plt .plot (data [0 , :], data [1 , :], "*k" )
58+             print ("Converge" , dError , count )
13659            break 
13760        elif  maxIter  <=  count :
13861            break 
13962
14063    R  =  np .matrix (H [0 :2 , 0 :2 ])
14164    T  =  np .matrix (H [0 :2 , 2 ])
142-     print (H )
143-     print (R )
144-     print (T )
14565
14666    return  R , T 
14767
14868
14969def  nearest_neighbor_assosiation (pdata , data ):
15070
15171    ddata  =  pdata  -  data 
152-     #  print(ddata) 
15372
15473    d  =  np .linalg .norm (ddata , axis = 0 )
15574
15675    error  =  sum (d )
15776
77+     for  i  in  range (data .shape [1 ]):
78+         for  ii  in  range (data .shape [1 ]):
79+             print (i )
80+ 
15881    return  error 
15982
16083
0 commit comments