Switch to unified view

a b/Image features calculation code/Not Working/pmaps.m
1
%M = csvread('C:\Documents and Settings\dzinovev\Desktop\parsed_xml.csv',1,1);
2
%data = textread('\\ailab03\NEW_LIDC\LIDC\dataset.csv', '', 'delimiter', ',');
3
%fid = fopen('\\ailab03\NEW_LIDC\LIDC\dataset.csv');
4
%C = textscan(fid, '%s /n', 'delimiter', ',', 'treatAsEmpty', {'NA', 'na'}, 'commentStyle', '//');
5
%M = dlmread('\\ailab03\NEW_LIDC\LIDC\dataset.csv', ',');
6
7
A=fopen('\\ailab03\NEW_LIDC\LIDC\dataset.csv');
8
fid = fopen ('\\ailab03\NEW_LIDC\LIDC\dataset_big.csv', 'wt');
9
fid1 = fopen ('\\ailab03\NEW_LIDC\LIDC\missing.csv', 'wt');
10
fid2 = fopen ('\\ailab03\NEW_LIDC\LIDC\pmaps.csv', 'wt');
11
tline = fgetl(A);
12
current = 0;
13
missing = 0;
14
for r = 1:14927
15
    tline = fgetl(A);
16
    B=textscan(A,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s /n', 'delimiter',',');
17
    name = strcat(B{6},B{18},'.dcm');        
18
    filename = strcat(B{18});    
19
    noduleid = strcat(B{5});
20
    pts = strcat(B{19});
21
    lobul = B{12};
22
    malig = B{15};
23
    margin = B{11};
24
    spher = B{10};
25
    spicul = B{13};
26
    subtl = B{7};
27
    textur = B{14};
28
    data(r,1)=name;
29
    data(r,2)=filename;
30
    data(r,3)=noduleid;
31
    data(r,4)=pts;
32
    data(r,5) = lobul;
33
    data(r,6) = malig;
34
    data(r,7) = margin;
35
    data(r,8) = spher;
36
    data(r,9) = spicul;
37
    data(r,10) = subtl;
38
    data(r,11) = textur;
39
end
40
check = 1;%need two counters: 1 for going through the list, another for naming purposes
41
%name = 1;
42
while (check<14927)
43
    if (exist(char(data(check,1)),'file'))
44
        I = dicomread(data{check,1});
45
        m = data{check,4};
46
        points = strread(m,'%s','delimiter','*|'); 
47
        [k,l]=size(points);
48
        for pts=1:k
49
            point = strread(points{pts,1},'%d','delimiter','*;');
50
            xcoord(pts)=point(1);
51
            ycoord(pts)=point(2);
52
            %I(point(2),point(1))=2215;
53
        end;   
54
        ymax = max(xcoord);    
55
        ymin = min(xcoord);    
56
        xmax = max(ycoord);    
57
        xmin = min(ycoord);
58
        width = xmax-xmin;
59
        heigth = ymax-ymin;
60
        if(width > 4 && heigth > 4)
61
            current=current+1;
62
            %cropped = I(xmin:xmax,ymin:ymax);
63
            BW=zeros(size(I));
64
            %fprintf(fid,'%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s \n',num2str(current),char(B{1,2}),char(B{1,3}),char(B{1,4}),char(B{1,5}),char(B{1,6}),char(B{1,7}),char(B{1,8}),char(B{1,9}),char(B{1,10}),char(B{1,11}),char(B{1,12}),char(B{1,13}),char(B{1,14}),char(B{1,15}),char(B{1,16}),char(B{1,17}),char(B{1,18}));
65
            for pts=1:k
66
                point = strread(points{pts,1},'%d','delimiter','*;');
67
                BW(point(2),point(1))=1;
68
            end; 
69
            pmap = imfill(BW,'holes');
70
            cntr = 1;
71
            lob(cntr)=data(check,5);
72
            mal(cntr)=data(check,6);
73
            mar(cntr)=data(check,7);
74
            sph(cntr)=data(check,8);
75
            spi(cntr)=data(check,9);
76
            sub(cntr)=data(check,10);
77
            tex(cntr)=data(check,11);
78
            for n=1:14927
79
                if(n~=check)
80
                    b=1;
81
                    if((strcmp(data{check,1},data{n,1})==1)&&(strcmp(data{check,3},data{n,3})==1))
82
                        cntr = cntr+1;
83
                        lob(cntr)=data(n,5);
84
                        mal(cntr)=data(n,6);
85
                        mar(cntr)=data(n,7);
86
                        sph(cntr)=data(n,8);
87
                        spi(cntr)=data(n,9);
88
                        sub(cntr)=data(n,10);
89
                        tex(cntr)=data(n,11);
90
                        a='preved!';
91
                        m = data{n,4};
92
                        points = strread(m,'%s','delimiter','*|'); 
93
                        [k,l]=size(points);
94
                        BW=zeros(size(I));
95
                        for pts=1:k
96
                            point = strread(points{pts,1},'%d','delimiter','*;');
97
                            xcoord(pts)=point(1);
98
                            ycoord(pts)=point(2);
99
                            %I(point(2),point(1))=2215;
100
                            BW(point(2),point(1))=1;
101
                        end;   
102
                        BW = imfill(BW,'holes');
103
                        a
104
                        pmap=pmap+BW;
105
                        data(n)=cellstr('NaN');
106
                    end
107
                end
108
            end
109
            %check=check+1;
110
             for curthresh=1:4
111
                 binar = im2bw(pmap/10, (curthresh-1)/10);
112
                 if(sum(sum(binar))>0);
113
                     contur = bwmorph(binar,'remove');
114
                     [r,c]=find(pmap);
115
                     maxx = max(c);
116
                     minx = min(c);
117
                     maxy = max(r);
118
                     miny = min(r);
119
                     cropped = I(minx:maxx,miny:maxy);
120
                     BW = contur(minx:maxx,miny:maxy);
121
                %BW = imfill(BW,'holes');
122
                %imshow(BW,[0 1]);
123
                     [rows,cols]=size(cropped);
124
                     for m=1:rows
125
                         for n=1:cols
126
                             if(BW(m,n)==1)
127
                                 croppednod(m,n)=cropped(m,n);
128
                                 croppedback(m,n)=-2000;
129
                             else
130
                                 croppednod(m,n)=-2000;
131
                                 croppedback(m,n)=cropped(m,n);
132
                             end;
133
                         end;
134
                     end;
135
%                  figure;
136
%                  imshow(croppednod,[0 2215]);
137
%                  figure;
138
%                  imshow(croppedback,[0 2215]);
139
%                  figure;
140
%                  imshow(cropped,[0 2215]);
141
%                  figure;
142
%                  imshow(contur,[0 1]);
143
%                  figure;
144
%                  imshow(I);
145
                     fname = strcat(data(check,3),'_',data(check,2),'.dcm');
146
                     dicomwrite(int16(cropped), strcat('\\Ailab03\NEW_LIDC\LIDC\extract LIDC\pmap\',num2str(curthresh),'\crop\',fname{:}));        
147
                     dicomwrite(int16(croppednod), strcat('\\Ailab03\NEW_LIDC\LIDC\extract LIDC\pmap\',num2str(curthresh),'\segmented_nodules\',fname{:}));        
148
                     dicomwrite(int16(croppedback), strcat('\\Ailab03\NEW_LIDC\LIDC\extract LIDC\pmap\',num2str(curthresh),'\segmented_background\',fname{:}));            
149
                     dicomwrite(int16(BW), strcat('\\Ailab03\new_lidc\LIDC\extract LIDC\pmap\',num2str(curthresh),'\contours\',fname{:}));
150
                 end;
151
                % ceil(median(lob));
152
                 if(size(lob,2)>1)
153
                     fprintf(fid2,'%s,%s,%s,%s,%s,%s,%s,%s \n',fname{:},num2str(ceil(median(str2double(lob)))),num2str(ceil(median(str2double(mal)))),num2str(ceil(median(str2double(mar)))),num2str(ceil(median(str2double(sph)))),num2str(ceil(median(str2double(spi)))),num2str(ceil(median(str2double(sub)))),num2str(ceil(median(str2double(tex)))));
154
                 else
155
                     fprintf(fid2,'%s,%s,%s,%s,%s,%s,%s,%s \n',fname{:},str2double(lob{:}),str2double(mal{:}),str2double(mar{:}),str2double(sph{:}),str2double(spi{:}),str2double(sub{:}),str2double(tex{:}));
156
                 end                     
157
             end;
158
             %name=name+1;
159
        end;
160
        clear lob;
161
        clear mal;
162
        clear mar;
163
        clear sph;
164
        clear spi;
165
        clear sub;
166
        clear tex;
167
        clear xcoord;    
168
        clear ycoord;
169
        clear cropped;
170
        clear croppednod;
171
        clear croppedback;
172
        clear BW;
173
    else
174
        missing = missing + 1;
175
        fprintf(fid1,'%s,%s,%s \n',num2str(r+1),char(B{1,6}),char(B{1,18}));
176
    end;
177
    check=check+1;
178
    r
179
end;
180
missing
181
fclose(fid);
182
fclose(fid1);
183
fclose(fid2);
184
fclose(A);