|
a |
|
b/Image features calculation code/Not Working/nodules.m |
|
|
1 |
function extractnodules( ) |
|
|
2 |
%EXTRACTNODULES Extract nodule images from lung CT scans |
|
|
3 |
% |
|
|
4 |
% Ekarin Varutbangkul |
|
|
5 |
% July 8, 2006 |
|
|
6 |
% |
|
|
7 |
% Reads nodule data from 'DCMInfo2.txt' in working directory and pulls |
|
|
8 |
% individual nodule images from DICOM images referenced in the data |
|
|
9 |
% file, writing them to new DICOM images in the 'nodules/' directory. |
|
|
10 |
% Also saves all Haralick and annotation data to 'haralick.txt' in the |
|
|
11 |
% same directory. |
|
|
12 |
|
|
|
13 |
tic |
|
|
14 |
|
|
|
15 |
fid = fopen('contours.txt', 'rt'); |
|
|
16 |
% read line from data file |
|
|
17 |
tline = fgetl(fid); |
|
|
18 |
% extract nodule information |
|
|
19 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
20 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
21 |
%cn = 1; |
|
|
22 |
%while cn<4 |
|
|
23 |
while feof(fid) == 0 |
|
|
24 |
|
|
|
25 |
% k1 = n_file; |
|
|
26 |
% k2 = n_id; |
|
|
27 |
k1 = pk; |
|
|
28 |
s_minx = n_minx; |
|
|
29 |
s_maxx = n_maxx; |
|
|
30 |
s_miny = n_miny; |
|
|
31 |
s_maxy = n_maxy; |
|
|
32 |
fn = sprintf('%s', char(n_filename)); |
|
|
33 |
fn2 = sprintf('%d.dcm', pk); |
|
|
34 |
fprintf(1,' Processing: %d (x=[%d,%d] y=[%d,%d]) ...\n', pk, n_minx, n_maxx, n_miny, n_maxy); |
|
|
35 |
|
|
|
36 |
% make sure file exists |
|
|
37 |
if (exist(fn,'file')) |
|
|
38 |
|
|
|
39 |
% read image |
|
|
40 |
info = dicominfo(fn); |
|
|
41 |
I = dicomread(info); |
|
|
42 |
|
|
|
43 |
% get nodule range |
|
|
44 |
n_rows = n_maxy - n_miny + 1; |
|
|
45 |
n_cols = n_maxx - n_minx + 1; |
|
|
46 |
|
|
|
47 |
% minimum nodule size is 5x5 pixels |
|
|
48 |
if (n_rows > 4 && n_cols > 4) |
|
|
49 |
|
|
|
50 |
% extract nodule data |
|
|
51 |
I2 = int16(zeros(n_rows,n_cols)); |
|
|
52 |
I3 = int16(zeros(n_rows,n_cols)); |
|
|
53 |
I4 = int16(zeros(n_rows,n_cols)); |
|
|
54 |
for r = 1:n_rows |
|
|
55 |
for c = 1:n_cols |
|
|
56 |
I2(r,c) = I(n_miny+(r-1),n_minx+(c-1)); |
|
|
57 |
end |
|
|
58 |
end |
|
|
59 |
bg = -2000; |
|
|
60 |
k_minx = n_minx; |
|
|
61 |
k_miny = n_miny; |
|
|
62 |
while (k1 == pk) & (feof(fid) == 0) |
|
|
63 |
I3((n_y-n_miny)+1,(n_x-n_minx)+1)= 1; |
|
|
64 |
if feof(fid) == 0 |
|
|
65 |
tline = fgetl(fid); |
|
|
66 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
67 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
68 |
end |
|
|
69 |
end |
|
|
70 |
BW = zeros(n_rows,n_cols); |
|
|
71 |
BW = imfill(I3,'holes'); |
|
|
72 |
I3 = BW; |
|
|
73 |
I3(I3 == 0) = bg; |
|
|
74 |
I4 = BW; |
|
|
75 |
% old segmentation code |
|
|
76 |
%{ |
|
|
77 |
for r = 1:n_rows |
|
|
78 |
for c = 1:n_cols |
|
|
79 |
if (I3(r,c) ~= 1) |
|
|
80 |
I3(r,c) = bg; |
|
|
81 |
else |
|
|
82 |
break; |
|
|
83 |
end |
|
|
84 |
end |
|
|
85 |
end |
|
|
86 |
for r = 1:n_rows |
|
|
87 |
for c = n_cols:-1:1 |
|
|
88 |
if (I3(r,c) ~= 1) |
|
|
89 |
I3(r,c) = bg; |
|
|
90 |
else |
|
|
91 |
break; |
|
|
92 |
end |
|
|
93 |
end |
|
|
94 |
end |
|
|
95 |
for c = 1:n_cols |
|
|
96 |
for r = 1:n_rows |
|
|
97 |
if (I3(r,c) ~= 1) |
|
|
98 |
I3(r,c) = bg; |
|
|
99 |
else |
|
|
100 |
break; |
|
|
101 |
end |
|
|
102 |
end |
|
|
103 |
end |
|
|
104 |
for c = 1:n_cols |
|
|
105 |
for r = n_rows:-1:1 |
|
|
106 |
if (I3(r,c) ~= 1) |
|
|
107 |
I3(r,c) = bg; |
|
|
108 |
else |
|
|
109 |
break; |
|
|
110 |
end |
|
|
111 |
end |
|
|
112 |
end |
|
|
113 |
%} |
|
|
114 |
for r = 1:n_rows |
|
|
115 |
for c = 1:n_cols |
|
|
116 |
if I3(r,c)~= bg |
|
|
117 |
I3(r,c) = I(k_miny+(r-1),k_minx+(c-1)); |
|
|
118 |
end |
|
|
119 |
end |
|
|
120 |
end |
|
|
121 |
for r = 1:n_rows |
|
|
122 |
for c = 1:n_cols |
|
|
123 |
if I4(r,c)== 0 |
|
|
124 |
I4(r,c) = I(k_miny+(r-1),k_minx+(c-1)); |
|
|
125 |
else |
|
|
126 |
I4(r,c) = bg; |
|
|
127 |
end |
|
|
128 |
end |
|
|
129 |
end |
|
|
130 |
|
|
|
131 |
% save nodule image |
|
|
132 |
info.Width = n_cols; |
|
|
133 |
info.Height = n_rows; |
|
|
134 |
info.Rows = n_rows; |
|
|
135 |
info.Columns = n_cols; |
|
|
136 |
dicomwrite(I2, strcat('crop/',fn2),info); |
|
|
137 |
dicomwrite(I3, strcat('segmented_nodules/',fn2),info); |
|
|
138 |
dicomwrite(I4, strcat('segmented_background/',fn2),info); |
|
|
139 |
|
|
|
140 |
% fprintf(1,' Saving data ...\n'); |
|
|
141 |
else |
|
|
142 |
% fprintf(1,'%s is too small (less than 5x5) -- skipping ...\n',fn2) |
|
|
143 |
epk = pk; |
|
|
144 |
while (feof(fid) == 0) & (epk == pk) |
|
|
145 |
tline = fgetl(fid); |
|
|
146 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
147 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
148 |
end |
|
|
149 |
end |
|
|
150 |
else |
|
|
151 |
fprintf(1,' File does not exist: %s -- skipping ...\n', fn) |
|
|
152 |
while (feof(fid) == 0) & (epk == pk) |
|
|
153 |
tline = fgetl(fid); |
|
|
154 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
155 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
156 |
end |
|
|
157 |
end |
|
|
158 |
% cn = cn+1; |
|
|
159 |
end |
|
|
160 |
fclose(fid); |
|
|
161 |
toc |