Particle Filter Code
Particle Filter Code
% function predictNewLocationsOfTracks(mask,centroids)
% Use the Particle filter to predict the centroid of each
% track in the current frame, and update its bounding box accordingly.
% Inputs:
% mask: array
% centroids: array
% Outputs:
%
function predictNewLocationsOfTracks(mask,centroids)
global obj;
global tracks;
for i = 1:length(tracks)
bbox = tracks(i).bbox;
% Predict the current location of the track.
tracks(i).particles = pfDiffusion(tracks(i).particles,mask);
tracks(i).particles = pfResample(tracks(i).particles,mask,centroids);
predictedCentroid = mean(tracks(i).particles);
% Shift the bounding box so that its center is at
% the predicted location.
predictedCentroid = int32(predictedCentroid(1:2)) - bbox(3:4) / 2;
tracks(i).bbox = [predictedCentroid, bbox(3:4)];
end
end
% Update visibility.
tracks(trackIdx).totalVisibleCount = ...
tracks(trackIdx).totalVisibleCount + 1;
tracks(trackIdx).consecutiveInvisibleCount = 0;
end
end
% function tracks=updateUnassignedTracks(unassignedTracks)
% Mark each unassigned track as invisible, and increase its age by 1.
% Inputs:
% unassignedTracks: array
% Outputs:
% tracks: struct
function tracks=updateUnassignedTracks(unassignedTracks)
global obj;
global tracks;
for i = 1:length(unassignedTracks)
ind = unassignedTracks(i);
tracks(ind).age = tracks(ind).age + 1;
tracks(ind).consecutiveInvisibleCount = ...
tracks(ind).consecutiveInvisibleCount + 1;
end
end
% function deleteLostTracks()
% deletes tracks that have been invisible for too many consecutive frames
% Inputs:
%
% Outputs:
%
function deleteLostTracks()
global obj;
global tracks;
if isempty(tracks)
return;
end
invisibleForTooLong = 30;
ageThreshold = 8;
% Compute the fraction of the track's age for which it was visible.
ages = [tracks(:).age];
totalVisibleCounts = [tracks(:).totalVisibleCount];
visibility = totalVisibleCounts ./ ages;
for i = 1:size(centroids, 1)
centroid = centroids(i,:);
bbox = bboxes(i, :);
minVisibleCount = 8;
if ~isempty(tracks)
for i=1:length(tracks)
if reliableTrackInds(i) == 1 && tracks(i).showId == 0
tracks(i).showId = showId;
showId = showId + 1;
end
end
% Get ids.
ids = int32([reliableTracks(:).showId]);
if(Particles(i,2)>size(mask,1)-1)
Particles(i,2) = size(mask,1);
end
if(Particles(i,1)<1)
Particles(i,1) = 1;
end
if(Particles(i,2)<1)
Particles(i,2) = 1;
end
end
end
if ~sum(W)
return;
end
W=W/sum(W);
cdf = cumsum(W);
M = size(Particles,1);
r_0 = rand / M;
for m = 1 : M
i = find(cdf >= r_0,1,'first');
Particles(m,1) = Particles(i,1);
Particles(m,2) = Particles(i,2);
r_0 = r_0 + 1/M;
end
end