-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDWIfromDICOMs.m
141 lines (122 loc) · 4.18 KB
/
getDWIfromDICOMs.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
function [dwiData, dwiData_tab] = getDWIfromDICOMs(vials_files, dicom_lists, dispOverlay, dispROIs)
if ~exist('dispOverlay', 'var')
dispOverlay = false;
end
if ~exist('dispROIs', 'var')
dispROIs = false;
end
%
% if ~iscell(vials_files)
% if ~iscell(dicom_lists)
% dicom_lists = {dicom_lists};
% end
% vials_files = {vials_files};
% end
for rep = 1:length(vials_files)
vials_file = vials_files{rep};
dicom_list = dicom_lists{rep};
if ischar(dicom_list)
number_dicoms = 1;
else
number_dicoms = length(dicom_list);
end
% GET VIAL DATA: Resample onto Vials Reference Image
load([vials_file '.mat'], 'bg_imge', 'bg_geomInfo', 'vials');
imgeVials = bg_imge;
geomInfoVials = bg_geomInfo;
for f =1:number_dicoms
if iscell(dicom_list)
trial = dicom_list{f};
elseif isstruct(dicom_list)
trial = dicom_list(f).name;
else
trial = dicom_list;
end
display(trial)
if any(matches(split(trial,'_'),'EPI')) %strcmp(trial(1:3), 'EPI')
cax = [0 4000];
else
cax = [0 3000];
end
% Load image
load([pwd '/DicomMats/' trial], 'imge','acqInfo','geomInfo', 'imge_reg');
if exist('imge_reg', 'var')
imge=imge_reg;
end
% Pad slice dimension if image is 2D for reslicing
if size(imge, 3) == 1
if ndims(imge) == 4
imge = repmat(imge,[1,1,2,1]);
elseif ndims(imge) == 5
imge = repmat(imge,[1,1,2,1,1]);
end
end
% Resample to Vial Reference
if size(imgeVials,[1,2]) ~= size(imge, [1,2])
for te = 1:size(imge,5)
[imge_interp(:,:,:,:,te)] = coregister_images('linear', imgeVials, geomInfoVials, imge, geomInfo);
end
if dispOverlay
overlayVolume(imgeVials(:,:,:,1), imge_interp(:,:,:,1,1), 'title',trial);
end
else
imge_interp = imge;
end
if dispROIs
figure;
imagesc(squeeze(imge_interp(:,:,13,1,1))); colormap('gray'); axis image; caxis(cax);
for v = 1:13
hold on; visboundaries(vials(v).circleROI_mask,'Color',vials(v).color,'EnhanceVisibility',0)
end
title(strrep(trial, '_', ' '));
saveas(gcf,['Mag_' trial(1:end-4)], 'png');
end
% Get the DWI signal for all vial ROIs and b-values
%S = zeros(length(inds),size(imge_interp,4), size(imge,5), length(vials));
for v = 1:length(vials)
inds = vials(v).circleROI_inds;
for i = 1:length(inds)
Sv(i,:,:,:) = squeeze(imge_interp(inds(i,1),inds(i,2), :, :, :));
end
S{v} = Sv;
clear Sv
end
% Fix the acqInfo CompositeDelay field. Add isComposite Field
clear bool_cell
parsed_trial = split(trial, '_');
bool_cell = strfind(parsed_trial, 'MELV');
ind_check = find(~cellfun(@isempty, bool_cell));
if isempty(ind_check)
is_composite = 0;
delay = nan;
else
is_composite = 1;
clear bool_cell
bool_cell = strfind(parsed_trial,'d');
ind_delay = find(~cellfun(@isempty, bool_cell));
delay_str = parsed_trial{ind_delay};
delay = str2double(delay_str(2:end));
end
% Create struct for storing DWI signals and image acquisition info
acqInfo.CompositeDelay = delay;
acqInfo.IsComposite = is_composite;
acqInfo.Filename = trial;
pltInfo = acqInfo;
pltInfo.Signal = S;
clear S
% Concatenate info for all files into single struct
if f == 1
pltInfoAll = pltInfo;
else
pltInfoAll = [pltInfoAll, pltInfo];
end
clear imge_reg imge_interp
end
if rep == 1
dwiData = pltInfoAll;
else
dwiData = [dwiData, pltInfoAll];
end
dwiData_tab = struct2table(dwiData);
end
end