forked from netstim/leaddbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathea_coreg_all_mri.m
97 lines (84 loc) · 3.71 KB
/
ea_coreg_all_mri.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
function ea_coreg_all_mri(options,usebrainmask)
% __________________________________________________________________________________
% Copyright (C) 2015 Charite University Medicine Berlin, Movement Disorders Unit
% Andreas Horn
if ~exist('usefa','var')
usefa=1;
end
[options,presentfiles]=ea_assignpretra(options);
directory=[options.root,options.patientname,filesep];
for coregfi=2:length(presentfiles)
if ~ea_coreglocked(options,presentfiles{coregfi}) % file has already been locked and approved by used
ea_coreg2images(options,[directory,presentfiles{coregfi}],[directory,presentfiles{1}],[directory,presentfiles{coregfi}]);
% reslice images if needed
V1=ea_open_vol([directory,presentfiles{1}]);
V2=ea_open_vol([directory,presentfiles{coregfi}]);
if ~isequal(V1.mat,V2.mat)
ea_conformspaceto([directory,presentfiles{1}],[directory,presentfiles{coregfi}],1);
end
% better slab support:
nii=ea_load_nii(V2.fname);
nii.img(abs(nii.img)<0.0001)=0;
ea_write_nii(nii);
end
end
function masks=segmentall(from,options)
directory=[fileparts(from{1}),filesep];
for fr=1:length(from)
[~,fn,ext]=fileparts(from{fr});
switch [fn,ext]
case options.prefs.fa2anat
if ~exist([directory,'tc2',options.prefs.prenii_unnormalized],'file')
ea_newseg(directory,options.prefs.prenii_unnormalized,0,options);
% assume that tc2 doesn't exist
nii=ea_load_nii([directory,'c2',options.prefs.prenii_unnormalized]);
nii.img=nii.img>0.7;
nii.fname=[directory,'tc2',options.prefs.prenii_unnormalized];
spm_write_vol(nii,nii.img);
end
masks{fr,1}=[ea_space(options),'c2mask.nii'];
masks{fr,2}=[directory,'tc2',options.prefs.prenii_unnormalized];
otherwise
if ~exist([directory,'tc1',options.prefs.prenii_unnormalized],'file')
ea_newseg(directory,options.prefs.prenii_unnormalized,0,options);
% assume that tc1 doesn't exist
nii=ea_load_nii([directory,'c1',options.prefs.prenii_unnormalized]);
nii.img=nii.img>0.3;
nii.fname=[directory,'tc1',options.prefs.prenii_unnormalized];
spm_write_vol(nii,nii.img);
end
masks{fr,1}=[ea_space(options),'c1mask.nii'];
masks{fr,2}=[directory,'tc1',options.prefs.prenii_unnormalized];
end
end
if ~exist([directory,'tc1c2',options.prefs.prenii_unnormalized],'file')
Vc1=ea_load_nii([directory,'tc1',options.prefs.prenii_unnormalized]);
Vc2=ea_load_nii([directory,'tc2',options.prefs.prenii_unnormalized]);
Vc1.img=Vc1.img+Vc2.img;
Vc1.fname=[directory,'tc1c2',options.prefs.prenii_unnormalized];
spm_write_vol(Vc1,Vc1.img);
end
function ea_genbrainmask(options)
directory=[options.root,options.patientname,filesep];
ea_newseg(directory,options.prefs.prenii_unnormalized,0,options);
c1=ea_load_nii([directory,'c1',options.prefs.prenii_unnormalized]);
c2=ea_load_nii([directory,'c2',options.prefs.prenii_unnormalized]);
c3=ea_load_nii([directory,'c3',options.prefs.prenii_unnormalized]);
bm=c1;
bm.img=c1.img+c2.img+c3.img;
bm.fname=[directory,'brainmask.nii'];
bm.img=bm.img>0.5;
spm_write_vol(bm,bm.img);
function ea_maskimg(options,file,prefix)
directory=[options.root,options.patientname,filesep];
if ~exist([directory,'brainmask.nii'],'file')
ea_genbrainmask(options);
end
[pth,fn,ext]=fileparts(file);
if ~exist([pth,filesep,prefix,fn,ext],'file')
nii=ea_load_nii(file);
bm=ea_load_nii([directory,'brainmask.nii']);
nii.img=nii.img.*double(bm.img);
nii.fname=[pth,filesep,prefix,fn,ext];
spm_write_vol(nii,nii.img);
end