make_nii.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. % Make NIfTI structure specified by an N-D matrix. Usually, N is 3 for
  2. % 3D matrix [x y z], or 4 for 4D matrix with time series [x y z t].
  3. % Optional parameters can also be included, such as: voxel_size,
  4. % origin, datatype, and description.
  5. %
  6. % Once the NIfTI structure is made, it can be saved into NIfTI file
  7. % using "save_nii" command (for more detail, type: help save_nii).
  8. %
  9. % Usage: nii = make_nii(img, [voxel_size], [origin], [datatype], [description])
  10. %
  11. % Where:
  12. %
  13. % img: Usually, img is a 3D matrix [x y z], or a 4D
  14. % matrix with time series [x y z t]. However,
  15. % NIfTI allows a maximum of 7D matrix. When the
  16. % image is in RGB format, make sure that the size
  17. % of 4th dimension is always 3 (i.e. [R G B]). In
  18. % that case, make sure that you must specify RGB
  19. % datatype, which is either 128 or 511.
  20. %
  21. % voxel_size (optional): Voxel size in millimeter for each
  22. % dimension. Default is [1 1 1].
  23. %
  24. % origin (optional): The AC origin. Default is [0 0 0].
  25. %
  26. % datatype (optional): Storage data type:
  27. % 2 - uint8, 4 - int16, 8 - int32, 16 - float32,
  28. % 32 - complex64, 64 - float64, 128 - RGB24,
  29. % 256 - int8, 511 - RGB96, 512 - uint16,
  30. % 768 - uint32, 1792 - complex128
  31. % Default will use the data type of 'img' matrix
  32. % For RGB image, you must specify it to either 128
  33. % or 511.
  34. %
  35. % description (optional): Description of data. Default is ''.
  36. %
  37. % e.g.:
  38. % origin = [33 44 13]; datatype = 64;
  39. % nii = make_nii(img, [], origin, datatype); % default voxel_size
  40. %
  41. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  42. %
  43. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  44. %
  45. function nii = make_nii(varargin)
  46. nii.img = varargin{1};
  47. dims = size(nii.img);
  48. dims = [length(dims) dims ones(1,8)];
  49. dims = dims(1:8);
  50. voxel_size = [0 ones(1,7)];
  51. origin = zeros(1,5);
  52. descrip = '';
  53. switch class(nii.img)
  54. case 'uint8'
  55. datatype = 2;
  56. case 'int16'
  57. datatype = 4;
  58. case 'int32'
  59. datatype = 8;
  60. case 'single'
  61. if isreal(nii.img)
  62. datatype = 16;
  63. else
  64. datatype = 32;
  65. end
  66. case 'double'
  67. if isreal(nii.img)
  68. datatype = 64;
  69. else
  70. datatype = 1792;
  71. end
  72. case 'int8'
  73. datatype = 256;
  74. case 'uint16'
  75. datatype = 512;
  76. case 'uint32'
  77. datatype = 768;
  78. otherwise
  79. error('Datatype is not supported by make_nii.');
  80. end
  81. if nargin > 1 & ~isempty(varargin{2})
  82. voxel_size(2:4) = double(varargin{2});
  83. end
  84. if nargin > 2 & ~isempty(varargin{3})
  85. origin(1:3) = double(varargin{3});
  86. end
  87. if nargin > 3 & ~isempty(varargin{4})
  88. datatype = double(varargin{4});
  89. if datatype == 128 | datatype == 511
  90. dims(5) = [];
  91. dims(1) = dims(1) - 1;
  92. dims = [dims 1];
  93. end
  94. end
  95. if nargin > 4 & ~isempty(varargin{5})
  96. descrip = varargin{5};
  97. end
  98. if ndims(nii.img) > 7
  99. error('NIfTI only allows a maximum of 7 Dimension matrix.');
  100. end
  101. maxval = round(double(max(nii.img(:))));
  102. minval = round(double(min(nii.img(:))));
  103. nii.hdr = make_header(dims, voxel_size, origin, datatype, ...
  104. descrip, maxval, minval);
  105. switch nii.hdr.dime.datatype
  106. case 2
  107. nii.img = uint8(nii.img);
  108. case 4
  109. nii.img = int16(nii.img);
  110. case 8
  111. nii.img = int32(nii.img);
  112. case 16
  113. nii.img = single(nii.img);
  114. case 32
  115. nii.img = single(nii.img);
  116. case 64
  117. nii.img = double(nii.img);
  118. case 128
  119. nii.img = uint8(nii.img);
  120. case 256
  121. nii.img = int8(nii.img);
  122. case 511
  123. img = double(nii.img(:));
  124. img = single((img - min(img))/(max(img) - min(img)));
  125. nii.img = reshape(img, size(nii.img));
  126. nii.hdr.dime.glmax = double(max(img));
  127. nii.hdr.dime.glmin = double(min(img));
  128. case 512
  129. nii.img = uint16(nii.img);
  130. case 768
  131. nii.img = uint32(nii.img);
  132. case 1792
  133. nii.img = double(nii.img);
  134. otherwise
  135. error('Datatype is not supported by make_nii.');
  136. end
  137. return; % make_nii
  138. %---------------------------------------------------------------------
  139. function hdr = make_header(dims, voxel_size, origin, datatype, ...
  140. descrip, maxval, minval)
  141. hdr.hk = header_key;
  142. hdr.dime = image_dimension(dims, voxel_size, datatype, maxval, minval);
  143. hdr.hist = data_history(origin, descrip);
  144. return; % make_header
  145. %---------------------------------------------------------------------
  146. function hk = header_key
  147. hk.sizeof_hdr = 348; % must be 348!
  148. hk.data_type = '';
  149. hk.db_name = '';
  150. hk.extents = 0;
  151. hk.session_error = 0;
  152. hk.regular = 'r';
  153. hk.dim_info = 0;
  154. return; % header_key
  155. %---------------------------------------------------------------------
  156. function dime = image_dimension(dims, voxel_size, datatype, maxval, minval)
  157. dime.dim = dims;
  158. dime.intent_p1 = 0;
  159. dime.intent_p2 = 0;
  160. dime.intent_p3 = 0;
  161. dime.intent_code = 0;
  162. dime.datatype = datatype;
  163. switch dime.datatype
  164. case 2,
  165. dime.bitpix = 8; precision = 'uint8';
  166. case 4,
  167. dime.bitpix = 16; precision = 'int16';
  168. case 8,
  169. dime.bitpix = 32; precision = 'int32';
  170. case 16,
  171. dime.bitpix = 32; precision = 'float32';
  172. case 32,
  173. dime.bitpix = 64; precision = 'float32';
  174. case 64,
  175. dime.bitpix = 64; precision = 'float64';
  176. case 128
  177. dime.bitpix = 24; precision = 'uint8';
  178. case 256
  179. dime.bitpix = 8; precision = 'int8';
  180. case 511
  181. dime.bitpix = 96; precision = 'float32';
  182. case 512
  183. dime.bitpix = 16; precision = 'uint16';
  184. case 768
  185. dime.bitpix = 32; precision = 'uint32';
  186. case 1792,
  187. dime.bitpix = 128; precision = 'float64';
  188. otherwise
  189. error('Datatype is not supported by make_nii.');
  190. end
  191. dime.slice_start = 0;
  192. dime.pixdim = voxel_size;
  193. dime.vox_offset = 0;
  194. dime.scl_slope = 0;
  195. dime.scl_inter = 0;
  196. dime.slice_end = 0;
  197. dime.slice_code = 0;
  198. dime.xyzt_units = 0;
  199. dime.cal_max = 0;
  200. dime.cal_min = 0;
  201. dime.slice_duration = 0;
  202. dime.toffset = 0;
  203. dime.glmax = maxval;
  204. dime.glmin = minval;
  205. return; % image_dimension
  206. %---------------------------------------------------------------------
  207. function hist = data_history(origin, descrip)
  208. hist.descrip = descrip;
  209. hist.aux_file = 'none';
  210. hist.qform_code = 0;
  211. hist.sform_code = 0;
  212. hist.quatern_b = 0;
  213. hist.quatern_c = 0;
  214. hist.quatern_d = 0;
  215. hist.qoffset_x = 0;
  216. hist.qoffset_y = 0;
  217. hist.qoffset_z = 0;
  218. hist.srow_x = zeros(1,4);
  219. hist.srow_y = zeros(1,4);
  220. hist.srow_z = zeros(1,4);
  221. hist.intent_name = '';
  222. hist.magic = '';
  223. hist.originator = origin;
  224. return; % data_history