pad_nii.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. % PAD_NII: Pad the NIfTI volume from any of the 6 sides
  2. %
  3. % Usage: nii = pad_nii(nii, [option])
  4. %
  5. % Inputs:
  6. %
  7. % nii - NIfTI volume.
  8. %
  9. % option - struct instructing how many voxel to be padded from which side.
  10. %
  11. % option.pad_from_L = ( number of voxel )
  12. % option.pad_from_R = ( number of voxel )
  13. % option.pad_from_P = ( number of voxel )
  14. % option.pad_from_A = ( number of voxel )
  15. % option.pad_from_I = ( number of voxel )
  16. % option.pad_from_S = ( number of voxel )
  17. % option.bg = [0]
  18. %
  19. % Options description in detail:
  20. % ==============================
  21. %
  22. % pad_from_L: Number of voxels from Left side will be padded.
  23. %
  24. % pad_from_R: Number of voxels from Right side will be padded.
  25. %
  26. % pad_from_P: Number of voxels from Posterior side will be padded.
  27. %
  28. % pad_from_A: Number of voxels from Anterior side will be padded.
  29. %
  30. % pad_from_I: Number of voxels from Inferior side will be padded.
  31. %
  32. % pad_from_S: Number of voxels from Superior side will be padded.
  33. %
  34. % bg: Background intensity, which is 0 by default.
  35. %
  36. % NIfTI data format can be found on: http://nifti.nimh.nih.gov
  37. %
  38. % - Jimmy Shen (jshen@research.baycrest.org)
  39. %
  40. function nii = pad_nii(nii, opt)
  41. dims = abs(nii.hdr.dime.dim(2:4));
  42. origin = abs(nii.hdr.hist.originator(1:3));
  43. if isempty(origin) | all(origin == 0) % according to SPM
  44. origin = round((dims+1)/2);
  45. end
  46. pad_from_L = 0;
  47. pad_from_R = 0;
  48. pad_from_P = 0;
  49. pad_from_A = 0;
  50. pad_from_I = 0;
  51. pad_from_S = 0;
  52. bg = 0;
  53. if nargin > 1 & ~isempty(opt)
  54. if ~isstruct(opt)
  55. error('option argument should be a struct');
  56. end
  57. if isfield(opt,'pad_from_L')
  58. pad_from_L = round(opt.pad_from_L);
  59. if pad_from_L >= origin(1) | pad_from_L < 0
  60. error('pad_from_L cannot be negative');
  61. end
  62. end
  63. if isfield(opt,'pad_from_P')
  64. pad_from_P = round(opt.pad_from_P);
  65. if pad_from_P >= origin(2) | pad_from_P < 0
  66. error('pad_from_P cannot be negative');
  67. end
  68. end
  69. if isfield(opt,'pad_from_I')
  70. pad_from_I = round(opt.pad_from_I);
  71. if pad_from_I >= origin(3) | pad_from_I < 0
  72. error('pad_from_I cannot be negative');
  73. end
  74. end
  75. if isfield(opt,'pad_from_R')
  76. pad_from_R = round(opt.pad_from_R);
  77. if pad_from_R > dims(1)-origin(1) | pad_from_R < 0
  78. error('pad_from_R cannot be negative');
  79. end
  80. end
  81. if isfield(opt,'pad_from_A')
  82. pad_from_A = round(opt.pad_from_A);
  83. if pad_from_A > dims(2)-origin(2) | pad_from_A < 0
  84. error('pad_from_A cannot be negative');
  85. end
  86. end
  87. if isfield(opt,'pad_from_S')
  88. pad_from_S = round(opt.pad_from_S);
  89. if pad_from_S > dims(3)-origin(3) | pad_from_S < 0
  90. error('pad_from_S cannot be negative');
  91. end
  92. end
  93. if isfield(opt,'bg')
  94. bg = opt.bg;
  95. end
  96. end
  97. blk = bg * ones( pad_from_L, dims(2), dims(3) );
  98. nii.img = cat(1, blk, nii.img);
  99. blk = bg * ones( pad_from_R, dims(2), dims(3) );
  100. nii.img = cat(1, nii.img, blk);
  101. dims = size(nii.img);
  102. blk = bg * ones( dims(1), pad_from_P, dims(3) );
  103. nii.img = cat(2, blk, nii.img);
  104. blk = bg * ones( dims(1), pad_from_A, dims(3) );
  105. nii.img = cat(2, nii.img, blk);
  106. dims = size(nii.img);
  107. blk = bg * ones( dims(1), dims(2), pad_from_I );
  108. nii.img = cat(3, blk, nii.img);
  109. blk = bg * ones( dims(1), dims(2), pad_from_S );
  110. nii.img = cat(3, nii.img, blk);
  111. nii = make_nii(nii.img, nii.hdr.dime.pixdim(2:4), ...
  112. [origin(1)+pad_from_L origin(2)+pad_from_P origin(3)+pad_from_I], ...
  113. nii.hdr.dime.datatype, nii.hdr.hist.descrip);
  114. return;