-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdicom_lst.m
184 lines (183 loc) · 6.67 KB
/
dicom_lst.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
%#######################################################################
%
% * DICOM LiST Program *
%
% M-File which reads a DICOMDIR file and collects information
% about the MRI series and DICOM files.
%
% NOTES: 1. Reads Philips MRI DICOMDIR files. May not work with
% other types of images.
%
% 2. Program also reads and collects information from the
% first DICOM file in each series.
%
% 3. A table of information is displayed to the screen,
% written to a MS-Excel spreadsheet and to a Matlab MAT
% file. The spreadsheet and MAT file are written in the
% same directory as the DICOMDIR file. The MAT file
% contains additional variables and all the file names in
% each series and patient position in MRI coordinates.
%
% 4. The program traps for some, but not all parameters in
% the DICOM file header.
%
% 17-Aug-2020 * Mack Gardner-Morse
%
%#######################################################################
%
% Get Directory with DICOM Images
%
[ddirfile,ddir] = uigetfile('*.*','Pick a DICOMDIR file.');
ddirfile = fullfile(ddir,ddirfile);
%
% Check for DICOMDIR File
%
if exist(ddirfile,'file')
%
% Get File Names and Acquired Pixel Spacing
%
d0 = images.dicom.parseDICOMDIR(ddirfile);
ns = size(d0.Patients.Studies.Series,2);
%
nimages = zeros(ns,1); % Number of DICOM files
arow = zeros(ns,1); % Acquired image row size
acol = zeros(ns,1); % Acquired image column size
apixel = zeros(ns,2); % Acquired pixel spacing
afile1 = cell(ns,1); % First DICOM file in series
afile2 = cell(ns,1); % Last DICOM file in series
afiles = cell(ns,1); % All DICOM file in series
im_type = cell(ns,1); % Image type in series
%
for k = 1:ns
nimages(k) = size(d0.Patients.Studies.Series(k).Images,2);
if nimages(k)>0
n = nimages(k);
arow(k) = d0.Patients.Studies.Series(k).Images(1).Payload.Rows;
acol(k) = d0.Patients.Studies.Series(k).Images(1).Payload. ...
Columns;
apixel(k,:) = d0.Patients.Studies.Series(k).Images(1). ...
Payload.PixelSpacing';
im_type{k} = d0.Patients.Studies.Series(k).Images(1). ...
Payload.ImageType;
afile1{k} = d0.Patients.Studies.Series(k).Images(1). ...
Payload.ReferencedFileID;
afile2{k} = d0.Patients.Studies.Series(k).Images(n). ...
Payload.ReferencedFileID;
afiles{k} = cell(n,1);
for l = 1:n
afiles{k}{l} = d0.Patients.Studies.Series(k).Images(l). ...
Payload.ReferencedFileID;
end
end
end
else
uiwait(msgbox('DICOMDIR file not found!','Information', ...
'warn','modal'));
end
%
% Get DICOM Information from Files
%
idx = nimages>0; % Actual series and not just space between series
nseries = sum(idx);
id = find(idx);
%
adur = zeros(nseries,2); % Acquisition duration (s)
adurs = cell(nseries,1); % Acquisition duration string (mm:ss)
psz = zeros(nseries,2); % Rows and Columns
isz = zeros(nseries,2); % Width and Height
sthk = zeros(nseries,1); % Slice Thickness
sspc = zeros(nseries,1); % Spacing Between Slices
pspc = zeros(nseries,2); % Pixel Spacing
ptxt = cell(nseries,1); % Protocol Name
stxt = cell(nseries,1); % Series Description
sl = zeros(nseries,1); % Rescale Slope
rinterc = zeros(nseries,1); % Rescale Intercept
splt = zeros(nseries,1); % Spin lock time (Trigger Time)
sn = zeros(nseries,1); % Series number
sdat = datetime(zeros(nseries,1),0,0,'Format','dd-MMM-yyyy HH:mm:ss');
%
for k = 1:nseries
%
l = id(k);
%
fnam = afiles{l}{1};
fnam = fullfile(ddir,fnam);
if exist(fnam,'file')
info = dicominfo(fnam);
%
if isfield(info,'AcquisitionDuration')
adur(k) = info.AcquisitionDuration;
adurs{k} = char(duration(seconds(adur(k)),'Format','mm:ss'));
else
adurs{k} = '00:00';
end
if isfield(info,'Rows')
psz(k,:) = [info.Rows info.Columns];
isz(k,:) = [info.Width info.Height];
end
if isfield(info,'SliceThickness')
sthk(k) = info.SliceThickness;
pspc(k,:) = info.PixelSpacing';
sspc(k) = info.SpacingBetweenSlices;
end
if isfield(info,'ImagePositionPatient')
pos(k,:) = info.ImagePositionPatient';
end
if isfield(info,'RescaleSlope')
sl(k) = info.RescaleSlope;
rinterc(k) = info.RescaleIntercept;
end
ptxt{k} = info.ProtocolName;
stxt{k} = info.SeriesDescription;
sn(k) = info.SeriesNumber;
if isfield(info,'TriggerTime')
splt(k) = info.TriggerTime;
end
dat = info.SeriesDate;
tim = info.SeriesTime;
sdat(k) = datetime([dat tim],'InputFormat','yyyyMMddHHmmss.SSSSS');
end
%
end
%
% Get Spin Lock Times from Series Description
%
idv = contains(stxt,'TSL_');
%
splcktc = cellstr(repmat('None',nseries,1)); % Cell array for all series
%
if any(idv)
splckt = extractBetween(stxt(idv),'TSL_','ms'); % Spin lock times as text
splckt = strrep(splckt,'_',',');
splcktc(idv) = splckt;
end
%
% Put Series Data into a Table and Get Column Names
%
colnams = {'Series#','SeriesDateTime','SeriesDescription', ...
'ProtocolName','ImageType','AcquisitionDuration', ...
'SpinLockTimes','#ofFiles','FirstFile','LastFile','Rows', ...
'Columns','PixelX','PixelY','SliceThickness', ...
'SpacingBetweenSlices','ImageRows','ImageColumns', ...
'Width','Height','ImagePixelX','ImagePixelY', ...
'RescaleSlope','RescaleIntercept'};
t0 = table(sn,sdat,string(stxt),string(ptxt),string(im_type(idx)), ...
char(adurs),string(splcktc),nimages(idx), ...
string(afile1(idx)),string(afile2(idx)),arow(idx), ...
acol(idx),round(apixel(idx,1),3),round(apixel(idx,2),3), ...
sthk,sspc,psz(:,1),psz(:,2),isz(:,1),isz(:,2), ...
round(pspc(:,1),3),round(pspc(:,2),3),sl,rinterc, ...
'VariableNames',colnams)
%
% Write Table to Spreadsheet
%
xlsnam = fullfile(ddir,'dicom_lst.xlsx');
writetable(t0,xlsnam);
%
% Save MAT File
%
clear d0 dat info k l n tim;
matnam = fullfile(ddir,'dicom_lst.mat');
save(matnam);
%
return