Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. function optimal_RtZ=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. %Ensure all input matrices have the expected shape
  35. x=reshape(x',2,[]);
  36. % y=reshape(y,3,[]);
  37. %%Unpack the unknown parameters
  38. R=reshape(RtZ_Ri(1:4),2,2);
  39. t=RtZ_Ri(5:6);
  40. Z=reshape((RtZ_Ri(7:(7+dim-1))).',[],2) ;
  41. Ri = reshape(RtZ_Ri((7+dim):end),[],size(y,1))';
  42.  
  43.  
  44.  
  45. %%Compute the error terms
  46.  
  47. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  48. % % Create kd-tree
  49. % kd = KDTreeSearcher(y);
  50. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. % % kd-tree look-up
  52. % idz = knnsearch(kd,Z);
  53. % PyZ = y(idz,:)
  54.  
  55. [PyZ,distance,t_a] = distance2curve(y,Z,'spline');
  56.  
  57. % NP = NY(idz,:)
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59.  
  60.  
  61.  
  62. Ematch=w(1)*norm(Z-PyZ,'fro')^2;
  63.  
  64.  
  65. Rxplust=bsxfun(@plus,R*x,t);
  66. Eprior=w(2)*norm(Z-Rxplust', 'fro')^2;
  67.  
  68. for j=1:size(Z,1)
  69.  
  70. for i=1:size(Z,1)
  71.  
  72. Eprior2=w(3)*norm((Z(j)-Z(i))-(reshape((Ri(i,:))',2,2) *(x(j)-x(i))),'fro')^2;
  73.  
  74. end
  75. end
  76.  
  77.  
  78. %%Compute total objective
  79. Ereg=Ematch+Eprior2+Eprior;
  80. end
  81.  
  82.  
  83.  
  84. function show_contour_plot(X,Y)
  85.  
  86. X(end+1,:) = X(1,:);
  87. Y(end+1,:) = Y(1,:);
  88. plot(X(:,1), X(:,2), '.', 'color',[ 0 0 .9]); hold on;
  89. plot(Y(:,1), Y(:,2), 'x', 'color',[.5 .5 0]); hold on;
  90. drawnow;
  91. end
  92.  
  93. function [X Y] = mean_center_and_normalize(X,Y)
  94. XnY = [X;Y];
  95. avg = mean(XnY);
  96. X = X - repmat(avg, [size(X,1),1]);
  97. Y = Y - repmat(avg, [size(Y,1),1]);
  98. XnY = [X;Y];
  99. d = sqrt(max(sum(XnY.^2,2)));
  100. X = X./d;
  101. Y = Y./d;
  102. end
  103.  
  104.  
  105. function [c,ceq]=nonlcon(params)
  106. %Ensure R is a rotation matrix (orthogonal with determinant=1)
  107. R=reshape(params(1:4),2,2);
  108. v=R*R'-eye(2);
  109. ceq(5,1) = det(R)-1;
  110. ceq(1:4)= v(:);
  111. c=[];
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement