Diff of /CODE [000000] .. [acaf93]

Switch to unified view

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