spm_dartel_integrate.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. function [Phi,DPhi] = spm_dartel_integrate(U,t,K)
  2. % Integrate a Dartel flow field
  3. % FORMAT [Phi,DPhi] = spm_dartel_exp(U,t,K)
  4. % U - name of flow field (nx x ny x nz x nt x 3)
  5. % t - [t0 t1] Start and end time (values between 0 and 1)
  6. % K - log2 of the Euler time steps to integrate the
  7. % flow field.
  8. %
  9. % Phi - deformation field (nx x ny x nz x 3)
  10. % DPhi - Jacobian determinant field (nx x ny x nz)
  11. %
  12. % The function integrates
  13. % Phi(x,t) = \int_{t_0}^{t_1} U(Phi(x,t),t) dt
  14. % where U is a piecewise constant flow field
  15. %
  16. % Note: this function is ready for LDDMM-style flow fields, even
  17. % though the none of the official Dartel tools can generate them
  18. % yet.
  19. % _______________________________________________________________________
  20. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  21. % John Ashburner
  22. % $Id: spm_dartel_integrate.m 5506 2013-05-14 17:13:43Z john $
  23. if isa(U,'char'), U = nifti(U); end;
  24. if isa(U,'nifti'), U = U.dat; end;
  25. % Figure out which bits of flow field to use, the numbers of
  26. % Euler time steps, and the scales.
  27. nt = size(U,4);
  28. K = ceil(log2(2^K/nt));
  29. big = 2^12;
  30. t = min(max(t,0),1);
  31. t0 = t(1);
  32. t1 = t(2);
  33. tt0 = t0*nt;
  34. tt1 = t1*nt;
  35. sc = zeros(1,nt);
  36. if t1>t0,
  37. for i=1:nt,
  38. sc(i) = max(round((min(tt1,i)-max(tt0,i-1))*big)/big,0);
  39. end
  40. ind = find(sc~=0);
  41. else
  42. for i=1:nt,
  43. sc(i) = max(round((min(tt0,i)-max(tt1,i-1))*big)/big,0);
  44. end
  45. ind = fliplr(find(sc~=0));
  46. end
  47. if isempty(ind), % t0==t1
  48. ind = 1;
  49. sc = 0;
  50. ts = 0;
  51. else
  52. sc = sc(ind);
  53. ts = zeros(1,numel(ind));
  54. for i=1:numel(ind),
  55. ts(i) = ceil(log2((2^K)*sc(i))-1/big);
  56. sc(i) = sc(i)*2^(K-ts(i));
  57. end
  58. if t0>t1, sc = -sc; end
  59. end
  60. % Do the integrations
  61. if nargout==1,
  62. % Deformation field only
  63. u = squeeze(single(U(:,:,:,ind(1),:)));
  64. Phi = dartel3('Exp',u,[ts(1), sc(1)]);
  65. for i=2:numel(ind),
  66. u = squeeze(single(U(:,:,:,ind(i),:)));
  67. Phi = dartel3('comp',Phi,dartel3('Exp',u,[ts(i), sc(i)]));
  68. end
  69. else
  70. % Deformation and Jacobian determinant fields
  71. u = squeeze(single(U(:,:,:,ind(1),:)));
  72. [Phi,DPhi] = dartel3('Exp',u,[ts(1), sc(1), 1]);
  73. for i=2:numel(ind),
  74. u = squeeze(single(U(:,:,:,ind(i),:)));
  75. [Phi1,DPhi1] = dartel3('Exp',u,[ts(i), sc(i), 1]);
  76. [Phi,DPhi] = dartel3('comp',Phi,Phi1,DPhi,DPhi1);
  77. clear Phi1 DPhi1
  78. end
  79. end