-
Notifications
You must be signed in to change notification settings - Fork 3
/
RunPreproc.m
326 lines (278 loc) · 9.36 KB
/
RunPreproc.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function out = RunPreproc(paths,opt)
% Some basic preprocessing of hospital neuroimaging data.
%
% INPUT
% paths - Can be given as:
% paths = im.ext,
% paths = {im1.ext, ..., imN.ext}
% paths = {{im1.ext, ..., imN.ext},
% {'', ..., labn.ext, ..., ''}}
%
% where valid extensions are .nii and .nii.gz, and labn.ext should
% be in the same index position as the image it was labelled on.
% opt - Preprocessing options
%
% OUTPUT
% out.pth.im - Cell array of paths to preprocessed image(s)
% out.pth.im2d - Cell array of paths to 2D versions of preprocessed
% image(s) (if opt.do.write2d = true)
% out.pth.lab - Cell array of path to label image (if labels given)
% out.pth.lab2d - Cell array of path to 2D version of label image (if
% labels given and if opt.do.write2d = tru)
% out.mat - Orientation matrices to go back to native space
% orientation as:
% Mc = spm_get_space(P{c});
% spm_get_space(f,M{c}*Mc);
%_______________________________________________________________________
% Copyright (C) 2019 Wellcome Trust Centre for Neuroimaging
% Set options
if nargin < 2, opt = struct; end
opt = get_default_opt(opt);
% Check MATLAB path
if isempty(fileparts(which('spm')))
error('SPM12 not on the MATLAB path!'); % download from https://www.fil.ion.ucl.ac.uk/spm/software/download/
end
if opt.do.denoise || opt.do.superres
if isempty(fileparts(which('spm_superres')))
error('spm_superres not on the MATLAB path!'); % download/clone from https://github.com/brudfors/spm_superres
end
end
if ~isempty(opt.dir_out) && ~(exist(opt.dir_out,'dir') == 7)
% Create output directory
mkdir(opt.dir_out);
end
% Make sure output directory is encoded by its full path
s = what(opt.dir_out);
opt.dir_out = s.path;
% Copy (so to not overwrite originals)
[Nii,was_gz,nams] = read_and_copy(paths,opt.dir_out,opt.prefix);
C = numel(Nii{1});
if numel(Nii) > 1
% Collapse labels
Nii = collapse_labels(Nii,opt.labels.part);
end
if opt.do.erode
% Remove a few of the outer voxels
Nii = erode_im(Nii);
end
M = cell(1,C);
M(:) = {eye(4)};
if opt.do.res_orig
% Reset origin (important for CT)
vx = [];
if ~opt.do.superres && opt.do.vx
vx = opt.vx.size;
end
[Nii,M] = reset_origin(Nii,opt.reset_orig,vx);
end
if opt.do.real_mni
% Realing to MNI space
[Nii,M] = realign2mni(Nii,M,opt.realign2mni);
end
if opt.do.nm_reorient
% Reslice so that image data is in world space
[Nii,M] = nm_reorient_ims(Nii); % M is set to identity
end
if opt.do.crop
% Remove uneccesary data
Nii = crop(Nii,opt.do.real_mni,opt.crop);
end
if opt.do.coreg
% Coreg
Nii = coreg(Nii,opt.coreg);
end
if opt.do.denoise && ~opt.do.superres
% Denoise
Nii = denoise(Nii);
% Coreg (one more time after denoising)
Nii = coreg(Nii,opt.coreg);
end
% The below steps are for creating images of equal size, either by MTV
% super-resolution, or by just simply reslicing
if opt.do.superres
% Super-resolve
Nii = superres(Nii,opt.do.coreg,opt.superres);
% Coreg (one more time after super-resolving)
Nii = coreg(Nii,opt.coreg);
else
if opt.do.reslice
% Make images same dimensions
[Nii,M] = reslice_images(Nii,M,opt.reslice);
end
if opt.do.vx && ~opt.do.res_orig && ~opt.do.bb_spm
% Set same voxel size
Nii = resample_images(Nii,opt.vx);
end
end
if opt.do.bb_spm
% Crop to SPM12 BB
if opt.do.vx
vx = opt.vx.size;
else
vx = [];
end
Nii = apply_bb(Nii, vx, opt.bb.dim);
end
if numel(Nii) > 1 && isempty(opt.pth_template)
% Reslice labels
Nii = reslice_labels(Nii,opt.reslice);
end
pth_seg = {};
if opt.do.segment
% Run SPM12 segmentation
pth_seg = segment_preproc8(Nii,opt.segment);
end
if opt.do.bfcorr
% Bias field correct (depends on segment_preproc8())
Nii = bf_correct(Nii,pth_seg);
end
if opt.do.skullstrip
% Skull-strip (depends on segment_preproc8())
Nii = skull_strip(Nii,pth_seg);
if ~opt.do.segment0
for k=1:numel(pth_seg{1}), delete(pth_seg{1}{k}); end
end
end
if ~isempty(opt.pth_template) && isfile(opt.pth_template)
% Reslice and affinely register images to a template
[Nii,M] = reslice2template(Nii,M,opt.pth_template);
if numel(Nii) > 1
% Reslice labels
Nii = reslice_labels(Nii,opt.reslice);
end
end
pth_norm = {};
if opt.do.normalise
% Create normalised versions of Nii
pth_norm = write_normalised(Nii,opt.normalise,pth_seg);
end
if opt.do.int_norm
% Normalise image intensities in the range opt.int_norm.rng
Nii = intensity_normalise(Nii,opt.int_norm);
end
P2d = {};
if opt.do.write2d
% Write 2D versions
P2d = write_2d(Nii,pth_seg,opt.dir_out2d,opt.write2d);
end
% Allocate output
C = numel(Nii{1});
out = struct;
out.pth.im = cell(1,C);
out.pth.im2d = cell(1,C);
out.pth.lab = cell(1,C);
out.pth.lab2d = cell(1,C);
out.pth.seg = {};
out.pth.norm = {};
out.mat = cell(1,C);
for i=1:2
for c=1:C
if (i == 2 && numel(Nii) == 1) || (c > numel(Nii{i}) || isempty(Nii{i}(c).dat)), continue; end
f = Nii{i}(c).dat.fname;
if i == 1
out.mat{c} = M{c};
end
if i == 1
out.pth.im{c} = Nii{i}(c).dat.fname;
[pth,~,ext] = fileparts(out.pth.im{c});
nfname = fullfile(pth, [nams{1, c} ext]);
if ~strcmp(out.pth.im{c}, nfname)
movefile(out.pth.im{c}, nfname);
end
out.pth.im{c} = nfname;
if opt.do.go2native
p = out.pth.im{c};
Mc = spm_get_space(p);
spm_get_space(p,M{c}*Mc);
end
if was_gz == true
% Compress back to .gz
p = gzip(out.pth.im{c});
delete(out.pth.im{c});
out.pth.im{c} = p{1};
end
else
out.pth.lab{c} = Nii{i}(c).dat.fname;
[pth,~,ext] = fileparts(out.pth.lab{c});
nfname = fullfile(pth, [nams{2, c} ext]);
if ~strcmp(out.pth.lab{c}, nfname)
movefile(out.pth.lab{c}, nfname);
end
out.pth.lab{c} = nfname;
if opt.do.go2native
p = out.pth.lab{c};
Mc = spm_get_space(p);
spm_get_space(p,M{c}*Mc);
end
if was_gz == true
% Compress back to .gz
p = gzip(out.pth.lab{c});
delete(out.pth.lab{c});
out.pth.lab{c} = p{1};
end
end
if ~isempty(pth_seg)
out.pth.seg = pth_seg;
if opt.do.go2native
for i1=1:numel(out.pth.seg{1})
p = deblank(out.pth.seg{1}{i1});
Mc = spm_get_space(p);
spm_get_space(p,M{c}*Mc);
end
end
if was_gz == true
% Compress back to .gz
p = gzip(out.pth.seg);
delete(out.pth.seg);
out.pth.seg = p{1};
end
end
if ~isempty(pth_norm)
out.pth.norm = pth_norm;
if was_gz == true
% Compress back to .gz
p = gzip(out.pth.norm);
delete(out.pth.norm);
out.pth.norm = p{1};
end
end
if ~isempty(P2d)
if i == 1
out.pth.im2d{c} = P2d{i}{c};
[pth,~,ext] = fileparts(out.pth.im2d{c});
nfname = fullfile(pth, [nams{1, c} ext]);
if ~strcmp(out.pth.im2d{c}, nfname)
movefile(out.pth.im2d{c}, nfname);
end
out.pth.im2d{c} = nfname;
if was_gz == true
% Compress back to .gz
p = gzip(oout.pth.im2d{c});
delete(out.pth.im2d{c});
out.pth.im2d{c} = p{1};
end
else
out.pth.lab2d{c} = P2d{i}{c};
[pth,~,ext] = fileparts(out.pth.lab2d{c});
nfname = fullfile(pth, [nams{2, c} ext]);
if ~strcmp(out.pth.lab2d{c}, nfname)
movefile(out.pth.lab2d{c}, nfname);
end
out.pth.lab2d{c} = nfname;
if was_gz == true
% Compress back to .gz
p = gzip(out.pth.lab2d{c});
delete(out.pth.lab2d{c});
out.pth.lab2d{c} = p{1};
end
end
end
if opt.do.writemat
[pth,nam] = fileparts(f);
nP = fullfile(pth,['mat' nam '.mat']);
Mc = M{c};
save(nP,'Mc')
end
end
end
%==========================================================================