Home > track > WBCtracker3.m

WBCtracker3

PURPOSE ^

mode can be 'KF' for Kalman for 'MH' for Multiple Hypothesis

SYNOPSIS ^

function [tracksR Counter] = WBCtracker3(iFolder,mode,remote,firstFr,lastFr)

DESCRIPTION ^

 mode can be 'KF' for Kalman for 'MH' for Multiple Hypothesis
% INITIALIZATION
 if (nargin ==1)
     remote = 0;
     load _frames;
 end
 load /wbc/Data/_param/_parameters %parameters structure
 iPath = '\Research\NKT\Data\2009-04-23_control_dance_floor_observation\';
 iPath = '\Research\WBC\Data\';
 iPath = '\Research\RBC\Data\';
 P = initParam('wbc');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [tracksR Counter] = WBCtracker3(iFolder,mode,remote,firstFr,lastFr)
0002 % mode can be 'KF' for Kalman for 'MH' for Multiple Hypothesis
0003 %% INITIALIZATION
0004 % if (nargin ==1)
0005 %     remote = 0;
0006 %     load _frames;
0007 % end
0008 % load /wbc/Data/_param/_parameters %parameters structure
0009 % iPath = '\Research\NKT\Data\2009-04-23_control_dance_floor_observation\';
0010 % iPath = '\Research\WBC\Data\';
0011 % iPath = '\Research\RBC\Data\';
0012 % P = initParam('wbc');
0013 load _param/_WBCparameters
0014 
0015 load(strcat(iFolder,'/_frames.mat'));
0016 % mkdir('TR');
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 % firstFr = 1;
0030 % lastFr = length(iFiles);
0031 if (remote == 0 || remote ==2)
0032     f1= figure();
0033 end
0034 % For the first frame, start a new track for each cell
0035 while (isempty(frames(firstFr).Cells)) && (firstFr < lastFr)
0036     firstFr = firstFr + 1;
0037 end
0038 
0039 
0040 %% TRACKING
0041 
0042 % Initialize tracking
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 % Predict track based on the initialization
0106 for tr = 1: length(tracks)
0107     tracks(tr) = kf.predict(tracks(tr));
0108 end
0109 
0110 % Consider each frame
0111 for fr = firstFr+1:lastFr
0112     feature =({});
0113 %     fprintf('\n\tTracking frame %3.0f ... ', fr);
0114 %     tic;
0115 %     imID = frames(fr).Id;
0116 %     set(gcf,'Name',['Frame ' num2str(imID)]);
0117 %     bg = imread (strcat('BD/',iFiles(fr).name));
0118 
0119     
0120     %% PLOT for FR image
0121 %     if (remote == 0)
0122 %         if (mod(fr,P.tFrame_CloseFigure)==0)
0123 %             close(f1);
0124 %             f1 = figure();
0125 %         end
0126 %
0127 %
0128 %         imshow(bg);hold on;
0129 %         % Plot the detections
0130 %         plotFrame(frames(fr),'y',P);
0131 %         % Plot the predictions
0132 %         for tr = 1: length(tracks)
0133 %             tracks(tr).plotPredict('c',P);
0134 %         end
0135 %         imOut = ['TR/','FR-',postfix,'.png'];
0136 %         fillfigure(f);
0137 %
0138 %         exportfigure(imOut,f1,size(bg));
0139 %
0140 %     end
0141 
0142     %% Build multiple hypotheses
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         %% Build corresponding table
0153         [H idTable feaTab] = corTable(tracks,frames(fr),mergeCells,splitCells_,splitCells,P);
0154 
0155         %% Greedy Search
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         %% Add cells
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 % 1:1
0173                             if strcmp(tracks(r).Type,'COLLIDE')==1 % keep colliding
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 % 2:1
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)]; % Save the velocity
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)]; % Save the velocity
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 % 1:2 split
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); % Restore velocity
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); % Restore velocity
0245                             end
0246 
0247                         otherwise % empty
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     %% Update
0269     for tr=1: length(tracks)
0270         tracks(tr) = kf.update(tracks(tr));
0271     end
0272 
0273     %% Start new track for the rest of the new measurement & Area filter.
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     %% Clean up inactive tracks
0285     trTemp= [];
0286     for tr = 1: length(tracks)
0287         % Predict next locations using Kalman
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     %% PLOT for TR image
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         % Plot the current tracks
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         % Save the figure
0318         fillfigure();
0319         imOut = [iFolder,'/TR/',postfix,'.tr.png'];
0320         exportfigure(imOut,f1,size(bg));
0321     end
0322 %     t = toc;
0323 %     fprintf('in %3.1f seconds.', t);
0324 end
0325 
0326 %% Add the remaining tracks
0327 trComplete = [trComplete tracks];
0328 tracks = trComplete;
0329 trComplete = [];
0330 
0331 %% PLOT all tracks
0332 if (remote ==0 || remote ==2)
0333     hold off; imshow(zeros(size(bg))); hold on;
0334     %         trackMap = jet(id+1);
0335     for tr =1: length(tracks)
0336         if tracks(tr).nCells > P.tFrame_PlotFinalTrack
0337             %                 color = trackMap(tracks(tr).Id,:);
0338             tracks(tr).plotAll2D(P);
0339             %             tracks(tr).plotAll3D(color,P);
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 % cd ..
0352 % fileName = strcat('_tracks',mode,'.mat');
0353 % save(fileName,'tracks','tracksR');
0354 % save _features features
0355 end
0356 
0357 
0358

Generated on Thu 17-Mar-2011 14:45:51 by m2html © 2005