load_nii.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. % Load NIFTI or ANALYZE dataset. Support both *.nii and *.hdr/*.img
  2. % file extension. If file extension is not provided, *.hdr/*.img will
  3. % be used as default.
  4. %
  5. % A subset of NIFTI transform is included. For non-orthogonal rotation,
  6. % shearing etc., please use 'reslice_nii.m' to reslice the NIFTI file.
  7. % It will not cause negative effect, as long as you remember not to do
  8. % slice time correction after reslicing the NIFTI file. Output variable
  9. % nii will be in RAS orientation, i.e. X axis from Left to Right,
  10. % Y axis from Posterior to Anterior, and Z axis from Inferior to
  11. % Superior.
  12. %
  13. % Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], ...
  14. % [dim7_idx], [old_RGB], [tolerance], [preferredForm])
  15. %
  16. % filename - NIFTI or ANALYZE file name.
  17. %
  18. % img_idx (optional) - a numerical array of 4th dimension indices,
  19. % which is the indices of image scan volume. The number of images
  20. % scan volumes can be obtained from get_nii_frame.m, or simply
  21. % hdr.dime.dim(5). Only the specified volumes will be loaded.
  22. % All available image volumes will be loaded, if it is default or
  23. % empty.
  24. %
  25. % dim5_idx (optional) - a numerical array of 5th dimension indices.
  26. % Only the specified range will be loaded. All available range
  27. % will be loaded, if it is default or empty.
  28. %
  29. % dim6_idx (optional) - a numerical array of 6th dimension indices.
  30. % Only the specified range will be loaded. All available range
  31. % will be loaded, if it is default or empty.
  32. %
  33. % dim7_idx (optional) - a numerical array of 7th dimension indices.
  34. % Only the specified range will be loaded. All available range
  35. % will be loaded, if it is default or empty.
  36. %
  37. % old_RGB (optional) - a scale number to tell difference of new RGB24
  38. % from old RGB24. New RGB24 uses RGB triple sequentially for each
  39. % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect
  40. % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for
  41. % each slices. If the image that you view is garbled, try to set
  42. % old_RGB variable to 1 and try again, because it could be in
  43. % old RGB24. It will be set to 0, if it is default or empty.
  44. %
  45. % tolerance (optional) - distortion allowed in the loaded image for any
  46. % non-orthogonal rotation or shearing of NIfTI affine matrix. If
  47. % you set 'tolerance' to 0, it means that you do not allow any
  48. % distortion. If you set 'tolerance' to 1, it means that you do
  49. % not care any distortion. The image will fail to be loaded if it
  50. % can not be tolerated. The tolerance will be set to 0.1 (10%), if
  51. % it is default or empty.
  52. %
  53. % preferredForm (optional) - selects which transformation from voxels
  54. % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate
  55. % "prefer sform or qform, but use others if preferred not present".
  56. % Upper case indicate the program is forced to use the specificied
  57. % tranform or fail loading. 'preferredForm' will be 's', if it is
  58. % default or empty. - Jeff Gunter
  59. %
  60. % Returned values:
  61. %
  62. % nii structure:
  63. %
  64. % hdr - struct with NIFTI header fields.
  65. %
  66. % filetype - Analyze format .hdr/.img (0);
  67. % NIFTI .hdr/.img (1);
  68. % NIFTI .nii (2)
  69. %
  70. % fileprefix - NIFTI filename without extension.
  71. %
  72. % machine - machine string variable.
  73. %
  74. % img - 3D (or 4D) matrix of NIFTI data.
  75. %
  76. % original - the original header before any affine transform.
  77. %
  78. % Part of this file is copied and modified from:
  79. % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
  80. %
  81. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  82. %
  83. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  84. %
  85. function nii = load_nii(filename, img_idx, dim5_idx, dim6_idx, dim7_idx, ...
  86. old_RGB, tolerance, preferredForm)
  87. if ~exist('filename','var')
  88. error('Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [tolerance], [preferredForm])');
  89. end
  90. if ~exist('img_idx','var') | isempty(img_idx)
  91. img_idx = [];
  92. end
  93. if ~exist('dim5_idx','var') | isempty(dim5_idx)
  94. dim5_idx = [];
  95. end
  96. if ~exist('dim6_idx','var') | isempty(dim6_idx)
  97. dim6_idx = [];
  98. end
  99. if ~exist('dim7_idx','var') | isempty(dim7_idx)
  100. dim7_idx = [];
  101. end
  102. if ~exist('old_RGB','var') | isempty(old_RGB)
  103. old_RGB = 0;
  104. end
  105. if ~exist('tolerance','var') | isempty(tolerance)
  106. tolerance = 0.1; % 10 percent
  107. end
  108. if ~exist('preferredForm','var') | isempty(preferredForm)
  109. preferredForm= 's'; % Jeff
  110. end
  111. v = version;
  112. % Check file extension. If .gz, unpack it into temp folder
  113. %
  114. if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
  115. if ~strcmp(filename(end-6:end), '.img.gz') & ...
  116. ~strcmp(filename(end-6:end), '.hdr.gz') & ...
  117. ~strcmp(filename(end-6:end), '.nii.gz')
  118. error('Please check filename.');
  119. end
  120. if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
  121. error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
  122. elseif strcmp(filename(end-6:end), '.img.gz')
  123. filename1 = filename;
  124. filename2 = filename;
  125. filename2(end-6:end) = '';
  126. filename2 = [filename2, '.hdr.gz'];
  127. tmpDir = tempname;
  128. mkdir(tmpDir);
  129. gzFileName = filename;
  130. filename1 = gunzip(filename1, tmpDir);
  131. filename2 = gunzip(filename2, tmpDir);
  132. filename = char(filename1); % convert from cell to string
  133. elseif strcmp(filename(end-6:end), '.hdr.gz')
  134. filename1 = filename;
  135. filename2 = filename;
  136. filename2(end-6:end) = '';
  137. filename2 = [filename2, '.img.gz'];
  138. tmpDir = tempname;
  139. mkdir(tmpDir);
  140. gzFileName = filename;
  141. filename1 = gunzip(filename1, tmpDir);
  142. filename2 = gunzip(filename2, tmpDir);
  143. filename = char(filename1); % convert from cell to string
  144. elseif strcmp(filename(end-6:end), '.nii.gz')
  145. tmpDir = tempname;
  146. mkdir(tmpDir);
  147. gzFileName = filename;
  148. filename = gunzip(filename, tmpDir);
  149. filename = char(filename); % convert from cell to string
  150. end
  151. end
  152. % Read the dataset header
  153. %
  154. [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename);
  155. % Read the header extension
  156. %
  157. % nii.ext = load_nii_ext(filename);
  158. % Read the dataset body
  159. %
  160. [nii.img,nii.hdr] = load_nii_img(nii.hdr,nii.filetype,nii.fileprefix, ...
  161. nii.machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB);
  162. % Perform some of sform/qform transform
  163. %
  164. nii = xform_nii(nii, tolerance, preferredForm);
  165. % Clean up after gunzip
  166. %
  167. if exist('gzFileName', 'var')
  168. % fix fileprefix so it doesn't point to temp location
  169. %
  170. nii.fileprefix = gzFileName(1:end-7);
  171. rmdir(tmpDir,'s');
  172. end
  173. return % load_nii