clip_nii.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. % CLIP_NII: Clip the NIfTI volume from any of the 6 sides
  2. %
  3. % Usage: nii = clip_nii(nii, [option])
  4. %
  5. % Inputs:
  6. %
  7. % nii - NIfTI volume.
  8. %
  9. % option - struct instructing how many voxel to be cut from which side.
  10. %
  11. % option.cut_from_L = ( number of voxel )
  12. % option.cut_from_R = ( number of voxel )
  13. % option.cut_from_P = ( number of voxel )
  14. % option.cut_from_A = ( number of voxel )
  15. % option.cut_from_I = ( number of voxel )
  16. % option.cut_from_S = ( number of voxel )
  17. %
  18. % Options description in detail:
  19. % ==============================
  20. %
  21. % cut_from_L: Number of voxels from Left side will be clipped.
  22. %
  23. % cut_from_R: Number of voxels from Right side will be clipped.
  24. %
  25. % cut_from_P: Number of voxels from Posterior side will be clipped.
  26. %
  27. % cut_from_A: Number of voxels from Anterior side will be clipped.
  28. %
  29. % cut_from_I: Number of voxels from Inferior side will be clipped.
  30. %
  31. % cut_from_S: Number of voxels from Superior side will be clipped.
  32. %
  33. % NIfTI data format can be found on: http://nifti.nimh.nih.gov
  34. %
  35. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  36. %
  37. function nii = clip_nii(nii, opt)
  38. dims = abs(nii.hdr.dime.dim(2:4));
  39. origin = abs(nii.hdr.hist.originator(1:3));
  40. if isempty(origin) | all(origin == 0) % according to SPM
  41. origin = round((dims+1)/2);
  42. end
  43. cut_from_L = 0;
  44. cut_from_R = 0;
  45. cut_from_P = 0;
  46. cut_from_A = 0;
  47. cut_from_I = 0;
  48. cut_from_S = 0;
  49. if nargin > 1 & ~isempty(opt)
  50. if ~isstruct(opt)
  51. error('option argument should be a struct');
  52. end
  53. if isfield(opt,'cut_from_L')
  54. cut_from_L = round(opt.cut_from_L);
  55. if cut_from_L >= origin(1) | cut_from_L < 0
  56. error('cut_from_L cannot be negative or cut beyond originator');
  57. end
  58. end
  59. if isfield(opt,'cut_from_P')
  60. cut_from_P = round(opt.cut_from_P);
  61. if cut_from_P >= origin(2) | cut_from_P < 0
  62. error('cut_from_P cannot be negative or cut beyond originator');
  63. end
  64. end
  65. if isfield(opt,'cut_from_I')
  66. cut_from_I = round(opt.cut_from_I);
  67. if cut_from_I >= origin(3) | cut_from_I < 0
  68. error('cut_from_I cannot be negative or cut beyond originator');
  69. end
  70. end
  71. if isfield(opt,'cut_from_R')
  72. cut_from_R = round(opt.cut_from_R);
  73. if cut_from_R > dims(1)-origin(1) | cut_from_R < 0
  74. error('cut_from_R cannot be negative or cut beyond originator');
  75. end
  76. end
  77. if isfield(opt,'cut_from_A')
  78. cut_from_A = round(opt.cut_from_A);
  79. if cut_from_A > dims(2)-origin(2) | cut_from_A < 0
  80. error('cut_from_A cannot be negative or cut beyond originator');
  81. end
  82. end
  83. if isfield(opt,'cut_from_S')
  84. cut_from_S = round(opt.cut_from_S);
  85. if cut_from_S > dims(3)-origin(3) | cut_from_S < 0
  86. error('cut_from_S cannot be negative or cut beyond originator');
  87. end
  88. end
  89. end
  90. nii = make_nii(nii.img( (cut_from_L+1) : (dims(1)-cut_from_R), ...
  91. (cut_from_P+1) : (dims(2)-cut_from_A), ...
  92. (cut_from_I+1) : (dims(3)-cut_from_S), ...
  93. :,:,:,:,:), nii.hdr.dime.pixdim(2:4), ...
  94. [origin(1)-cut_from_L origin(2)-cut_from_P origin(3)-cut_from_I], ...
  95. nii.hdr.dime.datatype, nii.hdr.hist.descrip);
  96. return;