rri_orient.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. % Convert image of different orientations to standard Analyze orientation
  2. %
  3. % Usage: nii = rri_orient(nii);
  4. % Jimmy Shen (jimmy@rotman-baycrest.on.ca), 26-APR-04
  5. %___________________________________________________________________
  6. function [nii, orient, pattern] = rri_orient(nii, varargin)
  7. if nargin > 1
  8. pattern = varargin{1};
  9. else
  10. pattern = [];
  11. end
  12. if(nargin > 2)
  13. orient = varargin{2};
  14. if(length(find(orient>6)) || length(find(orient<1))) %value checking
  15. orient=[1 2 3]; %set to default if bogus values set
  16. end
  17. else
  18. orient = [1 2 3];
  19. end
  20. dim = double(nii.hdr.dime.dim([2:4]));
  21. if ~isempty(pattern) & ~isequal(length(pattern), prod(dim))
  22. return;
  23. end
  24. % get orient of the current image
  25. %
  26. if isequal(orient, [1 2 3])
  27. orient = rri_orient_ui;
  28. pause(.1);
  29. end
  30. % no need for conversion
  31. %
  32. if isequal(orient, [1 2 3])
  33. return;
  34. end
  35. if isempty(pattern)
  36. pattern = 1:prod(dim);
  37. end
  38. pattern = reshape(pattern, dim);
  39. img = nii.img;
  40. % calculate after flip orient
  41. %
  42. rot_orient = mod(orient + 2, 3) + 1;
  43. % do flip:
  44. %
  45. flip_orient = orient - rot_orient;
  46. for i = 1:3
  47. if flip_orient(i)
  48. pattern = flipdim(pattern, i);
  49. img = flipdim(img, i);
  50. end
  51. end
  52. % get index of orient (do inverse)
  53. %
  54. [tmp rot_orient] = sort(rot_orient);
  55. % do rotation:
  56. %
  57. pattern = permute(pattern, rot_orient);
  58. img = permute(img, [rot_orient 4 5 6]);
  59. % rotate resolution, or 'dim'
  60. %
  61. new_dim = nii.hdr.dime.dim([2:4]);
  62. new_dim = new_dim(rot_orient);
  63. nii.hdr.dime.dim([2:4]) = new_dim;
  64. % rotate voxel_size, or 'pixdim'
  65. %
  66. tmp = nii.hdr.dime.pixdim([2:4]);
  67. tmp = tmp(rot_orient);
  68. nii.hdr.dime.pixdim([2:4]) = tmp;
  69. % re-calculate originator
  70. %
  71. tmp = nii.hdr.hist.originator([1:3]);
  72. tmp = tmp(rot_orient);
  73. flip_orient = flip_orient(rot_orient);
  74. for i = 1:3
  75. if flip_orient(i) & ~isequal(double(tmp(i)), 0)
  76. tmp(i) = int16(double(new_dim(i)) - double(tmp(i)) + 1);
  77. end
  78. end
  79. nii.hdr.hist.originator([1:3]) = tmp;
  80. nii.img = img;
  81. pattern = pattern(:);
  82. return; % rri_orient