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