forked from tkkarjal/magia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_nifti.m
56 lines (50 loc) · 1.36 KB
/
convert_to_nifti.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
function convert_to_nifti(source_dir,nii_dir,filename,varargin)
% The function assumes that source_dir contains only either
% i) a bunch of dicom files
% ii) a single ecat file (either .v or .img)
% These files are then converted into a single (4D when applicable) nifti
if(nargin==4)
scanner = varargin{1};
else
scanner = '';
end
[p,n,e] = fileparts(filename);
if(isempty(p))
p = nii_dir;
end
if(isempty(e))
e = '.nii';
end
filename = fullfile(p,[n e]);
cmd = sprintf('gunzip -r %s',source_dir);
system(cmd);
f = get_filenames(source_dir,'*.dcm');
if(~isempty(f))
nii_fnames = spm_dcm2nii_2(source_dir,nii_dir);
if(length(nii_fnames)==1)
movefile(nii_fnames{1},filename,'f');
else
if(strcmp(scanner,'pet-mri'))
I = length(nii_fnames):-1:1;
nii_fnames = nii_fnames(I);
end
spm_nifti_dynamize(nii_fnames,filename); % converts from 3d to 4d
end
else
f = get_filenames(source_dir,'*.v');
if(isempty(f))
f = get_filenames(source_dir,'*.img');
end
if(~isempty(f))
[p,~,e] = fileparts(filename);
h = fullfile(p,['temp' e]);
convert_ecat2nii(f{1},h);
spm_nifti_dynamize({h},filename);
delete(h);
else
error('Could not find dcm or ecat files from %s.\n',source_dir);
end
end
cmd = sprintf('gzip -r %s',source_dir);
system(cmd);
end