Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

make_ana.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. % Make ANALYZE 7.5 data structure specified by a 3D or 4D matrix.
  2. % Optional parameters can also be included, such as: voxel_size,
  3. % origin, datatype, and description.
  4. %
  5. % Once the ANALYZE structure is made, it can be saved into ANALYZE 7.5
  6. % format data file using "save_untouch_nii" command (for more detail,
  7. % type: help save_untouch_nii).
  8. %
  9. % Usage: ana = make_ana(img, [voxel_size], [origin], [datatype], [description])
  10. %
  11. % Where:
  12. %
  13. % img: a 3D matrix [x y z], or a 4D matrix with time
  14. % series [x y z t]. When image is in RGB format,
  15. % make sure that the size of 4th dimension is
  16. % always 3 (i.e. [R G B]). In that case, make
  17. % sure that you must specify RGB datatype to 128.
  18. %
  19. % voxel_size (optional): Voxel size in millimeter for each
  20. % dimension. Default is [1 1 1].
  21. %
  22. % origin (optional): The AC origin. Default is [0 0 0].
  23. %
  24. % datatype (optional): Storage data type:
  25. % 2 - uint8, 4 - int16, 8 - int32, 16 - float32,
  26. % 64 - float64, 128 - RGB24
  27. % Default will use the data type of 'img' matrix
  28. % For RGB image, you must specify it to 128.
  29. %
  30. % description (optional): Description of data. Default is ''.
  31. %
  32. % e.g.:
  33. % origin = [33 44 13]; datatype = 64;
  34. % ana = make_ana(img, [], origin, datatype); % default voxel_size
  35. %
  36. % ANALYZE 7.5 format: http://www.rotman-baycrest.on.ca/~jimmy/ANALYZE75.pdf
  37. %
  38. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  39. %
  40. function ana = make_ana(varargin)
  41. ana.img = varargin{1};
  42. dims = size(ana.img);
  43. dims = [4 dims ones(1,8)];
  44. dims = dims(1:8);
  45. voxel_size = [0 ones(1,3) zeros(1,4)];
  46. origin = zeros(1,5);
  47. descrip = '';
  48. switch class(ana.img)
  49. case 'uint8'
  50. datatype = 2;
  51. case 'int16'
  52. datatype = 4;
  53. case 'int32'
  54. datatype = 8;
  55. case 'single'
  56. datatype = 16;
  57. case 'double'
  58. datatype = 64;
  59. otherwise
  60. error('Datatype is not supported by make_ana.');
  61. end
  62. if nargin > 1 & ~isempty(varargin{2})
  63. voxel_size(2:4) = double(varargin{2});
  64. end
  65. if nargin > 2 & ~isempty(varargin{3})
  66. origin(1:3) = double(varargin{3});
  67. end
  68. if nargin > 3 & ~isempty(varargin{4})
  69. datatype = double(varargin{4});
  70. if datatype == 128 | datatype == 511
  71. dims(5) = [];
  72. dims = [dims 1];
  73. end
  74. end
  75. if nargin > 4 & ~isempty(varargin{5})
  76. descrip = varargin{5};
  77. end
  78. if ndims(ana.img) > 4
  79. error('NIfTI only allows a maximum of 4 Dimension matrix.');
  80. end
  81. maxval = round(double(max(ana.img(:))));
  82. minval = round(double(min(ana.img(:))));
  83. ana.hdr = make_header(dims, voxel_size, origin, datatype, ...
  84. descrip, maxval, minval);
  85. ana.filetype = 0;
  86. ana.ext = [];
  87. ana.untouch = 1;
  88. switch ana.hdr.dime.datatype
  89. case 2
  90. ana.img = uint8(ana.img);
  91. case 4
  92. ana.img = int16(ana.img);
  93. case 8
  94. ana.img = int32(ana.img);
  95. case 16
  96. ana.img = single(ana.img);
  97. case 64
  98. ana.img = double(ana.img);
  99. case 128
  100. ana.img = uint8(ana.img);
  101. otherwise
  102. error('Datatype is not supported by make_ana.');
  103. end
  104. return; % make_ana
  105. %---------------------------------------------------------------------
  106. function hdr = make_header(dims, voxel_size, origin, datatype, ...
  107. descrip, maxval, minval)
  108. hdr.hk = header_key;
  109. hdr.dime = image_dimension(dims, voxel_size, datatype, maxval, minval);
  110. hdr.hist = data_history(origin, descrip);
  111. return; % make_header
  112. %---------------------------------------------------------------------
  113. function hk = header_key
  114. hk.sizeof_hdr = 348; % must be 348!
  115. hk.data_type = '';
  116. hk.db_name = '';
  117. hk.extents = 0;
  118. hk.session_error = 0;
  119. hk.regular = 'r';
  120. hk.hkey_un0 = '0';
  121. return; % header_key
  122. %---------------------------------------------------------------------
  123. function dime = image_dimension(dims, voxel_size, datatype, maxval, minval)
  124. dime.dim = dims;
  125. dime.vox_units = 'mm';
  126. dime.cal_units = '';
  127. dime.unused1 = 0;
  128. dime.datatype = datatype;
  129. switch dime.datatype
  130. case 2,
  131. dime.bitpix = 8; precision = 'uint8';
  132. case 4,
  133. dime.bitpix = 16; precision = 'int16';
  134. case 8,
  135. dime.bitpix = 32; precision = 'int32';
  136. case 16,
  137. dime.bitpix = 32; precision = 'float32';
  138. case 64,
  139. dime.bitpix = 64; precision = 'float64';
  140. case 128
  141. dime.bitpix = 24; precision = 'uint8';
  142. otherwise
  143. error('Datatype is not supported by make_ana.');
  144. end
  145. dime.dim_un0 = 0;
  146. dime.pixdim = voxel_size;
  147. dime.vox_offset = 0;
  148. dime.roi_scale = 1;
  149. dime.funused1 = 0;
  150. dime.funused2 = 0;
  151. dime.cal_max = 0;
  152. dime.cal_min = 0;
  153. dime.compressed = 0;
  154. dime.verified = 0;
  155. dime.glmax = maxval;
  156. dime.glmin = minval;
  157. return; % image_dimension
  158. %---------------------------------------------------------------------
  159. function hist = data_history(origin, descrip)
  160. hist.descrip = descrip;
  161. hist.aux_file = 'none';
  162. hist.orient = 0;
  163. hist.originator = origin;
  164. hist.generated = '';
  165. hist.scannum = '';
  166. hist.patient_id = '';
  167. hist.exp_date = '';
  168. hist.exp_time = '';
  169. hist.hist_un0 = '';
  170. hist.views = 0;
  171. hist.vols_added = 0;
  172. hist.start_field = 0;
  173. hist.field_skip = 0;
  174. hist.omax = 0;
  175. hist.omin = 0;
  176. hist.smax = 0;
  177. hist.smin = 0;
  178. return; % data_history