PageRenderTime 128ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/master/imip/ex05_non_rigid_registration/demon_registration/functions_affine/affine_registration_error.m

http://uniprojectsdn.googlecode.com/
Objective C | 144 lines | 131 code | 13 blank | 0 comment | 14 complexity | f6a625e6b208df437a1138e75454a64d MD5 | raw file
Possible License(s): BSD-2-Clause
  1. function [e,egrad]=affine_registration_error(par,scale,I1,I2,type,mode)
  2. % This function affine_registration_error, uses affine transfomation of the
  3. % 3D input volume and calculates the registration error after transformation.
  4. %
  5. % [e,egrad]=affine_registration_error(parameters,scale,I1,I2,type,Grid,Spacing,MaskI1,MaskI2,Points1,Points2,PStrength,mode);
  6. %
  7. % input,
  8. % parameters (in 2D) : Rigid vector of length 3 -> [translateX translateY rotate]
  9. % or Affine vector of length 7 -> [translateX translateY
  10. % rotate resizeX resizeY shearXY
  11. % shearYX]
  12. %
  13. % parameters (in 3D) : Rigid vector of length 6 : [translateX translateY translateZ
  14. % rotateX rotateY rotateZ]
  15. % or Affine vector of length 15 : [translateX translateY translateZ,
  16. % rotateX rotateY rotateZ resizeX resizeY
  17. % resizeZ,
  18. % shearXY, shearXZ, shearYX, shearYZ, shearZX, shearZY]
  19. %
  20. % scale: Vector with Scaling of the input parameters with the same lenght
  21. % as the parameter vector.
  22. % I1: The 2D/3D image which is rigid or affine transformed
  23. % I2: The second 2D/3D image which is used to calculate the
  24. % registration error
  25. % type: The type of registration error used see registration_error.m
  26. % (optional)
  27. % mode: If 0: linear interpolation and outside pixels set to nearest pixel
  28. % 1: linear interpolation and outside pixels set to zero
  29. % 2: cubic interpolation and outsite pixels set to nearest pixel
  30. % 3: cubic interpolation and outside pixels set to zero
  31. %
  32. % outputs,
  33. % e: registration error between I1 and I2
  34. % egrad: error gradient of input parameters
  35. % example,
  36. % see example_3d_affine.m
  37. %
  38. % Function is written by D.Kroon University of Twente (April 2009)
  39. if(~exist('mode','var')), mode=0; end
  40. % Scale the inputs
  41. par=par.*scale;
  42. % Delta
  43. delta=1e-5;
  44. % Special case for simple squared difference (speed optimized code)
  45. if((size(I1,3)>3)&&(strcmp(type,'sd')))
  46. M=getransformation_matrix(par);
  47. if(isa(I1,'double'))
  48. if(nargout>1)
  49. [e,mgrad]=affine_error_3d_double(double(I1),double(I2),double(M),double(mode));
  50. Me=[mgrad(1) mgrad(2) mgrad(3) mgrad(4);
  51. mgrad(5) mgrad(6) mgrad(7) mgrad(8);
  52. mgrad(9) mgrad(10) mgrad(11) mgrad(12);
  53. 0 0 0 0];
  54. egrad=zeros(1,length(par));
  55. for i=1:length(par)
  56. par2=par;
  57. par2(i)=par(i)+delta*scale(i);
  58. Mg=getransformation_matrix(par2);
  59. diffM=(Mg-M).*Me;
  60. egrad(i)=sum(diffM(:))/delta;
  61. end
  62. else
  63. e=affine_error_3d_double(double(I1),double(I2),double(M),double(mode));
  64. end
  65. else
  66. if(nargout>1)
  67. [e,mgrad]=affine_error_3d_single(single(I1),single(I2),single(M),single(mode));
  68. Me=[mgrad(1) mgrad(2) mgrad(3) mgrad(4);
  69. mgrad(5) mgrad(6) mgrad(7) mgrad(8);
  70. mgrad(9) mgrad(10) mgrad(11) mgrad(12);
  71. 0 0 0 0];
  72. egrad=zeros(1,length(par));
  73. for i=1:length(par)
  74. par2=par;
  75. par2(i)=par(i)+delta*scale(i);
  76. Mg=getransformation_matrix(par2);
  77. diffM=(Mg-M).*Me;
  78. egrad(i)=sum(diffM(:))/delta;
  79. end
  80. else
  81. e=affine_error_3d_single(single(I1),single(I2),single(M),single(mode));
  82. end
  83. end
  84. return;
  85. end
  86. % Normal error calculation between the two images, and error gradient if needed
  87. % by final differences
  88. if(size(I1,3)<4)
  89. e=affine_registration_error_2d(par,I1,I2,type,mode);
  90. if(nargout>1)
  91. egrad=zeros(1,length(par));
  92. for i=1:length(par)
  93. par2=par; par2(i)=par(i)+delta*scale(i);
  94. egrad(i)=(affine_registration_error_2d(par2,I1,I2,type,mode)-e)/delta;
  95. end
  96. end
  97. else
  98. e=affine_registration_error_3d(par,I1,I2,type,mode);
  99. if(nargout>1)
  100. egrad=zeros(1,length(par));
  101. for i=1:length(par)
  102. par2=par; par2(i)=par(i)+delta*scale(i);
  103. egrad(i)=(affine_registration_error_3d(par2,I1,I2,type,mode)-e)/delta;
  104. end
  105. end
  106. end
  107. function e=affine_registration_error_2d(par,I1,I2,type,mode)
  108. M=getransformation_matrix(par);
  109. I3=affine_transform(I1,M,mode);
  110. % registration error calculation.
  111. e = image_difference(I3,I2,type);
  112. function e=affine_registration_error_3d(par,I1,I2,type,mode)
  113. M=getransformation_matrix(par);
  114. I3=affine_transform(I1,M,mode);
  115. % registration error calculation.
  116. e = image_difference(I3,I2,type);
  117. function M=getransformation_matrix(par)
  118. switch(length(par))
  119. case 6 %3d
  120. M=make_transformation_matrix(par(1:3),par(4:6));
  121. case 9 %3d
  122. M=make_transformation_matrix(par(1:3),par(4:6),par(7:9));
  123. case 15 %3d
  124. M=make_transformation_matrix(par(1:3),par(4:6),par(7:9),par(10:15));
  125. case 3 % 2d
  126. M=make_transformation_matrix(par(1:2),par(3));
  127. case 5 % 2d
  128. M=make_transformation_matrix(par(1:2),par(3),par(4:5));
  129. case 7 % 2d
  130. M=make_transformation_matrix(par(1:2),par(3),par(4:5),par(6:7));
  131. end