-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_2dash_12.m
264 lines (247 loc) · 8.36 KB
/
Code_2dash_12.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
tic;
% Read in a demo image.
baseFileName="2'-12.dcm";
grayImage = dicomread(baseFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 2); % Take green channel.
end
%%
im = J;
im = double(im);
im = mean(im,3);
im2=im;
sz = min(size(im));
% im = im(1:sz,1:sz);
% newsz = 3^floor(log(sz)/log(3));
im = imresize(im,[243,243]);
%
disp('Performing BWT decomposition...');
imbwt = bwt(im);
disp('Reconstructing from BWT coefficients...');
imr = ibwt(imbwt);
d = imr-im;
%figure
%subplot(2,2,1);
%imagesc(im);title('Original');
%subplot(2,2,2);
%imagesc(imbwt);%title('BWT decomposition');
%subplot(2,2,3);
%map=jet;
%colormap(map);
%set(gca,'XTick',[], 'YTick', [])
%imagesc(imr);%title('Reconstruction');
%subplot(2,2,4);
%plot(d(:));
%hold on;
%plot(eps*abs(im(:)),'r');
%plot(-eps*abs(im(:)),'r');
%hold off;
%title('Reconstruction error');
figure
imagesc(imr)
map=jet;
colormap(map);
set(gca,'XTick',[], 'YTick', [])
%
rgbImage = imread("1-16_IMR.png");
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
%rgbImage = imresize(rgbImage,[243,243]);
% Display the original color image.
figure,
subplot(1, 2, 1);
imshow(rgbImage);
%caption = sprintf('Original Color Image : %s', baseFileName);
title('BWT Feature Map', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
drawnow;
disp('Decomposing BWT coefficients into R G B Channels...');
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the red channel image.
fontSize = 14;
subplot(2, 6, 4);
imshow(redChannel);
title('Red Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the green channel image.
subplot(2, 6, 5);
imshow(greenChannel);
title('Green Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the blue channel image.
subplot(2, 6, 6);
imshow(blueChannel);
title('Blue Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
%
thresholdValue = 150;
%imadjust(skullFreeImage);
%binaryImage = skullFreeImage > thresholdValue;
binaryImage = redChannel > thresholdValue;
%binaryImage=~binaryImage;
%binaryImage = bwareafilt(binaryImage, 1);
seD = strel('disk',2);
%binaryImage = imerode(binaryImage,seD);
binaryImage = imdilate(binaryImage, true(3));
binaryImage = bwareafilt(binaryImage, 1);
%binaryImage = imfill(binaryImage);
% Display the image.
subplot(2, 6, 10);
imshow(binaryImage, []);
%
%binaryImage = imerode(binaryImage,seD);
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = bwareafilt(binaryImage, 1);
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(3));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = bwareafilt(binaryImage, 1);
%binaryImage = imerode(binaryImage,seD);
%binaryImage = imerode(binaryImage,seD);
%binaryImage = imerode(binaryImage,seD);
%binaryImage = imerode(binaryImage,seD);
%labeledImage = bwlabel(binaryImage);
%binaryImage = ismember(labeledImage, 2);
%binaryImage = imfill(binaryImage, 'holes');
%binaryImage = imfill(binaryImage);
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
subplot(2, 6, 11);
imshow(binaryImage, []);
caption = sprintf('Enhanced\n Skull Region');
title(caption, 'FontSize', fontSize, 'Color', 'k');
%
[r,c]= size(redChannel);
im2= imresize(im2,[r,c]);
outputImage1 = im1; % Initialize
outputImage1(binaryImage) = [0]; % Mask out.
outputImage = redChannel; % Initialize
outputImage(binaryImage) = [0]; % Mask out.
subplot(2, 6, 12);
imshow(outputImage1, []);
caption = sprintf('Estimated\n Brain Region');
title(caption, 'FontSize', fontSize, 'Color', 'k');
%showMaskAsOverlay(0.8, edges==1, 'g')
%caption = sprintf('Skull-Stripped Image');
%title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%
thresholdValue = 65;
%imadjust(skullFreeImage);
%binaryImage = skullFreeImage > thresholdValue;
binaryImage = outputImage > thresholdValue;
binaryImage1=binaryImage;
figure,
subplot(2,4,1)
imshow(binaryImage, []);
title('Segmented Region', 'FontSize', fontSize, 'Interpreter', 'None');
%
%binaryImage3 = bwareafilt(binaryImage, 3);
%binaryImage = bwareafilt(binaryImage, 1);
%binaryImage = binaryImage2-binaryImage1;
seD = strel('disk',1);
%binaryImage = imerode(binaryImage,seD);
%binaryImage = bwareafilt(binaryImage, 1);
%binaryImage = imdilate(binaryImage, true(3));
%binaryImage = imdilate(binaryImage, true(3));
binaryImage = imfill(binaryImage,'holes');
%binaryImage = bwareafilt(binaryImage, 1);
binaryImage = imerode(binaryImage,seD);
binaryImage = bwareafilt(binaryImage, 1);
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
binaryImage = imdilate(binaryImage, true(2));
% Display the image.
subplot(2,4,2)
imshow(binaryImage, []);
title('Segmented Tumor Region', 'FontSize', fontSize, 'Interpreter', 'None');
%
disp('Time Elapsed for Tumor Sgmentation from Brain MRI...');
toc
p = double(binaryImage);
p1=p;
img = dicomread("1-16_Mask.dcm");
q=double(imresize(img,[r,c]));
q1=q;
iou = jaccard(p,q);
iou
subplot(2,4,3)
imshow(q, []);
title('Ground Truth Mask', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2,4,4);
im1= imresize(im,[r,c]);
imshow(im1, []);
caption = sprintf('Tumor\nOutlined in red in the overlay');
title(caption, 'FontSize', fontSize, 'Color', 'k');
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
hold on;
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
% Note: since array is row, column not x,y to get the x you need to use the second column of thisBoundary.
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 1);
end
boundaries1 = bwboundaries(q);
numberOfBoundaries1 = size(boundaries1, 1);
for k = 1 : numberOfBoundaries1
thisBoundary = boundaries1{k};
% Note: since array is row, column not x,y to get the x you need to use the second column of thisBoundary.
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 1);
end
hold off;
%
p = p(:);
q = q(:);
subplot(2,4,6:7)
cm = confusionchart(p,q);
TP= cm.NormalizedValues(4);
TN= cm.NormalizedValues(1);
FP= cm.NormalizedValues(2);
FN= cm.NormalizedValues(3);
sensitivity = TP / (TP + FN)
specificity = TN / (FP + TN)
Dice_Coefficient = 2*TP /(2*TP + FP + FN)
accuracy = (TP+TN)/(TP+TN+FP+FN)
%%
figure
%subplot(2,4,4);
im1= imresize(im,[r,c]);
imshow(im1, []);
caption = sprintf('Tumor\nOutlined in red in the overlay');
title(caption, 'FontSize', fontSize, 'Color', 'k');
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
hold on;
%boundaries = bwboundaries(binaryImage);
%numberOfBoundaries = size(boundaries, 1);
%for k = 1 : numberOfBoundaries
% % Note: since array is row, column not x,y to get the x you need to use the second column of thisBoundary.
% plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 1);
%end
showMaskAsOverlay(0.5,q1,'b');
showMaskAsOverlay(0.2,p1,'r');
hold off;
%showMaskAsOverlay(1,infection,'r');