-
Notifications
You must be signed in to change notification settings - Fork 0
/
O_SegmentCells_v6_SingleImage.m
255 lines (205 loc) · 7.25 KB
/
O_SegmentCells_v6_SingleImage.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
function [iterTable,ImageID,O]=O_SegmentCells_v6_SingleImage(O,ImageIDs,t1,t2)
% to collect a new property:
% 1) DEFINE: Data.O.Collecting_X_Data='Collecting_Lkb_Data';
% 2) see example Collecting_Lkb_Data.m
% To have the software accept images as input rather than load images add
% the field: O.SegmentationParameters.No_Loading_Images=true. The images
% used will then be O.IM
% THERE ARE TWO VARIABLES WITH INFO ON HOW TO SEGMENT:
% 1) O.General_Thresholds
% 2) O.SegmentationParameters
disp(ImageIDs(1,1:4))
Labeled_channals=find(~strcmp(O.General_Thresholds.Label,'Non'));
Nucleus_ch=find(strcmp(O.General_Thresholds.Label,'Nucleus'));
Cell_ch=find(strcmp(O.General_Thresholds.Label,'Cell'));
Seed_ch=find(O.General_Thresholds.Seed); %This channel is used as seed for NUCLEI. Nuclei are always used as seeds for CELL channel.
OutPutdir=[O.OutputDir O.DataSetName];
if ~exist(OutPutdir,'dir')
mkdir(OutPutdir);
end
if isempty(Nucleus_ch)
disp('This version does not work without a defined nuclear channal')
return
end
% LoadingPie makes a pie chart plot for loding images
O.LoadingPie=false;
%% IMAGE PROCESSING STARTS HERE
O.ImageID=ImageIDs;
% STEP 1: loading 4 images
if ~isfield(O.SegmentationParameters,'No_Loading_Images')
[O]=O_LoadImages(O);
end
% to view loaded image: imshow(O.IM{2},[])
for ch=Labeled_channals'
% STEP 2: calculate seeds
im=O.IM{ch};
thr=O.General_Thresholds.Intensity_thr(ch);
alpha=O.General_Thresholds.smoothing_alpha(ch);
eval(['[BW_spots,sIM_small]=' O.Spotting_alg '(alpha,im,thr);'])
O.BW_spots{ch}=BW_spots;
% STEP 3: thresholding
O.BW{ch}=O.IM{ch}>thr;
O.sIM_small{ch}=sIM_small;
end
%% STEP 4: segmenting nucleus
% watershedding
ch=Nucleus_ch;
NucIM=O.IM{ch};
thr=O.General_Thresholds.Intensity_thr(ch);
if isempty(Seed_ch)
Seeds=O.BW_spots{ch};
else
Seeds=O.BW_spots{Seed_ch};
end
O.WS{ch}=O_WaterShedCells(NucIM,Seeds,O.sIM_small{ch});
% combining watershed with thresholded im
O.BW{ch}=O.BW{ch} & O.WS{ch};
% RegionalThresholding
if O.SegmentationParameters.Regional_Thresholding==1
WS=imreconstruct(O.BW{ch},O.WS{ch});
WS=bwlabel(WS);
CC = bwconncomp(WS);
THR=ones(size(O.BW{ch}))*10*thr;
for i=1:length(CC.PixelIdxList)
srt=sort(im(CC.PixelIdxList{i}),'descend');
LocalThr=median(srt(1:50));
THR(CC.PixelIdxList{i})=LocalThr;
end
O.BW{ch}=im>0.5*THR&WS;
end
% REMOVING "CELLS" THAT ARE TOO BIG OR SMALL
Mn=O.General_Thresholds.Min_Area(ch);
Mx=O.General_Thresholds.Max_Area(ch);
O.BW{ch}=bwareafilt(O.BW{ch},[Mn Mx+1]);
% clean up
O.BW{ch}=imfill(O.BW{ch},'holes');
% Miriam 4/7/17: Remove this step to aid in segmenting cells. Borders will
% be cleared later
% O.BW{ch}=imclearborder(O.BW{ch});
%% STEP 5: segmenting Cell (optional)
% watershedding
ch=Cell_ch;
if ~isempty(ch)
Seeds=O.BW{Nucleus_ch};
O.WS{ch}=O_WaterShedCells(O.IM{ch},Seeds);
% combining watershed with thresholded im
O.BW{ch}=(O.BW{ch} | O.BW{Nucleus_ch}) & O.WS{ch};
% clean up
O.BW{ch}=imfill(O.BW{ch},'holes');
Mn=O.General_Thresholds.Min_Area(ch);
Mx=O.General_Thresholds.Max_Area(ch);
O.BW{ch}=bwareafilt(O.BW{ch},[Mn Mx+1]);
O.BW{ch}=imclearborder(O.BW{ch});
end
%% STEP 6: making sure nucleii are CONTAINED in cells + labeling
if ~isempty(Cell_ch)
O.BW{Nucleus_ch}=O.BW{Nucleus_ch} & O.BW{Cell_ch};
O.BW{Cell_ch}=imreconstruct(O.BW{Nucleus_ch},O.BW{Cell_ch});
for ch=setdiff(Labeled_channals',[Cell_ch Nucleus_ch])
O.BW{ch}=O.BW{ch} & O.BW{Cell_ch};
end
O.BW{Cell_ch}=bwlabel(O.BW{Cell_ch});
for ch=setdiff(Labeled_channals',[Cell_ch])
O.BW{ch}=double(O.BW{ch}).*O.BW{Cell_ch};
end
else
O.BW{Nucleus_ch}=bwlabel(O.BW{Nucleus_ch});
end
%% STEP 7: COLLECTING DATA
NumberOfCells=max(O.BW{Nucleus_ch}(:));
[T]=CollectNucleusData(O,NumberOfCells);
iterTable=T;
if ~isempty(Cell_ch)
[T]=CollectCellData(O,NumberOfCells);
iterTable=[iterTable T];
end
if isfield(O.SegmentationParameters,'Edge_Sharpness') && O.SegmentationParameters.Edge_Sharpness==1
[T]=Collecting_EdgeDef(O,NumberOfCells);
iterTable=[iterTable T];
end
if isfield(O,'Collecting_X_Data')
fprintf('Running %s...\n', O.Collecting_X_Data)
eval(['[T]=' O.Collecting_X_Data '(O,NumberOfCells);'])
iterTable=[iterTable T];
end
if isfield(O,'Collecting_More_Data') && NumberOfCells > 0
for i=1:length(O.Collecting_More_Data)
func_name = O.Collecting_More_Data{i};
fprintf('Running %s...\n', func_name)
eval(['[T]=' func_name '(O,NumberOfCells);'])
iterTable=[iterTable T];
end
end
% figure(1)
% imshow(showseg_thick(NormalizeImage(O.IM{1}),O.BW{1},[1 0 0]),[])
% figure(2)
% imshow(showseg_thick(NormalizeImage(O.IM{3}),O.BW{3},[1 0 0]),[])
% % [x,y]=find(O.BW_spots);
% % hold on
% % plot(y,x,'.c')
% % hold off
% disp('l')
%%
ImageID=O.ImageID;
function [T]=CollectNucleusData(O,NumberOfCells)
Nucleus_ch=find(cell2mat(strfind(O.General_Thresholds.Label,'Nucleus')));
NucStats=regionprops(O.BW{Nucleus_ch},'Area','Eccentricity','Solidity','Centroid',...
'MajorAxisLength','MinorAxisLength','Orientation');
Eccentricity=cat(1,NucStats.Eccentricity);
Solidity=cat(1,NucStats.Solidity);
MajorAxisLength=cat(1,NucStats.MajorAxisLength);
MinorAxisLength=cat(1,NucStats.MinorAxisLength);
Orientation=cat(1,NucStats.Orientation);
Centroid=cat(1,NucStats.Centroid);
NArea=cat(1,NucStats.Area);
Ncells=length(NArea);
NInt=zeros(length(NArea),4);
for Channel=1:4
if ismember(Channel,O.ImagedChannels)
stats_nuc=regionprops(O.BW{Nucleus_ch},O.IM{Channel},'MeanIntensity');
NInt(1:Ncells,Channel)=cat(1,stats_nuc.MeanIntensity).*NArea;
end
end
T=table(Eccentricity,Solidity,MajorAxisLength,MinorAxisLength,Orientation,Centroid,NArea,NInt);
function [T]=CollectCellData(O,NumberOfCells)
Cell_ch=find(cell2mat(strfind(O.General_Thresholds.Label,'Cell')));
CellStats=regionprops(O.BW{Cell_ch},'Area');
CArea=cat(1,CellStats.Area);
CInt=zeros(NumberOfCells,4);
for Channel=1:4
if ismember(Channel,O.ImagedChannels)
stats_cell=regionprops(O.BW{Cell_ch},O.IM{Channel},'MeanIntensity');
CInt(1:NumberOfCells,Channel)=cat(1,stats_cell.MeanIntensity).*CArea;
end
end
T=table(CArea,CInt);
function [T]=Collecting_EdgeDef(O,NumberOfCells)
Nucleus_ch=find(cell2mat(strfind(O.General_Thresholds.Label,'Nucleus')));
ED=zeros(1,NumberOfCells);
edgeim=stdfilt(O.IM{Nucleus_ch});
MaskPerim=imdilate(bwperim(O.BW{Nucleus_ch}),strel('disk',2));
MaskCellInterior=imfill(MaskPerim,'holes');
MaskPerim=MaskPerim.*O.BW{Nucleus_ch};
MaskCellInterior=MaskCellInterior.*O.BW{Nucleus_ch};
MaskCellInterior=bwareaopen(MaskCellInterior,30);
MaskCellInterior=bwlabel(MaskCellInterior);
MaskPerim=bwlabel(MaskPerim);
EdgeP1=regionprops(MaskPerim,edgeim,'meanintensity');
EdgeP2=regionprops(MaskCellInterior,edgeim,'meanintensity');
Ed1=cat(1,EdgeP1.MeanIntensity);
Ed2=cat(1,EdgeP2.MeanIntensity);
if size(Ed2,2)>1
Ed2=Ed2';
end
if size(Ed1,2)>1
Ed1=Ed1';
end
NEdgeDef=Ed1./Ed2;
T=table(NEdgeDef);
function [imn]=NormalizeImage(im,varargin)
IM=im2double(im);
imn=(IM-min(IM(:)))/(max(IM(:))-min(IM(:)));
if ~isempty(varargin)
imn=(IM-varargin{1}(1))/(varargin{1}(2)-varargin{1}(1)); %%??
end
function [BW]=ThresholdImage(im,thr,seeds,alg)