Diff of /Leukemia_MAin.m [000000] .. [acaf93]

Switch to unified view

a b/Leukemia_MAin.m
1
2
clc;
3
clear all;
4
close all;
5
[fna,pna]=uigetfile({'*.jpg;*.bmp;*.png'},'Select file');
6
fanme=[pna fna];
7
im = double(imread(fanme));
8
im=imresize(im,[200 300]);
9
figure;imshow(uint8(im));title('Input image');
10
tic
11
%  Lab conversion and color thresholding
12
im=im/255;
13
R=im(:,:,1);
14
G=im(:,:,2);
15
B=im(:,:,3);
16
% Set a threshold
17
T = 0.008856;
18
19
[M, N,plane] = size(R);
20
s = M * N;
21
RGB = [reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];
22
23
% RGB to XYZ
24
MAT = [0.412453 0.357580 0.180423;
25
       0.212671 0.715160 0.072169;
26
       0.019334 0.119193 0.950227];
27
XYZ = MAT * RGB;
28
29
% Normalize for D65 white point
30
X = XYZ(1,:) / 0.950456;
31
Y = XYZ(2,:);
32
Z = XYZ(3,:) / 1.088754; 
33
34
XT = X > T;
35
YT = Y > T;
36
ZT = Z > T;
37
38
Y3 = Y.^(1/3); 
39
40
fX = XT .* X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);
41
fY = YT .* Y3 + (~YT) .* (7.787 .* Y + 16/116);
42
fZ = ZT .* Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);
43
44
L = reshape(YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 * Y), M, N);
45
a = reshape(500 * (fX - fY), M, N);
46
b = reshape(200 * (fY - fZ), M, N);
47
48
  L = cat(3,L,a,b);
49
figure;imshow(L);title('L*a*b converted image'); 
50
51
% color thresholding
52
53
      out2 = colorthreshold(lab2uint8(L),'otsu');
54
      figure, imshow(uint8(out2));
55
      title('Color Thresholded image');
56
     OUCT=uint8(out2); 
57
     ReLAb=zeros(size(L));
58
     ReRGB=zeros(size(L));
59
Th=100;
60
for i=1:M
61
    for j=1:N
62
        if OUCT(i,j,1)<200 && OUCT(i,j,1)>90
63
            ReLAb(i,j,:)=L(i,j,:);
64
            ReRGB(i,j,:)=im(i,j,:);
65
        end
66
    end
67
end
68
69
   figure, imshow(ReLAb);title('L*a*b Mask image');
70
   figure, imshow(ReRGB);title('RGB Mask image');
71
   
72
   
73
74
%----RGB 2 Gray conversion
75
76
Grayim=rgb2gray(ReRGB);
77
figure, imshow(Grayim);title('Gray Mask image');
78
  
79
80
L=imadjust(Grayim);
81
H = histeq(Grayim);
82
%% Brighten most of the details in the image except the nucleus
83
R1=imadd(L,H);
84
%% Highlight all the objects and its borders in the image including the cell nucleus
85
R2 = imsubtract(R1,H);
86
%% Remove almost all the other blood components while retaining the nucleus with minimum affect of distortion on the nucleus part of the white blood cell.
87
% R3=imadd(R1,R2);
88
figure;
89
subplot(221);
90
imshow(Grayim);title('Gray image')
91
subplot(222);
92
imshow(H);title('Enhanced image')
93
subplot(223);
94
imshow(R1);title('Image except the nucleus')
95
subplot(224);
96
imshow(R2);title('Highlighted all objects')
97
toc
98
%check histogram
99
figure;
100
hi=imhist(R2);
101
hi1=hi(1:2:256);
102
horz=1:2:256;
103
bar(horz,hi1);
104
axis([0 255 0 1400]);
105
set(gca, 'xtick', 0:50:255);
106
set(gca, 'ytick', 0:2000:15000);
107
 xlabel('Gray level' );
108
ylabel('No of pixels' );
109
title('Histogram before opening the image');
110
111
%=====================
112
113
% Otsu thresholding   
114
   rk=otsu(R2);
115
  imthO=(Grayim>rk); 
116
figure, imshow(imthO);title('Otsu Binary Image');
117
  
118
%implements a 3-by-3 average filter
119
R3 = average_filter(imthO);
120
R31=double(im2bw(R3));
121
figure; imshow(R31);title('Average Filtered image');
122
123
hy = fspecial('sobel');
124
hx = hy';
125
Iy = imfilter(double(R3), hy, 'replicate');
126
Ix = imfilter(double(R3), hx, 'replicate');
127
gradmag = sqrt(Ix.^2 + Iy.^2);
128
figure
129
imshow(gradmag,[]), title('Gradient magnitude')
130
% Finally we are ready to compute the watershed-based segmentation.
131
L = watershed(gradmag);
132
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
133
figure
134
imshow(Lrgb)
135
title('Watershed segmentation')
136
%  Mark the Foreground Objects
137
se = strel('disk', 2);
138
Io = imopen(Grayim, se);
139
figure
140
imshow(Io), title('Opening (Io)')
141
142
Ie = imerode(Grayim, se);
143
Iobr = imreconstruct(Ie, Grayim);
144
figure
145
imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
146
147
Ioc = imclose(Io, se);
148
figure
149
imshow(Ioc), title('Opening-closing (Ioc)')
150
151
Iobrd = imdilate(Iobr, se);
152
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
153
Iobrcbr = imcomplement(Iobrcbr);
154
figure
155
imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
156
157
fgm = Iobrcbr>0.1;
158
figure
159
imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)')
160
161
I2 = Grayim;
162
I2(fgm) = 255;
163
figure
164
imshow(I2), title('Regional maxima superimposed on original image (I2)')
165
166
167
% se2 = strel('disk', 1);
168
% % fgm2 = imclose(fgm, se2);
169
% fgm = imerode(fgm, se2);
170
% This procedure tends to leave some stray isolated pixels that must be removed. You can do this using bwareaopen, which removes all blobs that have fewer than a certain number of pixels.
171
% getin=inputdlg('Enter the Area opening dimension(small objects-100 & Large objects-250)');
172
% aREAIN=str2double(getin{1,1});
173
aREAIN=50;
174
fgm4 = bwareaopen(fgm,aREAIN);
175
I3 = Grayim;
176
I3(fgm4) = 255;
177
figure
178
imshow(I3)
179
title('Modified regional maxima superimposed on original image (fgm4)')
180
figure
181
imshow(fgm4)
182
title('After Eliminating Platelets')
183
% 
184
% Compute Background Markers
185
% Now you need to mark the background. In the cleaned-up image, Iobrcbr, the dark pixels belong to the background, so you could start with a thresholding operation.
186
187
188
D = bwdist(fgm4);
189
DL = watershed(D);
190
bgm = DL == 0;
191
figure
192
imshow(bgm), title('Broken Watershed Nucleus')
193
%% eliminate border touching segments
194
hy = fspecial('sobel');
195
hx = hy';
196
Iy = imfilter(double(fgm4), hy, 'replicate');
197
Ix = imfilter(double(fgm4), hx, 'replicate');
198
gradmag = sqrt(Ix.^2 + Iy.^2);
199
figure
200
imshow(gradmag,[]), title('Gradient magnitude')
201
% Finally we are ready to compute the watershed-based segmentation.
202
L = watershed(gradmag);
203
% Step 6: Visualize the Result
204
% One visualization technique is to superimpose the foreground markers, background markers, and segmented object boundaries on the original image. You can use dilation as needed to make certain aspects, such as the object boundaries, more visible. Object boundaries are located where L == 0.
205
I4 = Grayim;
206
I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255;
207
figure
208
imshow(I4)
209
title('Markers and object boundaries superimposed on original image ')
210
211
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
212
figure
213
imshow(Lrgb)
214
title('Colored watershed label matrix (Lrgb)')
215
216
%% Final 
217
figure;
218
imshow(im);
219
hold on
220
boundaries = bwboundaries(fgm4);    
221
numberOfBoundaries = size(boundaries);
222
for k = 1 : numberOfBoundaries
223
    thisBoundary = boundaries{k};
224
    plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 1.5);
225
end
226
hold off;
227
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
a=imthO;
229
aa=imresize(a,[256 256]);
230
u_bw_filename = aa;
231
232
b=im;
233
 bb=imresize(b,[256 256]);
234
u_GT_filename = im2bw(bb);
235
236
u_GT = [((u_GT_filename)) > 0 ];
237
u_bw = [((u_bw_filename)) > 0 ];
238
239
temp_obj_eval = objective_evaluation_core(u_bw, u_GT);
240
241
disp('PSNR value --');
242
disp(temp_obj_eval.PSNR);