/
at_batchSeg.m
executable file
·326 lines (222 loc) · 7.91 KB
/
at_batchSeg.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
% position : [ 1 2 3 8] : list positions to be anayzed
% path, file, frames
% optional arguments:
% 'cells' : segment cell contours
% 'nucleus' : segment and score nuclei
% 'mapnucleus': map nuclei
% 'mapcells': map cells
% 'display' : display running segmentation
%%
function at_batchSeg(frames, position,varargin)
global segmentation timeLapse
segCells = getMapValue(varargin, 'cells');
segNucleus = getMapValue(varargin, 'nucleus');
mapNucleus = getMapValue(varargin, 'mapnucleus');
mapCells = getMapValue(varargin, 'mapcells');
cellcycle = getMapValue(varargin, 'cellcycle');
display = getMapValue(varargin, 'display');
if display
if segCells
hcells=figure('Position',[10 10 800 600]);
end
if segNucleus
hnucleus=figure('Position',[1000 10 800 600]);
end
end
for l=position
if segCells==0 && segNucleus==0
[segmentation timeLapse]=phy_openSegmentationProject(timeLapsepath,timeLapsefile,l,[1 3]);
else
strPath=strcat(timeLapsepath,timeLapsefile);
load(strPath);
timeLapse.path=timeLapsepath;
timeLapse.realPath=timeLapsepath;
segmentation=[];
segmentation=phy_createSegmentation(timeLapse,l);
segmentation.position=l;
setProcessingParameters
cc=1;
nstore=0;
for i=frames
imcell=[];
imbud=[];
if segCells
imcell=segmentCells(i);
end
if segNucleus
imbud=segmentNucleus(i);
end
if mapNucleus
nstore=mappeNucleus(cc,nstore,i);
end
if display
displayCells(imcell,imbud,i,segCells,segNucleus,hcells,hnucleus)
end
updateProgressMonitor('Progress', cc, size(frames, 2));
cc=cc+1;
end
if mapCells
% phy_mapCellBackwards(frames(end),frames(1));
end
if segCells
segmentation.cells1Segmented(frames(1):frames(end))=1;
end
if mapCells
segmentation.cells1Mapped(frames(1):frames(end))=1;
end
if segNucleus
segmentation.nucleusSegmented(frames(1):frames(end))=1;
end
if mapNucleus
segmentation.nucleusMapped(frames(1):frames(end))=1;
[segmentation.tnucleus fchange]=phy_makeTObject(segmentation.nucleus,segmentation.tnucleus);
end
segmentation.frameChanged(frames(1):frames(end))=1;
fprintf(['Saving Position: ' num2str(l) '...\n']);
save(fullfile(timeLapse.realPath,timeLapse.pathList.position{segmentation.position},'segmentation-batch.mat'),'segmentation');
end
fprintf(['Compute cell cycle stat: ' num2str(l) '...\n']);
if cellcycle
stat(l)=phy_extractCellCyclePhase(1:max([segmentation.tnucleus.N]),1);
save(fullfile(timeLapse.realPath,'cellcyclestat.mat'),'stat');
end
end
if display
close(hcells); close(hnucleus);
end
end
%%
function setProcessingParameters()
global segmentation
% cell segmentation
segmentation.processing.parameters{1,14}{1,2}=1;
segmentation.processing.parameters{1,14}{2,2}=400;
segmentation.processing.parameters{1,14}{3,2}=10000;
segmentation.processing.parameters{1,14}{4,2}=50;
segmentation.processing.parameters{1,14}{5,2}=0.35;
segmentation.processing.parameters{1,14}{6,2}=0;
segmentation.processing.parameters{1,14}{7,2}=0;
% nucleus segmentation
segmentation.processing.parameters{4,15}{1,2}=2;
segmentation.processing.parameters{4,15}{2,2}=20;
segmentation.processing.parameters{4,15}{3,2}=4000;
segmentation.processing.parameters{4,15}{4,2}=1000;
% nucleus mapping
segmentation.processing.parameters{4,9}{1,2}=1;
segmentation.processing.parameters{4,9}{2,2}=40;
segmentation.processing.parameters{4,9}{3,2}=1;
segmentation.processing.parameters{4,9}{4,2}=1;
segmentation.processing.parameters{4,9}{5,2}=0;
segmentation.processing.parameters{4,9}{6,2}=0;
end
%%
function nstore=mappeNucleus(cc,nstore,i)
global segmentation
if cc>1
nstore=max(nstore, max([segmentation.nucleus(i-1,:).n]));
temp=segmentation.discardImage(1:i-1); % frame is discarded by user ; display previous frame
trackFrame=find(temp==0,1,'last');
cell0=segmentation.nucleus(trackFrame,:);
cell1=segmentation.nucleus(i,:);
parametres=segmentation.processing.parameters{4,9};
segmentation.nucleus(i,:)=phy_mapCellsHungarian(cell0,cell1,nstore,parametres{2,2}, parametres{3,2},parametres{4,2},parametres{5,2},parametres{6,2});
end
end
%%
function displayCells(imcells,imbud,i,segCells,segNucleus,hcells,hnucleus)
global segmentation
if segCells
figure(hcells);
warning off all
imshow(imcells,[]); hold on;
warning on all
cellsout=segmentation.cells1(i,:);
for j=1:length(cellsout)
line(cellsout(j).x,cellsout(j).y,'Color','r','LineWidth',1);
text(cellsout(j).ox,cellsout(j).oy,num2str(cellsout(j).n),'Color','r');
end
text(10,10,['Cells - Position: ' num2str(segmentation.position) ' -Frame:' num2str(i)],'Color','r');
end
if segNucleus
figure(hnucleus);
warning off all
imshow(imbud,[]); hold on;
warning on all
cellsout=segmentation.nucleus(i,:);
for j=1:length(cellsout)
line(cellsout(j).x,cellsout(j).y,'Color','g','LineWidth',1);
text(cellsout(j).ox,cellsout(j).oy,num2str(cellsout(j).n),'Color','g');
end
text(10,10,['Nucleus - Position: ' num2str(segmentation.position) ' -Frame:' num2str(i)],'Color','g');
end
end
%%
function imcells=segmentCells(i)
global segmentation
imcells=phy_loadTimeLapseImage(segmentation.position,i,1,'non retreat');
segmentation.cells1(i,:)=phy_Object;
% cov=std(double(imcells(:)))/mean(double(imcells(:)));
% if cov<0.26
% segmentation.discardImage(i)=1;
% return;
% end
cells=phy_segmentWatershedGC(imcells,segmentation.processing.parameters{1,14}{2,2},...
segmentation.processing.parameters{1,14}{3,2},segmentation.processing.parameters{1,14}{4,2},...
segmentation.processing.parameters{1,14}{5,2},segmentation.processing.parameters{1,14}{6,2},...
segmentation.processing.parameters{1,14}{7,2});
for j=1:length(cells)
segmentation.cells1(i,j)=cells(j);
segmentation.cells1(i,j).image=i;
end
end
%%
function imbud=segmentNucleus(i)
global segmentation
imbud=phy_loadTimeLapseImage(segmentation.position,i,2,'non retreat');
warning off all
imbud=imresize(imbud,2);
warning on all
parametres=segmentation.processing.parameters{4,15};
budnecktemp=phy_segmentNucleus(imbud,parametres{4,2},parametres{2,2},parametres{3,2},parametres{1,2});
budneck=phy_Object;
for j=1:length(budnecktemp)
if budnecktemp(j).n~=0
segmentation.nucleus(i,j)=budnecktemp(j);
segmentation.nucleus(i,j).image=i;
end
end
end
%%
function [timeLapsepath timeLapsefile]=setProjectPath(path,file)
global timeLapse
str=strcat(path,file);
load(str);
timeLapse.realPath=strcat(path);
timeLapse.realName=file;
timeLapsepath=timeLapse.realPath;
timeLapsefile=[timeLapse.filename '-project.mat'];
end
%%
function value = getMapValue(map, key)
value = 0;
for i = 1:1:numel(map)
if strcmp(map{i}, key)
value = 1;
return
end
end
end
%%
function updateProgressMonitor(message, progress, maximum)
persistent previousLineLength;
if isempty(previousLineLength)
previousLineLength = 0;
end
percentage = round(progress * 100 / maximum);
% animation = 'oOC(|)|(Cc';
animation = '-\|/';
animationIndex = 1 + mod(progress, length(animation));
line = sprintf('%s: % 4.3g %% %s', message, percentage, animation(animationIndex));
fprintf([repmat('\b', [1 previousLineLength]) '%s'], line);
previousLineLength = length(line);
end