|
a |
|
b/Image features calculation code/Not Working/contours.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 |
|
|
|
52 |
I3 = int16(zeros(n_rows,n_cols)); |
|
|
53 |
k_minx = n_minx; |
|
|
54 |
k_miny = n_miny; |
|
|
55 |
while (k1 == pk) & (feof(fid) == 0) |
|
|
56 |
I3((n_y-n_miny)+1,(n_x-n_minx)+1)= 1; |
|
|
57 |
if feof(fid) == 0 |
|
|
58 |
tline = fgetl(fid); |
|
|
59 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
60 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
61 |
end |
|
|
62 |
end |
|
|
63 |
dicomwrite(I3, strcat('contours/',fn2),info); |
|
|
64 |
% fprintf(1,' Saving data ...\n'); |
|
|
65 |
else |
|
|
66 |
% fprintf(1,'%s is too small (less than 5x5) -- skipping ...\n',fn2) |
|
|
67 |
epk = pk; |
|
|
68 |
while (feof(fid) == 0) & (epk == pk) |
|
|
69 |
tline = fgetl(fid); |
|
|
70 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
71 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
72 |
end |
|
|
73 |
end |
|
|
74 |
else |
|
|
75 |
fprintf(1,' File does not exist: %s -- skipping ...\n', fn) |
|
|
76 |
while (feof(fid) == 0) & (epk == pk) |
|
|
77 |
tline = fgetl(fid); |
|
|
78 |
[pk n_filename n_minx n_maxx n_miny n_maxy n_x n_y ] ... |
|
|
79 |
= strread(tline, '%d %s %d %d %d %d %d %d'); |
|
|
80 |
end |
|
|
81 |
end |
|
|
82 |
% cn = cn+1; |
|
|
83 |
end |
|
|
84 |
fclose(fid); |
|
|
85 |
toc |