-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_dicoms_to_nifti.m
138 lines (129 loc) · 7.98 KB
/
convert_dicoms_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
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
function convert_dicoms_to_nifti(dicom_data_dir, nifti_data_dir, omit_check)
%% runs loop over all raw data dicom folders to convert to nifti
%% uses hMRI toolbox batch, so needs to be run from SPM environment
%%% this function also checks if the conversion has been done correctly
%%% in different ways, which do not work for certain sequences (in
%%% particular DTI). if this does not work for you, use the function
%%% "convert_dicoms_to_nifti_no_check.m"
%foldernames = dir([dicom_data_dir,'/S*']); %%% just consider folders starting with S for series
foldernames = dir([dicom_data_dir,'/*_*']); %%% just consider folders that have underscores in them
%%% hMRI toolbox for dicom conversion
%%% raw data are .ima images; convert dicoms in all folders to nifti files
for folder = 1:length(foldernames)
%%% check whether has already been done
str_to_check = foldernames(folder).name;
str_to_check_2 = strsplit(str_to_check, '_');
if strcmp(str_to_check_2{1}(1),'S')
str_to_check_3 = strsplit(str_to_check_2{1}, 'S');
sequence_number = str2num(str_to_check_3{2});
else
sequence_number = str2num(str_to_check_2{1});
end
nifti_files_there = dir([nifti_data_dir,'/*',sprintf('%04d',sequence_number),'/*.nii']);
if length(nifti_files_there) == 0 && sequence_number ~= 99 && ~contains(str_to_check,'[') % changed to <100 because these are not so releant; ~= 99 %%% only do conversion if no nifti files are in the folder and if is not the stupid phoenix report sequence
%%% check for files with .ima extension, as for Leipzig scanner
filenames = dir([foldernames(folder).folder,'/',foldernames(folder).name,'/*.ima']);
%%% if no files are found, check for filenames with MR at
%%% beginning, as for Magdeburg scanner
if length(filenames) == 0
filenames = dir([foldernames(folder).folder,'/',foldernames(folder).name,'/MR*']);
end
if length(filenames) == 0
filenames = dir([foldernames(folder).folder,'/',foldernames(folder).name,'/*.dcm']);
end
if length(filenames) > 0
clear filename_structure
%%% save name of all dicoms in a cell array
% for file = 1:length(filenames)
% filename_structure{file} = [foldernames(folder).folder,'/',foldernames(folder).name,'/',filenames(file).name];
% end
filename_structure = convert_dir_output_to_cell_structure(filenames);
%%% define an spm job
spm_jobman('initcfg');
spm('defaults', 'PET');
dicom2nifti_batch{1}.spm.tools.hmri.dicom.data = filename_structure; %%% as a batch can include several steps, we put the conversion as first step {1}, put as input all files in dir
dicom2nifti_batch{1}.spm.tools.hmri.dicom.outdir = {nifti_data_dir}; %%% set output directory
dicom2nifti_batch{1}.spm.tools.hmri.dicom.protfilter = '.*';
dicom2nifti_batch{1}.spm.tools.hmri.dicom.convopts.format = 'nii';
dicom2nifti_batch{1}.spm.tools.hmri.dicom.root = 'series';
dicom2nifti_batch{1}.spm.tools.hmri.dicom.convopts.metaopts.mformat = 'sep'; %%% luke's script needs json files
dicom2nifti_batch{1}.spm.tools.hmri.dicom.convopts.icedims = 0;
% if find(contains(str_to_check,'MAFI'))
% dicom2nifti_batch{1}.spm.tools.hmri.dicom.convopts.icedims = 1;
% end
%dicom2nifti_batch{1}.spm.tools.hmri.dicom.data;
%dicom2nifti_batch{1}.spm.tools.hmri.dicom.outdir
display('converting sequence')
nifti_data_dir
sequence_number
try
spm_jobman('run',dicom2nifti_batch);
end
if omit_check == 0
%%% check that the right number of niftis have been
%%% produced and that they have the correct number of
%%% slices
correctly_done = 0;
while correctly_done == 0
%%% get list of output files
nifti_files_produced = dir([nifti_data_dir,'/*',sprintf('%04d',sequence_number),'/*.nii']);
%%% based on what it produced (number of nifti
%%% files and number of slices in each), calculate
%%% the number of dicoms
dicoms_expected = 0;
for niftifile = 1:length(nifti_files_produced)
filename = [nifti_files_produced(niftifile).folder, '/', nifti_files_produced(niftifile).name];
VCON = spm_vol(filename);
VCON.dim;
dicoms_expected = dicoms_expected + VCON.dim(3); %%% should work as no timeseries
end
number_of_dicoms = length(filenames);
dicoms_expected
number_of_dicoms
if dicoms_expected == 0 || dicoms_expected == number_of_dicoms
display('they match')
correctly_done = 1;
elseif contains(str_to_check, 'ep2d_diff') %%% for diffusion sequence this is nonsense
correctly_done = 1;
else
%%% delete output nifti directory and redo
error = system(['yes | rm -R ',nifti_data_dir,'/*',sprintf('%04d',sequence_number)]);
error
if error == 0
spm_jobman('run',dicom2nifti_batch);
else %%% stupid workaround
correctly_done = 1;
display('no nifti folder has ever been created')
end
end
end
display('correctly done')
% %%% for important sequences check if they have been correctly
% %%% reconstructed
if contains(str_to_check, 'mtflash3d') || contains(str_to_check, 'gre_field') || contains(str_to_check, 'kp_seste') || contains(str_to_check, 'b1mapping')
correctly_done = 0;
%%% convert this folder as many times as needed until the
%%% files all have same dimensions
while correctly_done == 0
display('converting sequence')
sequence_number
%%% get list of output files
nifti_files_produced = dir([nifti_data_dir,'/*',sprintf('%04d',sequence_number),'/*.nii']);
%%% now check whether it has been correctly
correct_or_not = check_if_all_nifti_files_have_same_dimensions(nifti_files_produced)
if correct_or_not ~= 0 %%% then it has been correctly done
display('already good')
correctly_done = 1
else %%% delete whole folder now
display('doing again')
system(['yes | rm -R ',nifti_data_dir,'/*',sprintf('%04d',sequence_number)]);
spm_jobman('run',dicom2nifti_batch);
end
end
display('correctly done double checked')
end
end
end
end
end
end