3 * mtp is the ``Multi Tracked Paths'', an implementation of the
4 * k-shortest paths algorithm for multi-target tracking.
6 * Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of mtp.
11 * mtp is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * mtp is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 * License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
31 #include "mtp_tracker.h"
33 //////////////////////////////////////////////////////////////////////
35 scalar_t noisy_score(scalar_t true_score, scalar_t erroneous_score,
36 scalar_t score_noise, scalar_t flip_noise) {
37 if(drand48() < flip_noise) {
38 return erroneous_score + score_noise * (2.0f * scalar_t(drand48()) - 1.0f);
40 return true_score + score_noise * (2.0f * scalar_t(drand48()) - 1.0f);
44 int main(int argc, char **argv) {
46 int nb_time_steps = 8;
47 int motion_amplitude = 1;
49 MTPTracker *tracker = new MTPTracker();
51 tracker->allocate(nb_time_steps, nb_locations);
53 // We define the spatial structure by stating what are the possible
54 // motions of targets, and what are the entrances and the exits.
56 // Here our example is a 1D space with motions from any location to
57 // any location less than motion_amplitude away, entrance at
58 // location 0 (or in the first time frame, i.e. targets can already
59 // be in the scene when the sequence starts) and exit at location
60 // nb_locations-1 (or from the last time frame, i.e. targets can
61 // still be present when the sequence finishes)
63 for(int l = 0; l < nb_locations; l++) {
64 for(int m = 0; m < nb_locations; m++) {
65 tracker->allowed_motions[l][m] = abs(l - m) <= motion_amplitude;
69 for(int t = 0; t < nb_time_steps; t++) {
70 for(int l = 0; l < nb_locations; l++) {
71 // We allow targets to enter in the first time frame, or in
73 tracker->entrances[t][l] = (t == 0 || l == 0);
74 // We allow targets to leave from the last time frame, or from
75 // location nb_locations-1
76 tracker->exits[t][l] = (t == nb_time_steps - 1 || l == nb_locations-1);
80 // We construct the graph corresponding to this structure
82 tracker->build_graph();
84 // Then, we specify for every location and time step what is the
85 // detection score there.
87 scalar_t flip_noise = 0.05f;
88 scalar_t score_noise = 0.0f;
90 // We first put a background noise, with negative scores at every
93 for(int t = 0; t < nb_time_steps; t++) {
94 for(int l = 0; l < nb_locations; l++) {
95 tracker->detection_scores[t][l] = noisy_score(-1.0, 1.0, score_noise, flip_noise);
99 // Then we add two targets with a typical tracking local minimum
101 // * Target A moves from location 0 to the middle, stays there for a
102 // while, and comes back. It is strongly detected on the first
105 // * Target B moves from location nb_locations-1 to the middle, stay
106 // there for a while, and comes back. It is strongly detected on
109 int la, lb; // Target locations
110 scalar_t sa, sb; // Target detection scores
111 for(int t = 0; t < nb_time_steps; t++) {
112 if(t < nb_time_steps/2) {
114 lb = nb_locations - 1 - t;
115 sa = noisy_score(10.0, -1.0, score_noise, flip_noise);
116 sb = noisy_score( 1.0, -1.0, score_noise, flip_noise);
118 la = nb_time_steps - 1 - t;
119 lb = t - nb_time_steps + nb_locations;
120 sa = noisy_score( 1.0, -1.0, score_noise, flip_noise);
121 sb = noisy_score(10.0, -1.0, score_noise, flip_noise);
124 if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
125 if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
127 tracker->detection_scores[t][la] = sa;
128 tracker->detection_scores[t][lb] = sb;
131 { // Write down the tracker setting, so that we can use it as an
132 // example for the mtp command line
133 ofstream out_tracker("tracker.dat");
134 tracker->write(&out_tracker);
137 // Performs the tracking per se
141 // Prints the detected trajectories
143 for(int t = 0; t < tracker->nb_trajectories(); t++) {
144 cout << "Trajectory "
146 << " starting at " << tracker->trajectory_entrance_time(t)
147 << ", duration " << tracker->trajectory_duration(t)
148 << ", score " << tracker->trajectory_score(t)
149 << ", through locations";
150 for(int u = 0; u < tracker->trajectory_duration(t); u++) {
151 cout << " " << tracker->trajectory_location(t, u);