load_untouch_nii_img.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. % internal function
  2. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  3. function [img,hdr] = load_untouch_nii_img(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx)
  4. if ~exist('hdr','var') | ~exist('filetype','var') | ~exist('fileprefix','var') | ~exist('machine','var')
  5. error('Usage: [img,hdr] = load_nii_img(hdr,filetype,fileprefix,machine,[img_idx],[dim5_idx],[dim6_idx],[dim7_idx],[old_RGB],[slice_idx]);');
  6. end
  7. if ~exist('img_idx','var') | isempty(img_idx) | hdr.dime.dim(5)<1
  8. img_idx = [];
  9. end
  10. if ~exist('dim5_idx','var') | isempty(dim5_idx) | hdr.dime.dim(6)<1
  11. dim5_idx = [];
  12. end
  13. if ~exist('dim6_idx','var') | isempty(dim6_idx) | hdr.dime.dim(7)<1
  14. dim6_idx = [];
  15. end
  16. if ~exist('dim7_idx','var') | isempty(dim7_idx) | hdr.dime.dim(8)<1
  17. dim7_idx = [];
  18. end
  19. if ~exist('old_RGB','var') | isempty(old_RGB)
  20. old_RGB = 0;
  21. end
  22. if ~exist('slice_idx','var') | isempty(slice_idx) | hdr.dime.dim(4)<1
  23. slice_idx = [];
  24. end
  25. % check img_idx
  26. %
  27. if ~isempty(img_idx) & ~isnumeric(img_idx)
  28. error('"img_idx" should be a numerical array.');
  29. end
  30. if length(unique(img_idx)) ~= length(img_idx)
  31. error('Duplicate image index in "img_idx"');
  32. end
  33. if ~isempty(img_idx) & (min(img_idx) < 1 | max(img_idx) > hdr.dime.dim(5))
  34. max_range = hdr.dime.dim(5);
  35. if max_range == 1
  36. error(['"img_idx" should be 1.']);
  37. else
  38. range = ['1 ' num2str(max_range)];
  39. error(['"img_idx" should be an integer within the range of [' range '].']);
  40. end
  41. end
  42. % check dim5_idx
  43. %
  44. if ~isempty(dim5_idx) & ~isnumeric(dim5_idx)
  45. error('"dim5_idx" should be a numerical array.');
  46. end
  47. if length(unique(dim5_idx)) ~= length(dim5_idx)
  48. error('Duplicate index in "dim5_idx"');
  49. end
  50. if ~isempty(dim5_idx) & (min(dim5_idx) < 1 | max(dim5_idx) > hdr.dime.dim(6))
  51. max_range = hdr.dime.dim(6);
  52. if max_range == 1
  53. error(['"dim5_idx" should be 1.']);
  54. else
  55. range = ['1 ' num2str(max_range)];
  56. error(['"dim5_idx" should be an integer within the range of [' range '].']);
  57. end
  58. end
  59. % check dim6_idx
  60. %
  61. if ~isempty(dim6_idx) & ~isnumeric(dim6_idx)
  62. error('"dim6_idx" should be a numerical array.');
  63. end
  64. if length(unique(dim6_idx)) ~= length(dim6_idx)
  65. error('Duplicate index in "dim6_idx"');
  66. end
  67. if ~isempty(dim6_idx) & (min(dim6_idx) < 1 | max(dim6_idx) > hdr.dime.dim(7))
  68. max_range = hdr.dime.dim(7);
  69. if max_range == 1
  70. error(['"dim6_idx" should be 1.']);
  71. else
  72. range = ['1 ' num2str(max_range)];
  73. error(['"dim6_idx" should be an integer within the range of [' range '].']);
  74. end
  75. end
  76. % check dim7_idx
  77. %
  78. if ~isempty(dim7_idx) & ~isnumeric(dim7_idx)
  79. error('"dim7_idx" should be a numerical array.');
  80. end
  81. if length(unique(dim7_idx)) ~= length(dim7_idx)
  82. error('Duplicate index in "dim7_idx"');
  83. end
  84. if ~isempty(dim7_idx) & (min(dim7_idx) < 1 | max(dim7_idx) > hdr.dime.dim(8))
  85. max_range = hdr.dime.dim(8);
  86. if max_range == 1
  87. error(['"dim7_idx" should be 1.']);
  88. else
  89. range = ['1 ' num2str(max_range)];
  90. error(['"dim7_idx" should be an integer within the range of [' range '].']);
  91. end
  92. end
  93. % check slice_idx
  94. %
  95. if ~isempty(slice_idx) & ~isnumeric(slice_idx)
  96. error('"slice_idx" should be a numerical array.');
  97. end
  98. if length(unique(slice_idx)) ~= length(slice_idx)
  99. error('Duplicate index in "slice_idx"');
  100. end
  101. if ~isempty(slice_idx) & (min(slice_idx) < 1 | max(slice_idx) > hdr.dime.dim(4))
  102. max_range = hdr.dime.dim(4);
  103. if max_range == 1
  104. error(['"slice_idx" should be 1.']);
  105. else
  106. range = ['1 ' num2str(max_range)];
  107. error(['"slice_idx" should be an integer within the range of [' range '].']);
  108. end
  109. end
  110. [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx);
  111. return % load_nii_img
  112. %---------------------------------------------------------------------
  113. function [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx)
  114. switch filetype
  115. case {0, 1}
  116. fn = [fileprefix '.img'];
  117. case 2
  118. fn = [fileprefix '.nii'];
  119. end
  120. fid = fopen(fn,'r',machine);
  121. if fid < 0,
  122. msg = sprintf('Cannot open file %s.',fn);
  123. error(msg);
  124. end
  125. % Set bitpix according to datatype
  126. %
  127. % /*Acceptable values for datatype are*/
  128. %
  129. % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN
  130. % 1 Binary (ubit1, bitpix=1) % DT_BINARY
  131. % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8
  132. % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16
  133. % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32
  134. % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32
  135. % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64
  136. % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64
  137. % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24
  138. % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8
  139. % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96
  140. % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16
  141. % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32
  142. % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64
  143. % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64
  144. % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128
  145. % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
  146. % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
  147. %
  148. switch hdr.dime.datatype
  149. case 1,
  150. hdr.dime.bitpix = 1; precision = 'ubit1';
  151. case 2,
  152. hdr.dime.bitpix = 8; precision = 'uint8';
  153. case 4,
  154. hdr.dime.bitpix = 16; precision = 'int16';
  155. case 8,
  156. hdr.dime.bitpix = 32; precision = 'int32';
  157. case 16,
  158. hdr.dime.bitpix = 32; precision = 'float32';
  159. case 32,
  160. hdr.dime.bitpix = 64; precision = 'float32';
  161. case 64,
  162. hdr.dime.bitpix = 64; precision = 'float64';
  163. case 128,
  164. hdr.dime.bitpix = 24; precision = 'uint8';
  165. case 256
  166. hdr.dime.bitpix = 8; precision = 'int8';
  167. case 511
  168. hdr.dime.bitpix = 96; precision = 'float32';
  169. case 512
  170. hdr.dime.bitpix = 16; precision = 'uint16';
  171. case 768
  172. hdr.dime.bitpix = 32; precision = 'uint32';
  173. case 1024
  174. hdr.dime.bitpix = 64; precision = 'int64';
  175. case 1280
  176. hdr.dime.bitpix = 64; precision = 'uint64';
  177. case 1792,
  178. hdr.dime.bitpix = 128; precision = 'float64';
  179. otherwise
  180. error('This datatype is not supported');
  181. end
  182. tmp = hdr.dime.dim(2:end);
  183. tmp(find(tmp < 1)) = 1;
  184. hdr.dime.dim(2:end) = tmp;
  185. % move pointer to the start of image block
  186. %
  187. switch filetype
  188. case {0, 1}
  189. fseek(fid, 0, 'bof');
  190. case 2
  191. fseek(fid, hdr.dime.vox_offset, 'bof');
  192. end
  193. % Load whole image block for old Analyze format or binary image;
  194. % otherwise, load images that are specified in img_idx, dim5_idx,
  195. % dim6_idx, and dim7_idx
  196. %
  197. % For binary image, we have to read all because pos can not be
  198. % seeked in bit and can not be calculated the way below.
  199. %
  200. if hdr.dime.datatype == 1 | isequal(hdr.dime.dim(4:8),ones(1,5)) | ...
  201. (isempty(img_idx) & isempty(dim5_idx) & isempty(dim6_idx) & isempty(dim7_idx) & isempty(slice_idx))
  202. % For each frame, precision of value will be read
  203. % in img_siz times, where img_siz is only the
  204. % dimension size of an image, not the byte storage
  205. % size of an image.
  206. %
  207. img_siz = prod(hdr.dime.dim(2:8));
  208. % For complex float32 or complex float64, voxel values
  209. % include [real, imag]
  210. %
  211. if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
  212. img_siz = img_siz * 2;
  213. end
  214. %MPH: For RGB24, voxel values include 3 separate color planes
  215. %
  216. if hdr.dime.datatype == 128 | hdr.dime.datatype == 511
  217. img_siz = img_siz * 3;
  218. end
  219. img = fread(fid, img_siz, sprintf('*%s',precision));
  220. d1 = hdr.dime.dim(2);
  221. d2 = hdr.dime.dim(3);
  222. d3 = hdr.dime.dim(4);
  223. d4 = hdr.dime.dim(5);
  224. d5 = hdr.dime.dim(6);
  225. d6 = hdr.dime.dim(7);
  226. d7 = hdr.dime.dim(8);
  227. if isempty(slice_idx)
  228. slice_idx = 1:d3;
  229. end
  230. if isempty(img_idx)
  231. img_idx = 1:d4;
  232. end
  233. if isempty(dim5_idx)
  234. dim5_idx = 1:d5;
  235. end
  236. if isempty(dim6_idx)
  237. dim6_idx = 1:d6;
  238. end
  239. if isempty(dim7_idx)
  240. dim7_idx = 1:d7;
  241. end
  242. else
  243. d1 = hdr.dime.dim(2);
  244. d2 = hdr.dime.dim(3);
  245. d3 = hdr.dime.dim(4);
  246. d4 = hdr.dime.dim(5);
  247. d5 = hdr.dime.dim(6);
  248. d6 = hdr.dime.dim(7);
  249. d7 = hdr.dime.dim(8);
  250. if isempty(slice_idx)
  251. slice_idx = 1:d3;
  252. end
  253. if isempty(img_idx)
  254. img_idx = 1:d4;
  255. end
  256. if isempty(dim5_idx)
  257. dim5_idx = 1:d5;
  258. end
  259. if isempty(dim6_idx)
  260. dim6_idx = 1:d6;
  261. end
  262. if isempty(dim7_idx)
  263. dim7_idx = 1:d7;
  264. end
  265. %ROMAN: begin
  266. roman = 1;
  267. if(roman)
  268. % compute size of one slice
  269. %
  270. img_siz = prod(hdr.dime.dim(2:3));
  271. % For complex float32 or complex float64, voxel values
  272. % include [real, imag]
  273. %
  274. if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
  275. img_siz = img_siz * 2;
  276. end
  277. %MPH: For RGB24, voxel values include 3 separate color planes
  278. %
  279. if hdr.dime.datatype == 128 | hdr.dime.datatype == 511
  280. img_siz = img_siz * 3;
  281. end
  282. % preallocate img
  283. img = zeros(img_siz, length(slice_idx)*length(img_idx)*length(dim5_idx)*length(dim6_idx)*length(dim7_idx) );
  284. currentIndex = 1;
  285. else
  286. img = [];
  287. end; %if(roman)
  288. % ROMAN: end
  289. for i7=1:length(dim7_idx)
  290. for i6=1:length(dim6_idx)
  291. for i5=1:length(dim5_idx)
  292. for t=1:length(img_idx)
  293. for s=1:length(slice_idx)
  294. % Position is seeked in bytes. To convert dimension size
  295. % to byte storage size, hdr.dime.bitpix/8 will be
  296. % applied.
  297. %
  298. pos = sub2ind([d1 d2 d3 d4 d5 d6 d7], 1, 1, slice_idx(s), ...
  299. img_idx(t), dim5_idx(i5),dim6_idx(i6),dim7_idx(i7)) -1;
  300. pos = pos * hdr.dime.bitpix/8;
  301. % ROMAN: begin
  302. if(roman)
  303. % do nothing
  304. else
  305. img_siz = prod(hdr.dime.dim(2:3));
  306. % For complex float32 or complex float64, voxel values
  307. % include [real, imag]
  308. %
  309. if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
  310. img_siz = img_siz * 2;
  311. end
  312. %MPH: For RGB24, voxel values include 3 separate color planes
  313. %
  314. if hdr.dime.datatype == 128 | hdr.dime.datatype == 511
  315. img_siz = img_siz * 3;
  316. end
  317. end; % if (roman)
  318. % ROMAN: end
  319. if filetype == 2
  320. fseek(fid, pos + hdr.dime.vox_offset, 'bof');
  321. else
  322. fseek(fid, pos, 'bof');
  323. end
  324. % For each frame, fread will read precision of value
  325. % in img_siz times
  326. %
  327. % ROMAN: begin
  328. if(roman)
  329. img(:,currentIndex) = fread(fid, img_siz, sprintf('*%s',precision));
  330. currentIndex = currentIndex +1;
  331. else
  332. img = [img fread(fid, img_siz, sprintf('*%s',precision))];
  333. end; %if(roman)
  334. % ROMAN: end
  335. end
  336. end
  337. end
  338. end
  339. end
  340. end
  341. % For complex float32 or complex float64, voxel values
  342. % include [real, imag]
  343. %
  344. if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
  345. img = reshape(img, [2, length(img)/2]);
  346. img = complex(img(1,:)', img(2,:)');
  347. end
  348. fclose(fid);
  349. % Update the global min and max values
  350. %
  351. hdr.dime.glmax = double(max(img(:)));
  352. hdr.dime.glmin = double(min(img(:)));
  353. % old_RGB treat RGB slice by slice, now it is treated voxel by voxel
  354. %
  355. if old_RGB & hdr.dime.datatype == 128 & hdr.dime.bitpix == 24
  356. % remove squeeze
  357. img = (reshape(img, [hdr.dime.dim(2:3) 3 length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)]));
  358. img = permute(img, [1 2 4 3 5 6 7 8]);
  359. elseif hdr.dime.datatype == 128 & hdr.dime.bitpix == 24
  360. % remove squeeze
  361. img = (reshape(img, [3 hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)]));
  362. img = permute(img, [2 3 4 1 5 6 7 8]);
  363. elseif hdr.dime.datatype == 511 & hdr.dime.bitpix == 96
  364. img = double(img(:));
  365. img = single((img - min(img))/(max(img) - min(img)));
  366. % remove squeeze
  367. img = (reshape(img, [3 hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)]));
  368. img = permute(img, [2 3 4 1 5 6 7 8]);
  369. else
  370. % remove squeeze
  371. img = (reshape(img, [hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)]));
  372. end
  373. if ~isempty(slice_idx)
  374. hdr.dime.dim(4) = length(slice_idx);
  375. end
  376. if ~isempty(img_idx)
  377. hdr.dime.dim(5) = length(img_idx);
  378. end
  379. if ~isempty(dim5_idx)
  380. hdr.dime.dim(6) = length(dim5_idx);
  381. end
  382. if ~isempty(dim6_idx)
  383. hdr.dime.dim(7) = length(dim6_idx);
  384. end
  385. if ~isempty(dim7_idx)
  386. hdr.dime.dim(8) = length(dim7_idx);
  387. end
  388. return % read_image