load_untouch_nii.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. % Load NIFTI or ANALYZE dataset, but not applying any appropriate affine
  2. % geometric transform or voxel intensity scaling.
  3. %
  4. % Although according to NIFTI website, all those header information are
  5. % supposed to be applied to the loaded NIFTI image, there are some
  6. % situations that people do want to leave the original NIFTI header and
  7. % data untouched. They will probably just use MATLAB to do certain image
  8. % processing regardless of image orientation, and to save data back with
  9. % the same NIfTI header.
  10. %
  11. % Since this program is only served for those situations, please use it
  12. % together with "save_untouch_nii.m", and do not use "save_nii.m" or
  13. % "view_nii.m" for the data that is loaded by "load_untouch_nii.m". For
  14. % normal situation, you should use "load_nii.m" instead.
  15. %
  16. % Usage: nii = load_untouch_nii(filename, [img_idx], [dim5_idx], [dim6_idx], ...
  17. % [dim7_idx], [old_RGB], [slice_idx])
  18. %
  19. % filename - NIFTI or ANALYZE file name.
  20. %
  21. % img_idx (optional) - a numerical array of image volume indices.
  22. % Only the specified volumes will be loaded. All available image
  23. % volumes will be loaded, if it is default or empty.
  24. %
  25. % The number of images scans can be obtained from get_nii_frame.m,
  26. % or simply: hdr.dime.dim(5).
  27. %
  28. % dim5_idx (optional) - a numerical array of 5th dimension indices.
  29. % Only the specified range will be loaded. All available range
  30. % will be loaded, if it is default or empty.
  31. %
  32. % dim6_idx (optional) - a numerical array of 6th dimension indices.
  33. % Only the specified range will be loaded. All available range
  34. % will be loaded, if it is default or empty.
  35. %
  36. % dim7_idx (optional) - a numerical array of 7th dimension indices.
  37. % Only the specified range will be loaded. All available range
  38. % will be loaded, if it is default or empty.
  39. %
  40. % old_RGB (optional) - a scale number to tell difference of new RGB24
  41. % from old RGB24. New RGB24 uses RGB triple sequentially for each
  42. % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect
  43. % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for
  44. % each slices. If the image that you view is garbled, try to set
  45. % old_RGB variable to 1 and try again, because it could be in
  46. % old RGB24. It will be set to 0, if it is default or empty.
  47. %
  48. % slice_idx (optional) - a numerical array of image slice indices.
  49. % Only the specified slices will be loaded. All available image
  50. % slices will be loaded, if it is default or empty.
  51. %
  52. % Returned values:
  53. %
  54. % nii structure:
  55. %
  56. % hdr - struct with NIFTI header fields.
  57. %
  58. % filetype - Analyze format .hdr/.img (0);
  59. % NIFTI .hdr/.img (1);
  60. % NIFTI .nii (2)
  61. %
  62. % fileprefix - NIFTI filename without extension.
  63. %
  64. % machine - machine string variable.
  65. %
  66. % img - 3D (or 4D) matrix of NIFTI data.
  67. %
  68. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  69. %
  70. function nii = load_untouch_nii(filename, img_idx, dim5_idx, dim6_idx, dim7_idx, ...
  71. old_RGB, slice_idx)
  72. if ~exist('filename','var')
  73. error('Usage: nii = load_untouch_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [slice_idx])');
  74. end
  75. if ~exist('img_idx','var') | isempty(img_idx)
  76. img_idx = [];
  77. end
  78. if ~exist('dim5_idx','var') | isempty(dim5_idx)
  79. dim5_idx = [];
  80. end
  81. if ~exist('dim6_idx','var') | isempty(dim6_idx)
  82. dim6_idx = [];
  83. end
  84. if ~exist('dim7_idx','var') | isempty(dim7_idx)
  85. dim7_idx = [];
  86. end
  87. if ~exist('old_RGB','var') | isempty(old_RGB)
  88. old_RGB = 0;
  89. end
  90. if ~exist('slice_idx','var') | isempty(slice_idx)
  91. slice_idx = [];
  92. end
  93. v = version;
  94. % Check file extension. If .gz, unpack it into temp folder
  95. %
  96. if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
  97. if ~strcmp(filename(end-6:end), '.img.gz') & ...
  98. ~strcmp(filename(end-6:end), '.hdr.gz') & ...
  99. ~strcmp(filename(end-6:end), '.nii.gz')
  100. error('Please check filename.');
  101. end
  102. if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
  103. error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
  104. elseif strcmp(filename(end-6:end), '.img.gz')
  105. filename1 = filename;
  106. filename2 = filename;
  107. filename2(end-6:end) = '';
  108. filename2 = [filename2, '.hdr.gz'];
  109. tmpDir = tempname;
  110. mkdir(tmpDir);
  111. gzFileName = filename;
  112. filename1 = gunzip(filename1, tmpDir);
  113. filename2 = gunzip(filename2, tmpDir);
  114. filename = char(filename1); % convert from cell to string
  115. elseif strcmp(filename(end-6:end), '.hdr.gz')
  116. filename1 = filename;
  117. filename2 = filename;
  118. filename2(end-6:end) = '';
  119. filename2 = [filename2, '.img.gz'];
  120. tmpDir = tempname;
  121. mkdir(tmpDir);
  122. gzFileName = filename;
  123. filename1 = gunzip(filename1, tmpDir);
  124. filename2 = gunzip(filename2, tmpDir);
  125. filename = char(filename1); % convert from cell to string
  126. elseif strcmp(filename(end-6:end), '.nii.gz')
  127. tmpDir = tempname;
  128. mkdir(tmpDir);
  129. gzFileName = filename;
  130. filename = gunzip(filename, tmpDir);
  131. filename = char(filename); % convert from cell to string
  132. end
  133. end
  134. % Read the dataset header
  135. %
  136. [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename);
  137. if nii.filetype == 0
  138. nii.hdr = load_untouch0_nii_hdr(nii.fileprefix,nii.machine);
  139. nii.ext = [];
  140. else
  141. nii.hdr = load_untouch_nii_hdr(nii.fileprefix,nii.machine,nii.filetype);
  142. % Read the header extension
  143. %
  144. nii.ext = load_nii_ext(filename);
  145. end
  146. % Read the dataset body
  147. %
  148. [nii.img,nii.hdr] = load_untouch_nii_img(nii.hdr,nii.filetype,nii.fileprefix, ...
  149. nii.machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx);
  150. % Perform some of sform/qform transform
  151. %
  152. % nii = xform_nii(nii, tolerance, preferredForm);
  153. nii.untouch = 1;
  154. % Clean up after gunzip
  155. %
  156. if exist('gzFileName', 'var')
  157. % fix fileprefix so it doesn't point to temp location
  158. %
  159. nii.fileprefix = gzFileName(1:end-7);
  160. rmdir(tmpDir,'s');
  161. end
  162. return % load_untouch_nii