-
Notifications
You must be signed in to change notification settings - Fork 15
/
main_dicom_to_blocks.m
228 lines (177 loc) · 6.78 KB
/
main_dicom_to_blocks.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
%% main function
clc;
clear;
%% toolbox
addpath(genpath([pwd '/toolbox']))
%% module path values
util_path=[pwd '/util'];
input_path=[pwd '/io'];
%% module addpath
addpath(genpath(util_path));
addpath(genpath(input_path));
%% set global values
global path_nodule;
global path_data;
path_nodule = [pwd '/DATA']; %pwd : returns the current directory
path_data = [pwd '/DATA/LIDC-IDRI/']; %dcm files directory
%% set values
iso_px_size=1; % a standard unit ('mm-unit')
l_offset = [0, 0, 0;
1, 0, 0;
-1, 0, 0;
0, 1, 0;
0, -1, 0;
0, 0, 1;
0, 0, -1;
1, 1, 0;
-1, 1, 0;
-1, -1, 0;
1, -1, 0;
1, 0, 1;
-1, 0, 1;
-1, 0, -1;
1, 0, -1;
0, 1, 1;
0, -1, 1;
0, -1, -1;
0, 1, -1;
1, 1, 1
-1, 1, 1
-1, -1, 1
-1, 1, -1
-1, -1, -1
1, -1, 1
1, -1, -1
1, 1, -1];
%% directory paths
nrrd_img_path=[path_nodule '/nodule_radiomics/'];
%% make directory
if ~isdir(nrrd_img_path); mkdir(nrrd_img_path); end
%% saved data load or not
load_input = true;
%% get pids
filename_pid_list = [path_nodule '/dicom_pid_list.mat'];
if(fn_check_load_data(filename_pid_list, load_input))
[dicom_path_list,pid_list]=fn_scan_pid(path_data);
save(filename_pid_list, 'dicom_path_list', 'pid_list');
else
load(filename_pid_list);
end
is_pass = 0;
%% main process
%for idx = randperm(numel(pid_list))
for idx = 1:numel(pid_list)
pid = pid_list{idx};
% if strfind(pid, '0405') > 0
% is_pass = 0;
% else
% is_pass = 1;
% end
if is_pass > 0 || sum(strfind(pid,'.')) > 0
continue
end
tic % tic starts a stopwatch timer
fprintf('%d %s\n', idx, pid);
%% input part
dicom_path = dicom_path_list{idx};
[lung_img_3d, nodule_img_3d, dicom_tags, thick, pixelsize, nodule_info] = fn_dicom_read(dicom_path,pid);
if numel(nodule_info) == 0
continue
end
[nodule_img_3d, nodule_info] = fn_nodule_info_update(lung_img_3d,nodule_img_3d,nodule_info,thick,pixelsize);
%% nrrd
if ~isdir([nrrd_img_path '/' pid]); mkdir([nrrd_img_path '/' pid]); end
writetable([nodule_info(:,[1:3 5:6 end 10:12 15:17]) nodule_info.Characteristics], [nrrd_img_path pid '/' pid '.csv'])
meta = struct();
meta.type = 'int16';
meta.encoding = 'gzip';
meta.spaceorigin = dicom_tags{1}.ImagePositionPatient';
meta.spacedirections = [reshape(dicom_tags{1}.ImageOrientationPatient,3,2),[0;0;1]]*diag([pixelsize; thick]);
meta.endian = 'little';
fn_nrrdwrite([nrrd_img_path '/' pid '/' pid '_CT.nrrd'], int16(lung_img_3d(:,:,end:-1:1)), meta);
meta.type = 'uint8';
fn_nrrdwrite([nrrd_img_path '/' pid '/' pid '_CT_all-label.nrrd'], uint8(nodule_img_3d(:,:,end:-1:1)>0), meta);
% for sid = 1:4
% str_sid = num2str(sid);
% sid_nodule_image_3d = uint8(bitand(uint8(nodule_img_3d),2^(sid-1))>0);
% sid_nodules = strcmp(nodule_info.sid, str_sid);
% if sum(sid_nodules) > 0
% fn_nrrdwrite([nrrd_img_path '/' pid '/' pid '_CT_Phy' str_sid '-label.nrrd'], sid_nodule_image_3d(:,:,end:-1:1), meta)
% end
% end
%% nrrd 1mm
[interpol_lung_img_3d,interpol_nodule_img_3d]=fn_interpol3d(lung_img_3d,nodule_img_3d,thick,pixelsize,iso_px_size);
meta.type = 'int16';
meta.spacedirections = [reshape(dicom_tags{1}.ImageOrientationPatient,3,2),[0;0;1]];
fn_nrrdwrite([nrrd_img_path '/' pid '/' pid '_CT-1mm.nrrd'], int16(interpol_lung_img_3d(:,:,end:-1:1)), meta);
meta.type = 'uint8';
fn_nrrdwrite([nrrd_img_path '/' pid '/' pid '_CT_all-1mm-label.nrrd'], uint8(interpol_nodule_img_3d(:,:,end:-1:1)>0), meta);
fprintf('dicom images and annotations converted ... \t\t\t %6.2f sec\n', toc);
%% blocks
lung_img_3d = interpol_lung_img_3d;
nodule_img_3d = interpol_nodule_img_3d;
meta_lung_img_3d = meta;
sz_img_3d = size(lung_img_3d);
lung_img_3d(lung_img_3d<-1000) = -1000;
lung_img_3d(lung_img_3d>400) = 400;
lung_img_3d = (double(lung_img_3d)+1000)/1400;
meta.type = 'float';
meta.encoding = 'gzip';
meta.endian = 'little';
if ~isdir(['output/' pid]); mkdir(['output/' pid]); end
non_nodule_count = 0;
for nid = 1:size(nodule_info,1)
o_volume = table2array(nodule_info(nid,5));
o_centroid = round(table2array(nodule_info(nid,8)));
if sum(isnan(o_centroid)) > 0; continue; end
% nodules
for oid = 1:size(l_offset,1)
offset = l_offset(oid,:);
centroid = o_centroid + offset;% one voxel offset
bbox = [centroid-16+1,centroid+16];
if sum(bbox(1:3) < 1) > 0 || sum(bbox(4:6) > sz_img_3d) > 0
continue
end
lung_blk_3d = lung_img_3d(bbox(2):bbox(5),bbox(1):bbox(4),bbox(3):bbox(6));
nodule_blk_3d = nodule_img_3d(bbox(2):bbox(5),bbox(1):bbox(4),bbox(3):bbox(6));
if sum(nodule_blk_3d(:))/o_volume < 0.5
continue
end
%display(mean(lung_blk_3d(:)))
meta.spaceorigin = meta_lung_img_3d.spaceorigin+bbox(1:3)-1;
fn_nrrdwrite(['output/' pid '/' pid sprintf('_%03d_%03d_%03d',centroid([3 1 2])) '_32_N_' num2str(mean(lung_blk_3d(:)),2) '_' num2str(oid-1, '%02d') '.nrrd'], double(lung_blk_3d), meta);
% if oid == 1
% for rot = 1:3
% fn_nrrdwrite(['output/' pid '/' pid sprintf('_%03d_%03d_%03d',centroid([3 1 2])) '_32_N_' num2str(mean(lung_blk_3d(:)),2) '_r' num2str(rot*90, '%03d') '.nrrd'], double(rot90(lung_blk_3d,rot)), meta);
% end
% end
end
% non nodules
for a = randn(3,round(rand()*500))
offset = round(a'*32);
centroid = o_centroid + offset;% background sample
bbox = [centroid-16+1,centroid+16];
if sum(bbox(1:3) < 1) > 0 || sum(bbox(4:6) > sz_img_3d) > 0
continue
end
lung_blk_3d = lung_img_3d(bbox(2):bbox(5),bbox(1):bbox(4),bbox(3):bbox(6));
nodule_blk_3d = nodule_img_3d(bbox(2):bbox(5),bbox(1):bbox(4),bbox(3):bbox(6));
if mean(nodule_blk_3d(:)) == 0 && std(lung_blk_3d(:)) ~= 0
%display(mean(lung_blk_3d(:)))
meta.spaceorigin = meta_lung_img_3d.spaceorigin+bbox(1:3)-1;
fn_nrrdwrite(['output/' pid '/' pid sprintf('_%03d_%03d_%03d',centroid([3 1 2])) '_32_B_' num2str(mean(lung_blk_3d(:)),2) '.nrrd'], double(lung_blk_3d), meta);
non_nodule_count = non_nodule_count + 1;
end
if non_nodule_count > 1000
break
end
end
end
fprintf('block images saved ... \t\t\t %6.2f sec\n', toc);
fclose all;
end
% module rmpath
% rmpath('./io');
% rmpath('./interpolation');
% rmpath('./segmentation');
% rmpath('./nodule_candidate_detection');