Guest User

Untitled

a guest
Sep 29th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. function Kate_Attempt_RunOpt_Eqn13()
  2.  
  3. clc;clear all;
  4. X = [0.326118759777820,-0.277140616245993;0.437080391770018,-0.0558590878401746;0.548042023762216,0.162592814787714;0.659003655754414,0.375821686878482;0.769965287746612,0.581491348288492;]
  5. Y = [-0.698446913206480,-0.715661868085998;-0.625305502069696,-0.444146525051125;-0.503232714874489,-0.197186449680767;-0.475138257846766,0.126591717108409;-0.438086730813650,0.443496979840961;]
  6. [X Y] = mean_center_and_normalize(X,Y);
  7.  
  8. Z= X;
  9. dim = size(Z,1)*size(Z,2);
  10.  
  11. theta = 0;
  12. R = [cos(theta), -sin(theta); sin(theta) cos(theta)];
  13. t = [0 0];
  14. Ri = repmat(R(:),size(X,1),1);
  15.  
  16. RtZ_Ri_guess = [R(:);t(:);Z(:);Ri];
  17.  
  18.  
  19. w = [1;1;1]; %weights
  20.  
  21. optimal_RtZ =fmincon(@(RtZ_Ri)fun_Eqn13(RtZ_Ri,X,Y,w,dim) ,RtZ_Ri_guess,...
  22. [],[],[],[],[],[],@nonlcon);
  23.  
  24.  
  25. optimal_Z=reshape((optimal_RtZ(7:7+dim-1)).',[],2)
  26.  
  27. figure, show_contour_plot(Z,Y)
  28. figure, show_contour_plot(optimal_Z,Y)
  29.  
  30. end
  31.  
  32.  
  33. function Ereg=fun_Eqn13(RtZ_Ri,x,y,w,dim)
  34.  
  35. %%Ensure all input matrices have the expected shape
  36. x=reshape(x',2,[]);
  37. % y=reshape(y,3,[]);
  38. %%Unpack the unknown parameters
  39. R=reshape(RtZ_Ri(1:4),2,2);
  40. t=RtZ_Ri(5:6);
  41. Z=reshape((RtZ_Ri(7:(7+dim-1))).',[],2) ;
  42.  
  43. %%Compute the error terms
  44. % PyZ=... %implement with distance2curve()
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. % Create kd-tree
  47. kd = KDTreeSearcher(y);
  48. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49. % kd-tree look-up
  50. idz = knnsearch(kd,Z);
  51. PyZ = y(idz,:);
  52. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  53.  
  54. Ematch=w(1)*norm(Z-PyZ,'fro')^2;
  55. Rxplust=bsxfun(@plus,R*x,t);
  56. Eprior=w(2)*norm(Z-Rxplust', 'fro')^2;
  57.  
  58. for j=1:size(Z,1)
  59. start=7+dim-1;
  60. for i=1:size(Z,1)
  61. Ri = reshape(RtZ_Ri((start+1):start+4),2,2);
  62. start=start+4;
  63.  
  64. Eprior2=w(3)*norm((Z(j)-Z(i))-Ri*(x(j)-x(i)),'fro')^2;
  65. end
  66.  
  67. end
  68.  
  69.  
  70. %%Compute total objective
  71. Ereg=Ematch+Eprior+Eprior2;
  72. end
  73.  
  74. function show_contour_plot(X,Y)
  75.  
  76. X(end+1,:) = X(1,:);
  77. Y(end+1,:) = Y(1,:);
  78. plot(X(:,1), X(:,2), '.', 'color',[ 0 0 .9]); hold on;
  79. plot(Y(:,1), Y(:,2), 'x', 'color',[.5 .5 0]); hold on;
  80. drawnow;
  81. end
  82.  
  83. function [X Y] = mean_center_and_normalize(X,Y)
  84. XnY = [X;Y];
  85. avg = mean(XnY);
  86. X = X - repmat(avg, [size(X,1),1]);
  87. Y = Y - repmat(avg, [size(Y,1),1]);
  88. XnY = [X;Y];
  89. d = sqrt(max(sum(XnY.^2,2)));
  90. X = X./d;
  91. Y = Y./d;
  92. end
  93.  
  94.  
  95. function [c,ceq]=nonlcon(params)
  96. %Ensure R is a rotation matrix (orthogonal with determinant=1)
  97. R=reshape(params(1:4),2,2);
  98. v=R*R'-eye(2);
  99. ceq(5,1) = det(R)-1;
  100. ceq(1:4)= v(:);
  101. c=[];
  102. end
Advertisement
Add Comment
Please, Sign In to add comment