0001 function [tracksR] = TrackingKE(dataFolder,frFirst,frLast,mode,remote)
0002 iPath = 'C:\Research\DataSets\';
0003 iFolder = strcat(iPath,dataFolder);
0004 cd(strcat(iFolder,'\MN'));
0005 load ../_bloodFlowDir;
0006 iFiles = dir('*.mn.png');
0007 load ../_frames;
0008 count = 0;
0009 tracks= [];
0010 tracksR = [];
0011 mkdir('../TR');
0012 trComplete = [];
0013
0014 kf = KalmanFilt();
0015 features = [];
0016 tVal = 5;
0017
0018
0019 firstFr = frFirst;
0020 lastFr = frLast;
0021
0022
0023
0024 while (isempty(frames(firstFr).Cells)) && (firstFr < lastFr)
0025 firstFr = firstFr + 1;
0026 end
0027
0028
0029 if (firstFr < lastFr)
0030
0031
0032 for ce = 1: frames(firstFr).nCells
0033
0034 track = WBCTrack(frames(firstFr).Cells(ce),ce);
0035 tracks = [tracks track];
0036
0037 end
0038
0039 if (remote == 0)
0040 f1= figure(1);
0041 bg=zeros(1000,1000);
0042 imshow(bg);fillfigure();
0043 end
0044
0045 for fr = firstFr+1:lastFr
0046 feature =({});
0047 disp(['Tracking frame ' num2str(fr)]);
0048
0049
0050 imID = frames(fr).Id;
0051
0052 if (remote ==0)
0053 bg = imread (iFiles(fr).name);
0054 postfix = iFiles(fr).name(1:end-7);
0055
0056 f1= figure(1);
0057 imshow(bg);
0058 end
0059
0060 for tr = 1: length(tracks)
0061 tracks(tr) = kf.predict(tracks(tr));
0062 if (remote ==0)
0063 plotPredict(tracks(tr),'y');
0064 end
0065 end
0066 if (remote ==0)
0067 plotFrame(frames(fr),'y');
0068
0069 imOut = ['../TR/',postfix,'.pr.png'];
0070 fillfigure();
0071 exportfigure(imOut,f1,[1000 1000]);
0072 end
0073
0074 if (~isempty(frames(fr).Cells))
0075 if (strcmp(mode,'KC')==1)
0076 [mergeCells splitCells_ splitCells] = collision_prob(tracks,frames);
0077 elseif (strcmp(mode,'K')==1)
0078 mergeCells = [];
0079 splitCells_ = [];
0080 splitCells = [];
0081 else
0082 error('Not supported mode');
0083 end
0084
0085 [H idTable feaTab] = corTable(tracks,frames(fr),mergeCells,splitCells_,splitCells,mode);
0086
0087
0088 N = frames(fr).nCells;
0089 M = length(tracks) + length(mergeCells);
0090
0091
0092
0093
0094
0095
0096
0097
0098 matching = greedySearch(H,idTable,feaTab,M,N);
0099 feature.H = H;
0100 feature.idTable = idTable;
0101 feature.feaTab = feaTab;
0102
0103
0104 for r = 1: size(idTable,1)
0105 [tmin idx] = min(matching(r,:));
0106 ind = idTable{r,idx};
0107 if tmin<tVal
0108 switch length(ind)
0109 case 2
0110 if strcmp(tracks(r).Type,'COLLIDE')==1
0111
0112 ce1 = tracks(r).Cells(end);
0113
0114 rr = searchTrack(tracks,tracks(r).CoId);
0115 ce2 = tracks(rr).Cells(end);
0116
0117 frames(fr).Cells(ind(2)).Prev = [tracks(r).Id tracks(rr).Id];
0118
0119 [cce1 cce2] = getCollidedCells(frames(fr).Cells(ind(2)),ce1,ce2);
0120
0121 tracks(r).add(cce1);
0122 tracks(r).Cells(end).Status = tracks(r).Cells(end-1).Status;
0123
0124 tracks(rr).add(cce2);
0125 tracks(rr).Cells(end).Status = tracks(rr).Cells(end-1).Status;
0126 else
0127 tracks(r).Type = 'ACTIVE';
0128 tracks(r).add(frames(fr).Cells(ind(2)));
0129 end
0130 case 3
0131 tr1 = searchTrack(tracks,ind(1));
0132 tr2 = searchTrack(tracks,ind(2));
0133 if (tr1~=0) && (tr2 ~=0)
0134 ce1 = tracks(tr1).Cells(end);
0135 ce2 = tracks(tr2).Cells(end);
0136 frames(fr).Cells(ind(3)).Prev = [tracks(tr1).Id tracks(tr2).Id];
0137
0138
0139
0140
0141
0142
0143 [cce1 cce2] = getCollidedCells(frames(fr).Cells(ind(3)),ce1,ce2);
0144
0145 tracks(tr1).Type = 'COLLIDE';
0146 tracks(tr1).CoId = ind(2);
0147 tracks(tr1).add(cce1);
0148 tracks(tr1).Cells(end).Status = frames(fr).Cells(ind(3));
0149 tracks(tr1).svec = [tracks(tr1).x(3:4)];
0150
0151
0152 tracks(tr2).Type = 'COLLIDE';
0153 tracks(tr2).CoId = ind(1);
0154 tracks(tr2).add(cce2);
0155 tracks(tr2).Cells(end).Status = frames(fr).Cells(ind(3));
0156 tracks(tr2).svec = [tracks(tr2).x(3:4)];
0157
0158 end
0159
0160 case 4
0161 tr1 = searchTrack(tracks,ind(1));
0162 tr2 = searchTrack(tracks,ind(2));
0163
0164 if (tr~=0) && (tr2~=0)
0165 tracks(tr1).Type = 'ACTIVE';
0166 tracks(tr1).add(frames(fr).Cells(ind(3)));
0167 tracks(tr1).x_(3:4) = tracks(tr1).svec(1:2);
0168
0169 tracks(tr2).Type = 'ACTIVE';
0170 tracks(tr2).add(frames(fr).Cells(ind(4)));
0171 tracks(tr2).x_(3:4) = tracks(tr2).svec(1:2);
0172
0173 end
0174
0175 otherwise
0176 disp('Invalid case');
0177 end
0178 elseif (length(ind)==2) && (strcmp(tracks(r).Type,'COLLIDE')~=1)
0179 tracks(r).Type = 'LOST';
0180 end
0181 end
0182 else
0183 for tr =1 : length(tracks)
0184 tracks(tr).Type = 'LOST';
0185 end
0186 end
0187
0188 for tr=1: length(tracks)
0189 tracks(tr) = kf.update(tracks(tr));
0190 end
0191
0192
0193 if (~isempty(tracks))
0194 count = tracks(numel(tracks)).Id;
0195 end
0196 for ce = 1: frames(fr).nCells
0197 if isempty(frames(fr).Cells(ce).Prev)
0198 count = count +1;
0199 track = WBCTrack(frames(fr).Cells(ce),count);
0200 tracks = [tracks track];
0201 end
0202 end
0203 features = [features; feature];
0204
0205
0206 if (remote == 0)
0207 imshow(bg);
0208 end
0209 trTemp= [];
0210 for tr = 1: length(tracks)
0211 if strcmp(tracks(tr).Type,'INACTIVE')==0
0212 trTemp = [trTemp tracks(tr)];
0213 if (remote == 0)
0214 if (strcmp(tracks(tr).Type,'ACTIVE')==1)||(strcmp(tracks(tr).Type,'NEW')==1)||(strcmp(tracks(tr).Type,'COLLIDE')==1)
0215 tracks(tr).plot('g');
0216 elseif (strcmp(tracks(tr).Type,'LOST')==1)
0217 tracks(tr).plot('r');
0218 end
0219 end
0220 else
0221 trComplete = [trComplete tracks(tr)];
0222 if (remote ==0)
0223 tracks(tr).plot('w');
0224 end
0225 end
0226 end
0227 tracks = trTemp;
0228
0229 if (remote == 0)
0230 pause(.01);
0231
0232
0233 imOut = ['../TR/',postfix,'.tr.png'];
0234 exportfigure(imOut,f1,[1000 1000]);
0235 if (mod(fr,20)==0)
0236 close(f1);
0237 end
0238 end
0239
0240 end
0241
0242
0243 for tr =1: length(tracks)
0244 if tracks(tr).nKfCells > 0
0245 for ce = 1: tracks(tr).nKfCells
0246 tracks(tr).removeLast;
0247 end
0248 end
0249 end
0250
0251
0252 trComplete = [trComplete tracks];
0253 tracks = trComplete;
0254
0255
0256 if (remote == 0)
0257 f2=figure(2); imshow(zeros(1000,1000));
0258 for tr =1: length(tracks)
0259 if tracks(tr).nCells > 3
0260 plotAll(tracks(tr));
0261 end
0262 end
0263
0264 fillfigure();
0265 imOut = ['../TR/','ALL.png'];
0266 exportfigure(imOut,f2,[1000 1000]);
0267 end
0268 prefix = iFiles(1).name(1:end-10);
0269 tracksR = reduceTracks(tracks, prefix);
0270 tracksR = convert2matrix(tracksR);
0271 save ../_tracks.mat tracks
0272
0273 end
0274
0275 end
0276
0277
0278