-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell_seg_40x_12032021.m
239 lines (166 loc) · 6.5 KB
/
cell_seg_40x_12032021.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------Cell segmentation implementing the watershed transform-------%
%------------------------------------------------------------------------%
% Load any image at 40x resolution un the script and select Red, green or
% blue channel to count the desired neurons.
%Some functions come from Digital Image Processing book by Gonzalez.
% Depending on the image some functions can be changed to optimize the
% requieriments for each tye of experiment ( tincion use....etc) that is:
% Median or gaussian filtering and image equalization
%Arturo Vsliño Pérez
% 09/01/2019
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
clear all
addpath('D:\Auto cell counting code\image_segementation')
%Load de FIle
im=mat2gray(imread('Experiment-23-ApoTome-03-Stitching-04Arturo.png'));
%Separate color chanels
imred=im(:,:,1);
% imgreen=im(:,:,2);
% imblue=im(:,:,3);
%View the images
figure, imagesc(im), axis image
figure;
subplot(2,3,1), imagesc(imred),colorbar, axis image, title('Red Channel'), colormap gray
subplot(2,3,4),imhist(imred)
% subplot(2,3,2), imagesc(imgreen),colorbar, axis image, title('green Channel'), colormap gray
% subplot(2,3,5),imhist(imgreen)
% subplot(2,3,3), imagesc(imblue),colorbar, axis image, title('blue Channel'), colormap gray
% subplot(2,3,6),imhist(imblue)
%% select part of image
% im_p=imred(2500:3000,500:1000);
[im_p]=selec_rec(imred);
%%
%-------------------Image ecualization-------------------------------------
[im_equ]=ecu_im(im_p,1000);
%%
%---------------Perform convolution, filetring or other computations------
%------------------median filter------------------------------------------
% m=3; %tamaño de la mascara
% n=3;
% % im=ordfilt2(im_equ, median(1:m*n), ones(m,n));
% im=ordfilt2(im_p, 2, ones(m,n)); %filtro del percentil 0
% % im=ordfilt2(im, m*n-1, ones(m,n));%filtro del percentil 100
%-----------Other convolutions---------------------------------------------
% im = spfilt(im_equ, 'amean', 3, 3);
% im = spfilt(im_equ, 'hmean', 3, 3);
% im = spfilt(im_equ, 'gmean', 3, 3);
% im = spfilt(im_equ, 'chmean', 3, 3);
% im = spfilt(im_equ, 'median', 3, 3);
% im = spfilt(im_equ, 'max', 3, 3);
% im = spfilt(im_equ, 'min', 3,3);
% im = spfilt(im_equ, 'midpoint', 3,3);
% im = spfilt(im_equ, 'atrimmed', 3,3,6);
im = spfilt(im_equ, 'min', 3,3);
im = spfilt(im, 'amean', 3, 3);
figure,subplot(1,2,1)
imagesc(im), colormap gray, axis image
subplot(1,2,2)
imhist(im)
%%
%%----------------Erosions and dilations-----------------------------------
se=strel('disk',3);
fo=imopen(im,se);
foc=imclose(fo,se);
figure, imagesc(foc), colormap gray, axis image
figure, imagesc(fo), colormap gray, axis image
%%
im=foc;
%Apply convolution to increase S2NRatio
w=fspecial('gauss',50,7); %sacamos la mascara
% w=fspecial('average',5);con %sacamos la mascara
g_filt=imfilter(im,w,'conv','replicate','same');
g1=im-g_filt;
figure,
subplot(1,3,1)
imagesc(im), colormap gray, axis image, colorbar, title('raw')
subplot(1,3,2)
imagesc(g_filt), colormap gray, axis image, colorbar, title('filtered')
subplot(1,3,3)
imagesc(g1), colormap gray, axis image, colorbar, title('Raw-filtered')
% g = imsharpen(im);
%%
%----------Edege function, another usufel transofrmation sometimes--------
%
% close all
% [g,t]=edge(mat2gray(g_filt),'Canny');
%
% % [g,t]=edge(abs(g1),'Canny',0.5,2);
% %
% [g,t]=edge(mat2gray(g_filt),'sobel');
[g,t]=edge(mat2gray(g_filt),'log');
figure,
imagesc(g), colormap gray, axis image, colorbar, title('canny')
g_aux=im_p.*~(g==1);
figure, imagesc(g_aux), axis image, colormap gray
%%
%-------------------Image ecualization--------------------------------
[I]=ecu_im(g_filt,1000);
I=mat2gray(g_filt);
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% Watershed transform Using the distance transform
%----and otsus thresholding method %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[WTR,w]=WT(I) ;
figure,
subplot(1,2,1),
imagesc(I), colormap gray, axis image,title('Input Image')
subplot(1,2,2),
imagesc(WTR), colormap gray, axis image, title('Watershed segmentation')
ind_1=g_filt+w;
figure, imagesc(ind_1), colormap gray, axis image,title('Input Image')
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Marker controlled watershed transformation.
I=(I);
figure, imagesc(I), axis image
T=25; %height threshold
% [MWT]=MCW(I,T);
[MWT,bordes]=MCW_8bits(imcomplement(im2uint8(mat2gray(I))), T);
figure,
subplot(1,2,1),
imagesc(I), colormap gray, axis image
subplot(1,2,2),
imagesc(MWT), colormap gray, axis image, title(' Marker controlled Watershed segmentation')
%
%%
%------------------Count cells-------------------------------------------
[tot_cels,RGB]=cell_count(MWT,8);
im_aux=im_p.*~bordes;
im_aux_eq=im_equ.*~bordes;
figure,
imagesc(im_aux), colormap gray, axis image, title('input image')
figure,
imagesc(im_aux_eq), colormap gray, axis image, title('input image')
figure,
subplot(1,3,1)
imagesc(im_aux), colormap gray, axis image, title('input image')
subplot(1,3,2)
imagesc(I), colormap gray, axis image, title('input image')
subplot(1,3,3)
imagesc(RGB), colormap gray, axis image, title(['ncels' num2str(tot_cels)])
[tot_cels,RGB,seg]=cell_repre(MWT,ind_1,8) ;
figure, imagesc(seg), axis image, title(['ncels' num2str(tot_cels)])
figure, imagesc(RGB), axis image, title(['ncels' num2str(tot_cels)])
selec_rec2(im_aux,RGB)
%%
%------------------figure of good segmentation example--------------------%
fac=1;
im_example=mat2gray(im_p).*fac;
im_example(:,:,2)=bordes*fac;%double(bordes).*fac;
im_example(:,:,3)=mat2gray(im_p).*fac;
figure,
imagesc(im_example), axis image, title(['ncels' num2str(tot_cels)])
%figure of good segmentation example
fac=0.8;
im_example=im_p.*~bordes*fac;%double(bordes).*fac;
im_example(:,:,2)=im_p.*~bordes.*fac;%double(bordes).*fac;
im_example(:,:,3)=im_p.*~bordes.*fac;%double(bordes).*fac;
figure,
imagesc(im_example), axis image, title(['ncels' num2str(tot_cels)])
im_aux=im_p+bordes;
figure,
imagesc(im_aux), axis image, title(['ncels' num2str(tot_cels)])