-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtbx_cfg_superresolution.m
More file actions
585 lines (567 loc) · 16.8 KB
/
Copy pathtbx_cfg_superresolution.m
File metadata and controls
585 lines (567 loc) · 16.8 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
function root = tbx_cfg_superresolution
if ~isdeployed
addpath(fullfile(spm('Dir'),'toolbox','super-resolution'));
addpath(fullfile(spm('Dir'),'toolbox','Longitudinal'));
addpath(fullfile(spm('Dir'),'toolbox','Shoot'));
end
% -------------------------------------------------------------------------
plane = cfg_menu;
plane.tag = 'plane';
plane.name = 'In-plane profile';
plane.help = {
'Profile of the forward kernel in the in-plane directions.'
}';
plane.labels = {
'Rectangle'
'Gaussian'
}';
plane.values = {
0
2
}';
plane.val = {2};
% -------------------------------------------------------------------------
slice = cfg_menu;
slice.tag = 'slice';
slice.name = 'Slice profile';
slice.help = {
'Profile of the forward kernel in the slice (= thickest) direction.'
}';
slice.labels = {
'Rectangle'
'Gaussian'
}';
slice.values = {
0
2
}';
slice.val = {2};
% ----------------------------------------------v---------------------------
gap = cfg_entry;
gap.tag = 'gap';
gap.name = 'Slice gap';
gap.help = {
'Gap between slices (in percent).'
['In clinical 2D acquisitions, there is usually a gap between ' ...
'slices (i.e., only the central part of the slice id excited).']
}';
gap.strtype = 'r';
gap.num = [1 1];
gap.val = {1/3};
% -------------------------------------------------------------------------
acc = cfg_menu;
acc.tag = 'acc';
acc.name = 'Integration mode';
acc.help = {
'Integration mode of the forward kernel:'
'* Average: intensity is assumed to be independent of the voxel size,'
'* Accumulate: intensity is assumed to scale linearly with voxel size.'
}';
acc.labels = {
'Average'
'Accumulate'
}';
acc.values = {
0
1
}';
acc.val = {0};
% -------------------------------------------------------------------------
function elem = profile
elem = cfg_branch;
elem.tag = 'profile';
elem.name = 'Forward profile';
elem.val = {plane slice gap acc};
end
% -------------------------------------------------------------------------
function elem = vol(mode)
switch mode
case 'd'
elem = cfg_files;
elem.tag = 'files';
elem.name = 'Files';
elem.help = {
'Select scans from this channel for processing.'
}';
elem.filter = 'image';
elem.ufilter = '.*';
elem.num = [1 Inf];
elem.preview = @(f) spm_check_registration(char(f));
case 's'
elem = cfg_branch;
elem.tag = 'vol';
elem.name = 'Volume';
elem.val = {vol('d') profile};
end
end
% -------------------------------------------------------------------------
function elem = channel(mode)
elem = cfg_branch;
elem.tag = 'channel';
elem.name = 'Channel';
switch mode
case 'd'
elem.val = {vol(mode)};
case 's'
elem.val = {cfg_repeat};
elem.val{1}.tag = 'vols';
elem.val{1}.name = 'Volumes';
elem.val{1}.help = {'Select individual input volumes.'};
elem.val{1}.values = {vol(mode)};
elem.val{1}.val = {vol(mode)};
elem.val{1}.num = [1 Inf];
end
end
% -------------------------------------------------------------------------
function elem = data(mode)
switch mode
case 'd'
verb = 'denoised';
case 's'
verb = 'super-resolved';
end
elem = cfg_repeat;
elem.tag = 'channels';
elem.name = 'Channels';
elem.values = {channel(mode)};
elem.help = {
'Specify the number of different channels.'
['If you have scans of different contrasts that represent the ' ...
'same subject/organ/object, they can be ' verb ' together. ' ...
'The model assumes that edges are localised in similar locations ' ...
'across contrasts.']
}';
elem.val = {channel(mode)};
elem.num = [1 Inf];
end
% -------------------------------------------------------------------------
function elem = modelog
elem = cfg_menu;
elem.tag = 'log';
elem.name = 'Log-encoding';
elem.help = {
'Encode the reconstructed images by their log.'
['This ensures their positivity, and makes the regularisation ' ...
'scale-independant. However, the algorithm becomes a bit slower.']
}';
elem.labels = {'No' 'Yes'};
elem.values = {0 1};
elem.val = {0};
end
% -------------------------------------------------------------------------
function elem = solver(val)
elem = cfg_entry;
switch val
case 'fmg'
elem.tag = 'fmg';
elem.name = 'FMG';
elem.help = {
'Parameters of the full-multi-grid solver:'
'(1) Number of Full Multigrid cycles,'
'(2) Number of relaxation iterations per cycle.'
};
elem.strtype = 'i';
elem.val = {[2 2]};
elem.num = [1 2];
case 'cg'
elem.tag = 'cg';
elem.name = 'CG';
elem.help = {
'Number of conjugate gradient iterations.'
};
elem.strtype = 'i';
elem.val = {50};
elem.num = [1 1];
case 'relax'
elem.tag = 'relax';
elem.name = 'Relax';
elem.help = {
'Number of relaxation iterations.'
};
elem.strtype = 'i';
elem.val = {0};
elem.num = [1 1];
end
end
% -------------------------------------------------------------------------
function elem = solvers(val)
elem = cfg_branch;
elem.tag = 'solver';
elem.name = 'Solver';
switch val
case 1
elem.val = {solver('fmg') solver('cg') solver('relax')};
case 2
elem.val = {solver('fmg')};
end
end
% -------------------------------------------------------------------------
function elem = regtype(val)
switch val
case 0
elem = cfg_const;
elem.tag = 'none';
elem.name = 'None';
elem.val = {0};
elem.hidden = true;
case 1
elem = cfg_branch;
elem.tag = 'tv';
elem.name = 'Joint Total-Variation';
elem.val = {cfg_entry cfg_entry solvers(1)};
elem.val{1}.tag = 'val';
elem.val{1}.name = 'Value';
elem.val{1}.strtype = 'r';
elem.val{1}.val = {5E4};
elem.val{1}.num = [1 1];
elem.val{1}.help = {
'Regularisation value (expected variance of the gradients).'
}';
elem.val{2}.tag = 'smooth';
elem.val{2}.name = 'Smoother';
elem.val{2}.strtype = 'r';
elem.val{2}.val = {1E-3};
elem.val{2}.num = [1 1];
elem.val{2}.help = {
'RLS smoothing term (should be small).'
}';
case 2
elem = cfg_branch;
elem.tag = 'tkh';
elem.name = 'Tikhonov';
elem.val = {cfg_entry solvers(2)};
elem.val{1}.tag = 'val';
elem.val{1}.name = 'Value';
elem.val{1}.strtype = 'r';
elem.val{1}.val = {5E4};
elem.val{1}.num = [1 1];
elem.val{1}.help = {
'Regularisation value (expected variance of the gradients).'
}';
end
end
% -------------------------------------------------------------------------
function elem = reg
elem = cfg_choice;
elem.tag = 'reg';
elem.name = 'Regularisation type';
elem.help = {
'Type of image prior:'
'* None: maximum-likelihood reconstruction,'
'* Joint Total-Variation: edge-preserving prior (\ell_{2,1})'
'* Tikhonov: smoothing prior (\ell_2)'
}';
elem.values = {regtype(0) regtype(1) regtype(2)};
elem.val = elem.values(2);
end
% -------------------------------------------------------------------------
function elem = coregtype(val)
switch val
case 0
elem = cfg_const;
elem.tag = 'no';
elem.name = 'No';
elem.val = {0};
elem.hidden = true;
case 1
elem = cfg_branch;
elem.tag = 'yes';
elem.name = 'Yes';
elem.val = {cfg_entry};
elem.val{1}.tag = 'fwhm';
elem.val{1}.name = 'FWHM';
elem.val{1}.strtype = 'r';
elem.val{1}.val = {[21 14 7]};
elem.val{1}.num = [1 Inf];
elem.val{1}.help = {
'List of full-width at half-maximum to use for co-registration.'
'The FWHM specifies the extent of smoothing of the joint-histogram.'
'Starting with more smoothing can help with difficult cases.'
}';
end
end
% -------------------------------------------------------------------------
function elem = coreg
elem = cfg_choice;
elem.tag = 'coreg';
elem.name = 'Co-registration';
elem.help = {
'Co-register input volumes.'
}';
elem.values = {coregtype(0) coregtype(1)};
elem.val = elem.values(2);
end
% -------------------------------------------------------------------------
function elem = itermax
elem = cfg_entry;
elem.tag = 'max';
elem.name = 'Max';
elem.help = {
'Maximum number of Gauss-Newton / Reweighted Least Squares iterations.'
};
elem.strtype = 'i';
elem.val = {10};
elem.num = [1 1];
end
% -------------------------------------------------------------------------
function elem = tol
elem = cfg_entry;
elem.tag = 'tol';
elem.name = 'Tolerance';
elem.help = {
'Tolerance for early stopping.'
'The tolerance relates to the gain in joint log-likelihood.'
};
elem.strtype = 'r';
elem.val = {1E-3};
elem.num = [1 1];
end
% -------------------------------------------------------------------------
function elem = armijo
elem = cfg_entry;
elem.tag = 'armijo';
elem.name = 'Armijo factors';
elem.help = {
'Gauss-Newton damping factors.'
['If the damping factor is larger than 1, the Gauss-Newton update ' ...
'step is reduced by as much. This prevents the algorithm to '...
'overshoot during the first few iterations.']
};
elem.strtype = 'r';
elem.val = {[2 1]};
elem.num = [1 Inf];
end
% -------------------------------------------------------------------------
function elem = iter
elem = cfg_branch;
elem.tag = 'iter';
elem.name = 'Iterations';
elem.val = {itermax tol armijo};
end
% -------------------------------------------------------------------------
function elem = verbose
elem = cfg_menu;
elem.tag = 'verbose';
elem.name = 'Verbosity';
elem.labels = {
'Quiet'
'Print'
'Plot'
};
elem. values = {
0
1
2
};
elem.val = {1};
end
% -------------------------------------------------------------------------
function elem = mapout
elem = cfg_menu;
elem.tag = 'map';
elem.name = 'Memory Map';
elem.help = {
'Memory-map output data.'
'Memory-mapping requires more i/o operations but saves RAM.'
};
elem.labels = {'No' 'Yes'};
elem.values = {0 1};
elem.val = {1};
end
% -------------------------------------------------------------------------
function elem = prefix
elem = cfg_entry;
elem.tag = 'prefix';
elem.name = 'Prefix';
elem.help = {'Prefix for output filenames'};
elem.strtype = 's';
elem.num = [1 Inf];
elem.val = {'sr_'};
end
% -------------------------------------------------------------------------
function elem = output
elemi = cfg_const;
elemi.tag = 'same';
elemi.name = 'Same as input';
elemi.help = {'Output images are written in the same folder as the input files.'};
elemi.val = {''};
elemi.hidden = false;
elemo = cfg_files;
elemo.tag = 'folder';
elemo.name = 'Folder';
elemo.help = {'Select output folder'};
elemo.filter = 'dir';
elemo.ufilter = '.*';
elemo.num = [1 1];
elemo.hidden = false;
elem = cfg_choice;
elem.tag = 'output';
elem.name = 'Output';
elem.values = {elemi elemo};
elem.val = {elemi};
end
% -------------------------------------------------------------------------
function elem = io
elem = cfg_branch;
elem.tag = 'io';
elem.name = 'IO';
elem.help = {'Input/Output options.'};
elem.val = {output prefix mapout};
end
% -------------------------------------------------------------------------
function elem = vs
elem = cfg_entry;
elem.tag = 'vs';
elem.name = 'Voxel Size';
elem.help = {
'Target voxel size.'
};
elem.strtype = 'r';
elem.val = {NaN};
elem.num = [1 1];
end
% -------------------------------------------------------------------------
function elem = opt(mode)
elem = cfg_branch;
elem.tag = 'opt';
elem.name = 'Options';
elem.val = {};
if mode == 's'
elem.val{1} = vs;
end
elem.val = [elem.val {io modelog reg coreg iter verbose}];
end
% -------------------------------------------------------------------------
function elem = sr(mode)
elem = cfg_branch;
switch mode
case 'd'
elem.tag = 'denoise';
elem.name = 'Denoising';
elem.help = {'Denoise a multi-channel datasets.'};
case 's'
elem.tag = 'superres';
elem.name = 'Super-Resolution';
elem.help = {'Super-resolve a multi-channel datasets.'};
end
elem.val = {data(mode) opt(mode)};
end
% -------------------------------------------------------------------------
function elem = mode
elem = cfg_choice;
elem.tag = 'mode';
elem.name = 'Mode';
elem.help = {'Choose between denoising and super-Resolution.'};
elem.values = {sr('d') sr('s')};
elem.val = elem.values(2);
end
% -------------------------------------------------------------------------
root = cfg_exbranch;
root.tag = 'sr';
root.name = 'Denoising / Super-Resolution';
root.val = {mode};
root.help = {'Denoise and/or super-resolve multi-channel datasets.'};
root.prog = @sr_run;
root.vout = @vout_create;
end
%==========================================================================
function out = sr_run(job)
% ----
% Mode
% ----
opt = struct;
opt.mode = fieldnames(job.mode);
opt.mode = opt.mode{1};
Nc = numel(job.mode.(opt.mode).channel);
in = cell(1,Nc);
% -----------------------
% Input and slice profile
% -----------------------
if opt.mode(1) == 's'
% Super-resolution
opt.slice.thickest = cell(1,Nc);
opt.slice.other = cell(1,Nc);
opt.slice.gap = cell(1,Nc);
opt.slice.accumulate = cell(1,Nc);
for c=1:Nc
in{c} = {};
for v=1:numel(job.mode.(opt.mode).channel(c).vol)
files = job.mode.(opt.mode).channel(c).vol(v).files;
thickest = job.mode.(opt.mode).channel(c).vol(v).profile.slice;
other = job.mode.(opt.mode).channel(c).vol(v).profile.plane;
gap = job.mode.(opt.mode).channel(c).vol(v).profile.gap;
accumulate = job.mode.(opt.mode).channel(c).vol(v).profile.acc;
Nf = size(files,1);
in{c} = cat(1, in{c}, cellstr(job.mode.(opt.mode).channel(c).vol.files));
opt.slice.thickest{c} = cat(1, opt.slice.thickest{c}, thickest*ones(Nf,1));
opt.slice.other{c} = cat(1, opt.slice.other{c}, other*ones(Nf,1));
opt.slice.gap{c} = cat(1, opt.slice.gap{c}, gap*ones(Nf,1));
opt.slice.accumulate{c} = cat(1, opt.slice.accumulate{c}, accumulate*ones(Nf,1));
end
end
opt.vs = job.mode.(opt.mode).opt.vs;
else
% Denoising
for c=1:Nc
in{c} = cellstr(job.mode.(opt.mode).channel(c).files);
end
end
% -------
% Options
% -------
if isfield(job.mode.(opt.mode).opt.io.output, 'same')
opt.out.folder = '';
else
opt.out.folder = job.mode.(opt.mode).opt.io.output.folder{1};
end
opt.out.prefix = job.mode.(opt.mode).opt.io.prefix;
if job.mode.(opt.mode).opt.io.map
opt.out.mem = 'map';
else
opt.out.mem = 'load';
end
opt.log = job.mode.(opt.mode).opt.log;
regmode = fieldnames(job.mode.(opt.mode).opt.reg);
regmode = regmode{1};
switch regmode
case 'none'
opt.reg.mode = 0;
case 'tv'
opt.reg.mode = 1;
case 'tkh'
opt.reg.mode = 2;
end
opt.reg.value = job.mode.(opt.mode).opt.reg.(regmode).val;
opt.reg.smo = job.mode.(opt.mode).opt.reg.(regmode).smooth;
opt.solver.fmg = job.mode.(opt.mode).opt.reg.(regmode).solver.fmg;
opt.solver.cg = job.mode.(opt.mode).opt.reg.(regmode).solver.cg;
opt.solver.relax = job.mode.(opt.mode).opt.reg.(regmode).solver.relax;
opt.coreg.do = isfield(job.mode.(opt.mode).opt.coreg, 'yes');
if opt.coreg.do
opt.coreg.fwhm = job.mode.(opt.mode).opt.coreg.yes.fwhm;
end
opt.itermax = job.mode.(opt.mode).opt.iter.max;
opt.tolerance = job.mode.(opt.mode).opt.iter.tol;
opt.armijo = job.mode.(opt.mode).opt.iter.armijo;
opt.verbose = job.mode.(opt.mode).opt.verbose;
% ---
% Run
% ---
out = sr_fit(in, opt);
out = out.fnames;
end
%==========================================================================
function cdep = vout_create(job)
mode = fieldnames(job.mode);
mode = mode{1};
for c=1:numel(job.mode.(mode).channel)
cdep(c) = cfg_dep;
if mode(1) == 'd'
cdep(c).sname = sprintf('Denoised contrast (%d)', c);
else
cdep(c).sname = sprintf('Super-resolved contrast (%d)', c);
end
cdep(c).src_output = substruct('.','channel','()',{c},'.','recon','()',{':'});
cdep(c).tgt_spec = cfg_findspec({{'filter','image','strtype','e'}});
end
end