0001 function [tracksR Counter] = WBCtracker3(iFolder,mode,remote,firstFr,lastFr)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 load _param/_WBCparameters
0014
0015 load(strcat(iFolder,'/_frames.mat'));
0016
0017 id = 0;
0018 tracks= [];
0019
0020 Counter.merge = 0;
0021 Counter.colliding = 0;
0022 Counter.split = 0;
0023 Counter.regular = 0;
0024 Counter.lost = 0;
0025
0026 trComplete = [];
0027 kf = KalmanFilt();
0028 features = [];
0029
0030
0031 if (remote == 0 || remote ==2)
0032 f1= figure();
0033 end
0034
0035 while (isempty(frames(firstFr).Cells)) && (firstFr < lastFr)
0036 firstFr = firstFr + 1;
0037 end
0038
0039
0040
0041
0042
0043 if strcmp(mode,'MH')==1
0044 for ce = 1: frames(firstFr).nCells
0045 if (frames(firstFr).Cells(ce).Area > P.tArea)
0046
0047 eccentricity = sqrt((frames(firstFr).Cells(ce).Shape(2))^2 - (frames(firstFr).Cells(ce).Shape(3))^2)/(frames(firstFr).Cells(ce).Shape(2));
0048 area = frames(firstFr).Cells(ce).Area;
0049 if (area > P.tCollide_Size) && ( eccentricity > P.tCollide_Eccentricity)
0050 ce1 = WBC(frames(firstFr).Cells(ce).Frame,...
0051 frames(firstFr).Cells(ce).Id + frames(firstFr).nCells,...
0052 frames(firstFr).Cells(ce).Col,...
0053 frames(firstFr).Cells(ce).Row,...
0054 floor(frames(firstFr).Cells(ce).Area/2),...
0055 frames(firstFr).Cells(ce).Shape);
0056 ce1.Type = [0 0];
0057
0058 ce2 = WBC(frames(firstFr).Cells(ce).Frame,...
0059 frames(firstFr).Cells(ce).Id + frames(firstFr).nCells+1,...
0060 frames(firstFr).Cells(ce).Col,...
0061 frames(firstFr).Cells(ce).Row,...
0062 floor(frames(firstFr).Cells(ce).Area/2),...
0063 frames(firstFr).Cells(ce).Shape);
0064 ce2.Type = [0 0];
0065
0066 [cce1, cce2] =getCollidedCells (frames(firstFr).Cells(ce),ce1,ce2);
0067
0068
0069 id= id+1;
0070 track1 = WBCTrack(cce1,id,P);
0071 track1.Type = 'COLLIDE';
0072 track1.x = [cce1.Col cce1.Row 0 0]';
0073 track1.CoRec = [ce1; ce2; frames(firstFr).Cells(ce)];
0074 track1.svec = zeros(4,1);
0075
0076 id = id +1;
0077 track2 = WBCTrack(cce2,id,P);
0078 track2.Type = 'COLLIDE';
0079 track2.x = [cce2.Col cce2.Row 0 0]';
0080 track2.CoRec = [ce2; ce1; frames(firstFr).Cells(ce)];
0081 track2.svec = zeros(4,1);
0082
0083 track2.CoId = track1.Id;
0084 track1.CoId = track2.Id;
0085 tracks = [tracks track1];
0086 tracks = [tracks track2];
0087 else
0088
0089 id = id+1;
0090 track = WBCTrack(frames(firstFr).Cells(ce),id,P);
0091 track.Type = 'ACTIVE';
0092 tracks = [tracks track];
0093 end
0094 end
0095 end
0096 else
0097 for ce = 1: frames(firstFr).nCells
0098 if (frames(firstFr).Cells(ce).Area > P.tArea)
0099 track = WBCTrack(frames(firstFr).Cells(ce),ce,P);
0100 tracks = [tracks track];
0101 end
0102 end
0103 end
0104
0105
0106 for tr = 1: length(tracks)
0107 tracks(tr) = kf.predict(tracks(tr));
0108 end
0109
0110
0111 for fr = firstFr+1:lastFr
0112 feature =({});
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 if (~isempty(frames(fr).Cells))
0144 if strcmp(mode,'MH')==1
0145 [mergeCells splitCells_ splitCells] = collision_prob(tracks,frames);
0146 else
0147 mergeCells = [];
0148 splitCells_ = [];
0149 splitCells = [];
0150 end
0151
0152
0153 [H idTable feaTab] = corTable(tracks,frames(fr),mergeCells,splitCells_,splitCells,P);
0154
0155
0156 N = frames(fr).nCells;
0157 M = length(tracks) + length(mergeCells);
0158 matching = greedySearch(H,idTable,M,N);
0159 feature.H = H;
0160 feature.idTable = idTable;
0161 feature.feaTab = feaTab;
0162 feature.matching = matching;
0163 features = [features; feature];
0164
0165
0166 for r = 1: size(idTable,1)
0167 if H(r,1) ~= P.tCollision_CorrespondenceCode
0168 [tmin idx] = min(matching(r,:));
0169 ind = idTable{r,idx};
0170 if tmin<P.tVal
0171 switch length(ind)
0172 case 2
0173 if strcmp(tracks(r).Type,'COLLIDE')==1
0174 Counter.colliding = Counter.colliding + 1;
0175
0176 cell2 = searchTrack(tracks,tracks(r).CoId);
0177 [cce1, cce2] =getCollidedCells (frames(fr).Cells(ind(2)),tracks(r).CoRec(1),tracks(cell2).CoRec(1));
0178
0179 tracks(r).add(cce1);
0180 tracks(r).CoRec(1) = cce1;
0181 tracks(cell2).add(cce2);
0182 tracks(cell2).CoRec(1) = cce2;
0183
0184 frames(fr).Cells(ind(2)).Prev = [tracks(r).Id tracks(cell2).Id];
0185 elseif (strcmp(tracks(r).Type,'LOST')==1) && ~isempty(tracks(r).CoRec)
0186
0187 Counter.colliding = Counter.colliding + 1;
0188
0189 tracks(r).Type = 'COLLIDE';
0190 tracks(r).add(frames(fr).Cells(ind(2)));
0191 cell2 = searchTrack(tracks,tracks(r).CoId);
0192 tracks(cell2).Type = 'COLLIDE';
0193 tracks(cell2).add(frames(fr).Cells(ind(2)));
0194 else
0195 Counter.regular = Counter.regular +1;
0196 tracks(r).Type = 'ACTIVE';
0197 tracks(r).add(frames(fr).Cells(ind(2)));
0198 end
0199 case 3
0200 tr1 = searchTrack(tracks,ind(1));
0201 tr2 = searchTrack(tracks,ind(2));
0202 if (tr1~=0) && (tr2 ~=0)
0203
0204 Counter.merge = Counter.merge + 1;
0205
0206 tracks(tr1).Type = 'COLLIDE';
0207 tracks(tr1).CoId = tracks(tr2).Id;
0208 tracks(tr1).CoRec = [tracks(tr1).Cells(end); tracks(tr2).Cells(end); frames(fr).Cells(ind(3))];
0209
0210 [cce1, cce2] =getCollidedCells (frames(fr).Cells(ind(3)),tracks(tr1).Cells(end),tracks(tr2).Cells(end));
0211
0212 tracks(tr1).add(cce1);
0213 tracks(tr1).svec = [tracks(tr1).x(3:4)];
0214 tracks(tr1).x = [tracks(tr1).Cells(end).Col; tracks(tr1).Cells(end).Row; 0; 0];
0215
0216 tracks(tr2).Type = 'COLLIDE';
0217 tracks(tr2).CoId = tracks(tr1).Id;
0218 tracks(tr2).CoRec = [tracks(tr2).Cells(end); tracks(tr1).Cells(end); frames(fr).Cells(ind(3))];
0219 tracks(tr2).add(cce2);
0220 tracks(tr2).svec = [tracks(tr2).x(3:4)];
0221 tracks(tr2).x = [tracks(tr2).Cells(end).Col; tracks(tr2).Cells(end).Row; 0; 0];
0222
0223 frames(fr).Cells(ind(3)).Prev = [tracks(tr1).Id tracks(tr2).Id];
0224 end
0225
0226 case 4
0227 tr1 = searchTrack(tracks,ind(1));
0228 tr2 = searchTrack(tracks,ind(2));
0229 frames(fr)
0230 if (tr~=0) && (tr2~=0)
0231
0232 Counter.split = Counter.split + 1;
0233
0234 tracks(tr1).Type = 'ACTIVE';
0235 tracks(tr1).CoRec = [];
0236 tracks(tr1).CoId = [];
0237 tracks(tr1).add(frames(fr).Cells(ind(3)));
0238 tracks(tr1).x_(3:4) = tracks(tr1).svec(1);
0239
0240 tracks(tr2).Type = 'ACTIVE';
0241 tracks(tr2).CoRec = [];
0242 tracks(tr2).CoId = [];
0243 tracks(tr2).add(frames(fr).Cells(ind(4)));
0244 tracks(tr2).x_(3:4) = tracks(tr2).svec(1:2);
0245 end
0246
0247 otherwise
0248 disp('Invalid case');
0249 end
0250 elseif (length(ind)==2)
0251 Counter.lost = Counter.lost + 1;
0252
0253 if strcmp(tracks(r).Type,'COLLIDE')==1
0254 cell2 = searchTrack(tracks,tracks(r).CoId);
0255 tracks(cell2).Type = 'LOST';
0256 end
0257 tracks(r).Type = 'LOST';
0258 end
0259 end
0260 end
0261 else
0262 for tr =1 : length(tracks)
0263 Counter.lost = Counter.lost + 1;
0264 tracks(tr).Type = 'LOST';
0265 end
0266 end
0267
0268
0269 for tr=1: length(tracks)
0270 tracks(tr) = kf.update(tracks(tr));
0271 end
0272
0273
0274 for ce = 1: frames(fr).nCells
0275 if isempty(frames(fr).Cells(ce).Prev)&& (frames(fr).Cells(ce).Area > P.tArea)
0276
0277 id = id +1;
0278 track = WBCTrack(frames(fr).Cells(ce),id,P);
0279 tracks = [tracks track];
0280
0281 end
0282 end
0283
0284
0285 trTemp= [];
0286 for tr = 1: length(tracks)
0287
0288 tracks(tr) = kf.predict(tracks(tr));
0289 if strcmp(tracks(tr).Type,'INACTIVE')==0
0290 trTemp = [trTemp tracks(tr)];
0291 else
0292 trComplete = [trComplete tracks(tr)];
0293 end
0294 end
0295 tracks = trTemp;
0296
0297
0298
0299 if (remote == 0)
0300 mapper = BCMapper(P);
0301 bg = mapper.read(iFolder,'BD',fr);
0302 postfix = mapper.getname(iFolder, fr, '');
0303 hold off;imshow(bg);hold on;
0304
0305 for tr = 1: length(tracks)
0306 if strcmp(tracks(tr).Type,'INACTIVE')==0
0307 if (strcmp(tracks(tr).Type,'ACTIVE')==1)||(strcmp(tracks(tr).Type,'NEW')==1)||(strcmp(tracks(tr).Type,'COLLIDE')==1)
0308 tracks(tr).plot('g',P);
0309 elseif (strcmp(tracks(tr).Type,'LOST')==1)
0310 tracks(tr).plot('r',P);
0311 end
0312 else
0313 tracks(tr).plot('w',P);
0314 end
0315 end
0316
0317
0318 fillfigure();
0319 imOut = [iFolder,'/TR/',postfix,'.tr.png'];
0320 exportfigure(imOut,f1,size(bg));
0321 end
0322
0323
0324 end
0325
0326
0327 trComplete = [trComplete tracks];
0328 tracks = trComplete;
0329 trComplete = [];
0330
0331
0332 if (remote ==0 || remote ==2)
0333 hold off; imshow(zeros(size(bg))); hold on;
0334
0335 for tr =1: length(tracks)
0336 if tracks(tr).nCells > P.tFrame_PlotFinalTrack
0337
0338 tracks(tr).plotAll2D(P);
0339
0340 end
0341 end
0342 fillfigure();
0343 imOut = strcat(iFolder,'/TR/','ALL',num2str(firstFr),'-',num2str(lastFr),'.png');
0344 exportfigure(imOut,f1,size(bg));
0345 end
0346
0347
0348
0349 tracks_ARR = reduceTracks(tracks, iFolder);
0350 tracksR = convert2matrix(tracks_ARR);
0351
0352
0353
0354
0355 end
0356
0357
0358