-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicom_chk.m
423 lines (422 loc) · 11.7 KB
/
dicom_chk.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
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
%#######################################################################
%
% * DICOM CHecK Program *
%
% M-File which reads a series of DICOM directories, collects
% information about the DICOM files, plots the T1 or T2 data, and
% fits either spin lock (T1rho) or echo times (T2*) for a couple
% of slices. Plots are output to Postscript files.
%
% NOTES: 1. Reads Philips MRI DICOM files. May not work with
% other types of images.
%
% 2. Program reads and collects information from the first
% DICOM file header in each series.
%
% 3. The program traps for some, but not all parameters in
% the DICOM file header.
%
% 4. The program looks for subdirectories of the current
% directory that begin with the letter "s". The user
% than selects the subdirectories for processing.
%
% 5. The program uses the series description to find the
% spin lock times for the T1rho series. If the delimiter,
% "SL", is not found, a warning is issued and the spin lock
% times are assumed to be 0, 10, 40 and 80 ms.
%
% 6. The M-files T1r3d_calc.m, dftreg.m, dftregistration.m
% and exp1_fun.m must be in the current path or directory.
%
% 7. The 2D translation image registration is done by the
% Mathworks File Exchange function, dftregistration.m. See:
%
% https://www.mathworks.com/matlabcentral/fileexchange/18401-efficient-subpixel-image-registration-by-cross-correlation
%
% 8. The Matlab Optimization toolbox is required. The
% nonlinear least squares is performed by the Matlab
% function lsqcurvefit in the optimization toolbox.
%
% 9. The nonlinear least squares uses the Levenberg-
% Marquardt algorithm in the Matlab function lsqcurvefit.
%
% 10. The Matlab Parallel Computing toolbox is required.
% The Matlab parallel construct parfor is used to calculate
% the T1rho/T2* values in parallel. Use Matlab command
% parpool to control the number of workers.
%
% 30-Jun-2021 * Mack Gardner-Morse
%
%#######################################################################
%
% Get Series Directories with DICOM Images
%
ddirs = dir('s*');
ddirs = ddirs([ddirs.isdir]');
ddirs = {ddirs.name}';
[isel,ok] = listdlg('ListString',ddirs,'SelectionMode','multiple', ...
'Name','MRI Series Directories','PromptString', ...
'Select MRI Series: ','ListSize',[150 400]);
%
if ok<1
error(' *** dicom_chk: No series selected!');
end
%
ddirs = ddirs(isel);
ns = size(ddirs,1); % Number of series
%
% Loop through the Series and Get Header Information
%
nimages = zeros(ns,1); % Number of DICOM files
afiles = cell(ns,1); % All DICOM file in series
%
etn = zeros(ns,1); % Echo times as numbers for UTE T2 star sequences
psz = zeros(ns,2); % Rows and columns
pspc = zeros(ns,2); % Pixel Spacing
stxt = cell(ns,1); % Series Description
sn = zeros(ns,1); % Series number
sdat = datetime(zeros(ns,1),0,0,'Format','dd-MMM-yyyy HH:mm:ss');
%
for k = 1:ns
%
% Get DICOM Files
%
ddir = ddirs{k};
dfiles = dir(fullfile(ddir,'i*.dcm'));
dfiles = dfiles(~[dfiles.isdir]');
nimages(k) = size(dfiles,1);
afiles{k} = {dfiles.name}';
%
fnam = afiles{k}{1};
fnam = fullfile(ddir,fnam);
%
% Get DICOM Header Information from Files
%
if exist(fnam,'file')
%
info = dicominfo(fnam);
%
if isfield(info,'Rows')
psz(k,:) = [info.Rows info.Columns];
end
if isfield(info,'PixelSpacing')
pspc(k,:) = info.PixelSpacing';
end
stxt{k} = info.SeriesDescription;
sn(k) = info.SeriesNumber;
if isfield(info,'EchoTrainLength')
n = info.EchoTrainLength;
if n>nimages(k)
n = nimages(k);
end
et = NaN(n,1); % Echo times for this sequence
et(1) = info.EchoTime;
for m = 2:n
fnam = afiles{k}{m};
fnam = fullfile(ddir,fnam);
if exist(fnam,'file')
info1 = dicominfo(fnam);
et(m) = info1.EchoTime;
else
break;
end
end
etu = unique(et);
idnnan = ~isnan(etu);
etn(k) = etu(idnnan);
end
dat = info.SeriesDate;
tim = info.SeriesTime;
sdat(k) = datetime([dat tim],'InputFormat','yyyyMMddHHmmss.SSSSS');
end
%
end
%
% Get Spin Lock Times from Series Description
%
if contains(stxt,'rho','IgnoreCase',true)
idvr = contains(stxt,'SL'); % Should contain TSL
%
if ~any(idvr)
slt = [0 10 40 80]';
nslt = 4;
warning([' *** dicom_chk: Not able to get spin lock times', ...
' from series description!']);
else
slt = extractBetween(stxt(idvr),'SL','ms'); % Spin lock times as text
slt = strrep(slt,'_',' ');
slt = strtrim(slt);
slt = strrep(slt,' ',',');
slt = eval(['[' slt{1} ']'])';
nslt = size(slt,1);
if nslt~=4
error(' *** dicom_chk: Incorrect number of spin lock times!');
end
end
end
%
% T1rho
%
if ns==1&&exist('slt','var') % T1rho
%
t0 = 80; % Initial value for T1rho
%
% Get Index to Files and Get File Names
%
ddir = ddirs{1};
fnams = afiles{1};
nf = size(fnams,1); % Number of files
nsl = nf/nslt; % Number of slices
%
plt_sl = floor(nsl/4);
% plt_sl = plt_sl:plt_sl:3*plt_sl; % Slices to plot
plt_sl = [plt_sl; 3*plt_sl]; % Slices to plot
nplts = size(plt_sl,1);
%
% Get File Name Description and PS File Name
%
sn1 = int2str(sn(1));
fs = ['S' sn1];
ds = [' for Series ' sn1]; % Description with series number
%
pnam = ['T1rho_' fs '.ps']; % Plot file name
%
% Set Up Array for Loop
%
psz = psz(1,:);
npx = psz(1);
npy = psz(2);
%
dat3d = zeros([psz nslt]);
T1rhonls = zeros(npx,npy,nplts); % Nonlinear least squares time constant
%
% Loop through Plot Slices
%
for k = 1:nplts
%
ks = plt_sl(k);
sll = int2str(ks); % Slice number as letters
%
% Loop through Spin Lock Times
%
for l = 1:nslt % Loop through spin lock times
%
n = nslt*(ks-1)+l;
fnam = fnams{n}; % Filename for this spin lock time
fprintf(1,['\n Processing file: ' strrep(fnam,'\','\\') ...
', Slice: ' sll ', Spin lock time: ' ...
int2str(slt(l)) ' ms']);
%
% Load and Scale Image
%
img = dicomread(fullfile(ddir,fnam));
info = dicominfo(fullfile(ddir,fnam));
sl = double(info.RescaleSlope);
offst = double(info.RescaleIntercept); % Usually zero
r = sl*double(img)+offst;
dat3d(:,:,l) = r;
%
% Registration of Images for the Different Spin Lock Times
%
if l>1
dat3d(:,:,l) = dftreg(dat3d(:,:,l-1),dat3d(:,:,l),100);
end
end
%
fprintf(1,'\n'); % Line between slices
%
% Plot a Montage of Each Slice
%
figure;
orient landscape;
montage(dat3d(:,:,:),'DisplayRange',[0 0.95*max(dat3d(:))]);
title({['T1' ds]; ['Slice ' sll]},'FontSize',20, ...
'FontWeight','bold');
pos = get(gca,'pos');
pos(2) = 0.02;
set(gca,'pos',pos);
if k==1
print('-dpsc2','-r600','-fillpage',pnam);
else
print('-dpsc2','-r600','-fillpage','-append',pnam);
end
%
% Calculate T1rho Values for the Whole Slice
%
T1rhonls(:,:,k) = T1r3d_calc(dat3d,slt,t0);
%
% Plot T1rho Values for this Slice
%
figure;
orient landscape;
%
img = T1rhonls(:,:,k);
img(img<0) = 0;
img(img>100) = 100;
imagesc(img,[0 100]);
%
colormap jet;
axis image;
axis off;
%
colorbar;
%
title({['T1\rho' ds]; ['Slice ' sll]},'FontSize',16, ...
'FontWeight','bold');
%
print('-dpsc2','-r600','-fillpage','-append',pnam);
%
end
%
% T2*
%
elseif ns==5||ns==6 % T2*
%
t0 = 35; % Initial value for T2*
%
% Sort Echo Times
%
[etn,ids] = sort(etn);
ddirs = ddirs(ids);
afiles = afiles(ids);
%
% Get Number of Files and Slice Numbers
%
nf = zeros(ns,1);
%
for k = 1:ns
nf(k) = size(afiles{k},1); % Number of files
end
%
if any(diff(nf))
error([' *** dicom_chk: Inconsistent number of files in', ...
' directories!']);
end
%
nsl = nf(1); % Number of slices = number of files in directories
%
plt_sl = floor(nsl/4);
% plt_sl = plt_sl:plt_sl:3*plt_sl; % Slices to plot
plt_sl = [plt_sl; 3*plt_sl]; % Slices to plot
nplts = size(plt_sl,1);
%
% Get File Name Description and PS File Name
%
sns = int2str(sn);
sns = [sns repmat(',',ns,1)];
sna = sns(1,:);
%
for k = 2:ns
sna = [sna sns(k,:)];
end
%
sna = sna(1:end-1);
sna = sna(~isspace(sna));
sna = strrep(sna,',',', ');
%
fs = ['S' strip(sns(1,1:end-1)) '_' strip(sns(ns,1:end-1))];
ds = [' for Series ' sna]; % Description with series number
%
pnam = ['T2star_' fs '.ps']; % Plot file name
%
% Set Up Array for Loop
%
if any(any(diff(psz)))
error([' *** dicom_chk: Inconsistent size of images in', ...
' directories!']);
end
%
psz = psz(1,:);
npx = psz(1);
npy = psz(2);
%
dat3d = zeros([psz ns]);
T2starnls = zeros(npx,npy,nplts); % Nonlinear least squares time constant
%
% Loop through Plot Slices
%
for k = 1:nplts
%
ks = plt_sl(k);
sll = int2str(ks); % Slice number as letters
%
% Loop through Echo Times/Subdirectories
%
for l = 1:ns % Loop through echo times/directories
%
ddir = ddirs{l};
fnam = afiles{l}{ks}; % Filename for this echo time and slice
fnam = fullfile(ddir,fnam);
fprintf(1,['\n Processing file: ' strrep(fnam,'\','\\') ...
', Slice: ' sll ', Echo time: ' ...
num2str(etn(l)) ' ms']);
%
% Load and Scale Image
%
img = dicomread(fnam);
info = dicominfo(fnam);
img = double(img);
sl = double(info.RescaleSlope);
offst = double(info.RescaleIntercept); % Usually zero
r = sl*double(img)+offst;
dat3d(:,:,l) = r;
%
% Registration of Images for the Different Echo Times
%
if l>1
dat3d(:,:,l) = dftreg(dat3d(:,:,l-1),dat3d(:,:,l),100);
end
end
%
fprintf(1,'\n'); % Line between slices
%
% Plot a Montage of Each Slice
%
figure;
orient landscape;
montage(dat3d(:,:,:),'DisplayRange',[0 0.95*max(dat3d(:))]);
title({['T2' ds]; ['Slice ' sll]},'FontSize',20, ...
'FontWeight','bold');
pos = get(gca,'pos');
pos(2) = 0.02;
set(gca,'pos',pos);
if k==1
print('-dpsc2','-r600','-fillpage',pnam);
else
print('-dpsc2','-r600','-fillpage','-append',pnam);
end
%
% Calculate T2* Values for the Whole Slice
%
T2starnls(:,:,k) = T1r3d_calc(dat3d,etn,t0);
%
% Plot T2* Values for this Slice
%
figure;
orient landscape;
%
img = T2starnls(:,:,k);
img(img<0) = 0;
img(img>65) = 65;
imagesc(img,[0 65]);
%
colormap jet;
axis image;
axis off;
%
colorbar;
%
title({['T2*' ds]; ['Slice ' sll]},'FontSize',16, ...
'FontWeight','bold');
%
print('-dpsc2','-r600','-fillpage','-append',pnam);
%
end
%
% Incorrect Number of Directories
%
else
error(' *** dicom_chk: Incorrect number of directories!');
end
%
return