Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,240 @@
1
+%  Make nii 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
+%  However, NIfTI allows a maximum of 7D matrix. For RGB24 datatype, an
4
+%  extra dimension for RGB should be inserted immediately after [x y z].
5
+%  Optional parameters can also be included, such as: voxel_size, 
6
+%  origin, datatype, and description.
7
+%  
8
+%  Usage: nii = make_nii(img, [voxel_size], [origin], [datatype], ...
9
+%		[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. For RGB
16
+%			datatype, an extra dimension for RGB should
17
+%			be inserted immediately after [x y z].
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
+%		32 - complex64,  64 - float64,  128 - RGB24,
27
+%		256 - int8,  512 - uint16,  768 - uint32, 
28
+%		1792 - complex128
29
+%			Default will use the data type of 'img' matrix
30
+%
31
+%	description (optional):	Description of data. Default is ''.
32
+%
33
+%  e.g.:
34
+%     origin = [33 44 13]; datatype = 64;
35
+%     nii = make_nii(img, [], origin, datatype);    % default voxel_size
36
+%
37
+%  NIFTI data format can be found on: http://nifti.nimh.nih.gov
38
+%
39
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
40
+%
41
+function nii = make_nii(varargin)
42
+
43
+   nii.img = varargin{1};
44
+   dims = size(nii.img);
45
+   dims = [length(dims) dims ones(1,8)];
46
+   dims = dims(1:8);
47
+
48
+   voxel_size = [0 ones(1,7)];
49
+   origin = zeros(1,5);
50
+   descrip = '';
51
+
52
+   switch class(nii.img)
53
+      case 'uint8'
54
+         datatype = 2;
55
+      case 'int16'
56
+         datatype = 4;
57
+      case 'int32'
58
+         datatype = 8;
59
+      case 'single'
60
+         datatype = 16;
61
+      case 'double'
62
+         datatype = 64;
63
+      case 'int8'
64
+         datatype = 256;
65
+      case 'uint16'
66
+         datatype = 512;
67
+      case 'uint32'
68
+         datatype = 768;
69
+      otherwise
70
+         error('Datatype is not supported by make_nii.');
71
+   end
72
+
73
+   if nargin > 1 & ~isempty(varargin{2})
74
+      voxel_size(2:4) = double(varargin{2});
75
+   end
76
+
77
+   if nargin > 2 & ~isempty(varargin{3})
78
+      origin(1:3) = double(varargin{3});
79
+   end
80
+
81
+   if nargin > 3 & ~isempty(varargin{4})
82
+      datatype = double(varargin{4});
83
+   end
84
+
85
+   if nargin > 4 & ~isempty(varargin{5})
86
+      descrip = varargin{5};
87
+   end
88
+
89
+   if datatype == 128
90
+      if ndims(nii.img) > 8
91
+         error('NIfTI only allows a maximum of 7 Dimension matrix.');
92
+      end
93
+
94
+     dims(1) = dims(1)-1;
95
+     dims(5:8) = [dims(6:8) 1];
96
+
97
+   else
98
+      if ndims(nii.img) > 7
99
+         error('NIfTI only allows a maximum of 7 Dimension matrix.');
100
+      end
101
+   end
102
+
103
+   maxval = round(double(max(nii.img(:))));
104
+   minval = round(double(min(nii.img(:))));
105
+
106
+   nii.hdr = make_header(dims, voxel_size, origin, datatype, ...
107
+	descrip, maxval, minval);
108
+
109
+   switch nii.hdr.dime.datatype
110
+   case 2
111
+      nii.img = uint8(nii.img);
112
+   case 4
113
+      nii.img = int16(nii.img);
114
+   case 8
115
+      nii.img = int32(nii.img);
116
+   case 16
117
+      nii.img = single(nii.img);
118
+   case 32
119
+      nii.img = single(nii.img);
120
+   case 64
121
+      nii.img = double(nii.img);
122
+   case 128
123
+      nii.img = uint8(nii.img);
124
+   case 256
125
+      nii.img = int8(nii.img);
126
+   case 512
127
+      nii.img = uint16(nii.img);
128
+   case 768
129
+      nii.img = uint32(nii.img);
130
+   case 1792
131
+      nii.img = double(nii.img);
132
+   otherwise
133
+      error('Datatype is not supported by make_nii.');
134
+   end
135
+
136
+   return;					% make_nii
137
+
138
+
139
+%---------------------------------------------------------------------
140
+function hdr = make_header(dims, voxel_size, origin, datatype, ...
141
+	descrip, maxval, minval)
142
+
143
+   hdr.hk   = header_key;
144
+   hdr.dime = image_dimension(dims, voxel_size, datatype, maxval, minval);
145
+   hdr.hist = data_history(origin, descrip);
146
+    
147
+   return;					% make_header
148
+
149
+
150
+%---------------------------------------------------------------------
151
+function hk = header_key
152
+
153
+    hk.sizeof_hdr       = 348;			% must be 348!
154
+    hk.data_type        = '';
155
+    hk.db_name          = '';
156
+    hk.extents          = 0;
157
+    hk.session_error    = 0;
158
+    hk.regular          = 'r';
159
+    hk.dim_info         = 0;
160
+    
161
+    return;					% header_key
162
+
163
+
164
+%---------------------------------------------------------------------
165
+function dime = image_dimension(dims, voxel_size, datatype, maxval, minval)
166
+   
167
+   dime.dim = dims;
168
+   dime.intent_p1 = 0;
169
+   dime.intent_p2 = 0;
170
+   dime.intent_p3 = 0;
171
+   dime.intent_code = 0;
172
+   dime.datatype = datatype;
173
+   
174
+   switch dime.datatype
175
+   case   2,
176
+      dime.bitpix = 8;  precision = 'uint8';
177
+   case   4,
178
+      dime.bitpix = 16; precision = 'int16';
179
+   case   8,
180
+      dime.bitpix = 32; precision = 'int32';
181
+   case  16,
182
+      dime.bitpix = 32; precision = 'float32';
183
+   case  32,
184
+      dime.bitpix = 64; precision = 'float32';
185
+   case  64,
186
+      dime.bitpix = 64; precision = 'float64';
187
+   case  128,
188
+      dime.bitpix = 24; precision = 'uint8';
189
+   case 256 
190
+      dime.bitpix = 8;  precision = 'int8';
191
+   case 512 
192
+      dime.bitpix = 16; precision = 'uint16';
193
+   case 768 
194
+      dime.bitpix = 32; precision = 'uint32';
195
+   case  1792,
196
+      dime.bitpix = 128; precision = 'float64';
197
+   otherwise
198
+      error('Datatype is not supported by make_nii.');
199
+   end
200
+   
201
+   dime.slice_start = 0;
202
+   dime.pixdim = voxel_size;
203
+   dime.vox_offset = 0;
204
+   dime.scl_slope = 0;
205
+   dime.scl_inter = 0;
206
+   dime.slice_end = 0;
207
+   dime.slice_code = 0;
208
+   dime.xyzt_units = 0;
209
+   dime.cal_max = 0;
210
+   dime.cal_min = 0;
211
+   dime.slice_duration = 0;
212
+   dime.toffset = 0;
213
+   dime.glmax = maxval;
214
+   dime.glmin = minval;
215
+   
216
+   return;					% image_dimension
217
+
218
+
219
+%---------------------------------------------------------------------
220
+function hist = data_history(origin, descrip)
221
+   
222
+   hist.descrip = descrip;
223
+   hist.aux_file = 'none';
224
+   hist.qform_code = 0;
225
+   hist.sform_code = 0;
226
+   hist.quatern_b = 0;
227
+   hist.quatern_c = 0;
228
+   hist.quatern_d = 0;
229
+   hist.qoffset_x = 0;
230
+   hist.qoffset_y = 0;
231
+   hist.qoffset_z = 0;
232
+   hist.srow_x = zeros(1,4);
233
+   hist.srow_y = zeros(1,4);
234
+   hist.srow_z = zeros(1,4);
235
+   hist.intent_name = '';
236
+   hist.magic = '';
237
+   hist.originator = origin;
238
+   
239
+   return;					% data_history
240
+