main content

tracking a flock of birds -凯发k8网页登录

this example shows how to track a large number of objects. a large flock of birds is generated and a global nearest neighbor multi-object tracker, trackergnn, is used to estimate the motion of every bird in the flock.

scenario definition

the flock motion is simulated using the behavioral model proposed by reynolds [1]. in this example, the flock is comprised of 1000 simulated birds, called boids, whose initial position and velocity was previously saved. they follow the three rules of flocking: collision avoidance, velocity matching, and flock centering. each rule is associated with a weight and the overall behavior of the flock emerges from the relative weighting of each rule. in this case, weights are chosen that cause the flock to fly around a certain point and create a dense center. other weight settings can cause different behaviors to emerge.

tracking such a large and dense flock presents two challenges:

  1. how to efficiently track 1000 boids?

  2. how to be able to track individual boids in such a dense environment?

the following code simulates the flock behavior for 100 steps of 0.1 second. the plot on the left shows the flock as a whole and the plot on the right is zoomed in on the densest part at the flock center.

s = rng;   % keep the current state of the random number generator
rng(2019); % set the random number generator for repeatable results
load("initialflock.mat","x","v");
flock = helperflock("numboids",size(x,1),"collisionaviodanceweight",0.5,...
    "velocitymatchingweight",0.1,"flockcenteringweight",0.5,"velocity",v,...
    "position",x,"boidacceleration",1);
trulabels = string(num2str((1:flock.numboids)'));
bound = 20;
flockcenter = mean(x,1);
[tp1,tp2] = helpercreatedisplay(x,bound);
% simulate 100 steps of flocking
numsteps = 100;
allx = repmat(x,[1 1 numsteps]);
dt = 0.1;
for i = 1:numsteps
    [x,v] = move(flock,dt);
    allx(:,:,i) = x;
    plottrack(tp1.plotters(1),x)
    inview = findinview(x,-bound flockcenter,bound flockcenter);
    plottrack(tp2.plotters(1),x(inview,:),trulabels(inview))
    drawnow
end

tracker definition

you define the tracker as shown in the example how to efficiently track large numbers of objects.

you observe that the boids follow a curved path and choose a constant turn model defined by initctekf.

to limit the time required to calculate cost, you reduce the coarse cost calculation threshold in the assignmentthreshold to a low value.

further, you choose the more efficient jonker-volgenant as the assignment algorithm, instead of the default munkres algorithm.

you want tracks to be quickly confirmed and deleted, and set the confirmation and deletion thresholds to [2 3] and [2 2], respectively.

finally, you know that the sensor scans only a fraction of the flock at any given scan, and so you set the hasdetectabletrackidsinput to true to be able to pass the detectable track ids to the tracker.

the following line shows how the tracker is configured with the above properties. you can see how to generate code for a tracker in how to generate c code for a tracker, and the tracker for this example is saved in the function flocktracker_kernel.m

% tracker = trackergnn("filterinitializationfcn",@initctekf,"maxnumtracks",1500,...
%         "assignmentthreshold",[50 800],"assignment","jonker-volgenant",...
%         "confirmationthreshold",[2 3],"deletionthreshold",[2 2],...
%         "hasdetectabletrackidsinput",true);

track the flock

next, you run the scenario and track the flock.

a simplified sensor model is simulated using the detectflock supporting function. it simulates a sensor that scans the flock from left to right, and captures a fifth of the flock span in the x-axis in every scan. the sensor has a 0.98 probability of detection and the noise is simulated using a normal distribution with a standard deviation of 0.1 meters about each position component.

the sensor reports its currentscan bounds, which are used to provide the detectable track ids to the tracker.

clear flocktracker_kernel
positionselector = [1 0 0 0 0 0 0; 0 0 1 0 0 0 0; 0 0 0 0 0 1 0];
trackids = zeros(0,1,'uint32');
trax = zeros(0,3);
bounds = inf(3,2);
alltrax = zeros(size(allx));
allids = repmat({},1,numsteps);
trup2 = tp2.plotters(1);
trap2 = tp2.plotters(2);
trup2.historydepth = 2*trap2.historydepth;
clearplotterdata(tp1)
clearplotterdata(tp2)
for i = 1:numsteps
    t = i*dt;
    [detections, currentscan] = detectflock(allx(:,:,i),t);
    bounds(1,:) = currentscan;
    tracksinscan = findinview(trax,bounds(:,1),bounds(:,2));
    [tracks,info] = flocktracker_kernel(detections,t,trackids(tracksinscan,1));
    trax = gettrackpositions(tracks,positionselector);
    if ~isempty(tracks)
        trackids = uint32([tracks.trackid]');
    else
        trackids = zeros(0,1,'uint32');
    end
    alltrax(1:size(trax,1),1:3,i) = trax;
    allids{i} = string(trackids);
    helpervisualizedisplay(tp1,tp2,trulabels,allx,allids,alltrax,i)
end
rng(s); % reset the random number generator to its previous state

result of the tracker in generated code

the following gif shows the performance of the tracker in a mex file.

summary

this example showed how to track a large number of objects in a realistic scenario, where a scanning sensor only reports a fraction of the objects in each scan. the example showed how to set the tracker up for large number of objects and how to use the detectable track ids input to prevent tracks from being deleted.

references

[1] craig w. reynolds, "flocks, herds, and schools: a behavioral model", computer graphics, vol. 21, number 4, july 1987.

supporting functions

helpercreatedisplay

the function creates the example display and returns a handle to both theater plots.

function [tp1,tp2] = helpercreatedisplay(x,bound)
f  = figure("visible", "off");
set(f,"position",[1 1 1425 700]);
movegui(f,"center")
h1 = uipanel(f,"fontsize",12,"position",[.01 .01 .48 .98],"title","flock view");
h2 = uipanel(f,"fontsize",12,"position",[.51 .01 .48 .98],"title","flock center");
flockcenter = mean(x,1);
a1 = axes(h1,'position',[0.05 0.05 0.9 0.9]);
grid(a1,'on')
tp1 = theaterplot("parent",a1);
% flock view (truncated)
halfspan = 250;
tp1.xlimits = 100*round([-halfspan flockcenter(1) halfspan flockcenter(1)]/100);
tp1.ylimits = 100*round([-halfspan flockcenter(2) halfspan flockcenter(2)]/100);
tp1.zlimits = 100*round([-halfspan flockcenter(3) halfspan flockcenter(3)]/100);
trackplotter(tp1,"displayname","truth","historydepth",0,"marker","^","markersize",4,"connecthistory","off");
set(findall(a1,"type","line","tag","tptrackhistory_truth"),"color","k");
view(a1,3)
legend('location','northeast')
% flock center
a2 = axes(h2,'position',[0.05 0.05 0.9 0.9]);
grid(a2,'on')
tp2 = theaterplot("parent",a2);
tp2.xlimits = 10*round([-bound flockcenter(1) bound flockcenter(1)]/10);
tp2.ylimits = 10*round([-bound flockcenter(2) bound flockcenter(2)]/10);
tp2.zlimits = 10*round([-bound flockcenter(3) bound flockcenter(3)]/10);
trackplotter(tp2,"displayname","truth","historydepth",0,...
    "marker","^","markersize",6,"connecthistory","off","fontsize",1);
set(findall(a2,"type","line","tag","tptrackhistory_truth"),"color","k");
% track plotters
trackcolor = [0 0.4470 0.7410]; % blue
tracklength = 50;
trackplotter(tp1,"displayname","tracks","historydepth",tracklength,"connecthistory","off",...
    "marker",".","markersize",3,"markeredgecolor",trackcolor,"markerfacecolor",trackcolor);
set(findall(tp1.parent,"type","line","tag","tptrackhistory_tracks"),...
    "color",trackcolor,"markersize",3,"markeredgecolor",trackcolor);
trackplotter(tp2,"displayname","tracks","historydepth",tracklength,"connecthistory","on",...
    "marker","s","markersize",8,"markeredgecolor",trackcolor,"markerfacecolor","none","fontsize",1);
set (findall(tp2.parent,"type","line","tag","tptrackpositions_tracks"),"linewidth",2);
set(findall(tp2.parent,"type","line","tag","tptrackhistory_tracks"),"color",trackcolor,"linewidth",1);
view(a2,3)
legend('location','northeast')
set(f,'visible','on')
end

detectflock

the function simulates the sensor model. it returns an array of detections and the current sensor scan limits.

function [detections,scanlimits] = detectflock(x,t)
persistent sigma alldetections currentscan numscans
numboids = size(x,1);
pd = 0.98;
if isempty(sigma)
    sigma = 0.1;
    onedet = objectdetection(0,[0;0;0],"measurementnoise",sigma,'objectattributes',struct);
    alldetections = repmat(onedet,numboids,1);
    currentscan = 1;
    numscans = 5;
end
% vectorized calculation of all the detections
x = x   sigma*randn(size(x));
[alldetections.time] = deal(t);
y = mat2cell(x',3,ones(1,size(x,1)));
[alldetections.measurement] = deal(y{:});
% limit the coverage area based on the number of scans
flockxspan = [min(x(:,1),[],1),max(x(:,1),[],1)];
spanperscan = (flockxspan(2)-flockxspan(1))/numscans;
scanlimits = flockxspan(1)   spanperscan * [(currentscan-1) currentscan];
inds = and(x(:,1)>=scanlimits(1), x(:,1)<=scanlimits(2));
% add pd
draw = rand(size(inds));
inds = inds & (draw% promote the scan count
currentscan = currentscan 1;
if currentscan > numscans
    currentscan = 1;
end
end

findinview

the function returns a logical array for positions that fall within the limits of minbound and maxbound.

function inview = findinview(x,minbound,maxbound)
inview = false(size(x,1),1);
inview(:) = (x(:,1)>minbound(1) & x(:,1)...
         (x(:,2)>minbound(2) & x(:,2)...
         (x(:,3)>minbound(3) & x(:,3)end

helpervisualizedisplay

the function displays the flock and tracks after tracking.

function helpervisualizedisplay(tp1,tp2,trulabels,allx,allids,alltrax,i)
trup1 = tp1.plotters(1);
trap1 = tp1.plotters(2);
trup2 = tp2.plotters(1);
trap2 = tp2.plotters(2);
plottrack(trup1,allx(:,:,i))
n = numel(allids{i});
plottrack(trap1,alltrax(1:n,:,i))
bounds = [tp2.xlimits;tp2.ylimits;tp2.zlimits];
inview = findinview(allx(:,:,i),bounds(:,1),bounds(:,2));
plottrack(trup2,allx(inview,:,i),trulabels(inview))
inview = findinview(alltrax(1:n,:,i),bounds(:,1),bounds(:,2));
plottrack(trap2,alltrax(inview,:,i),allids{i}(inview))
drawnow
end
网站地图