Changed the headers to (C) Idiap, and added the gpl-3.0.txt file.
[mtp.git] / mtp_example.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Path'', an implementation of the
4  *  k-shortest path algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 // Multi-Tracked Path
26
27 #include <iostream>
28 #include <fstream>
29
30 using namespace std;
31
32 #include "tracker.h"
33
34 //////////////////////////////////////////////////////////////////////
35
36 scalar_t detection_score(scalar_t a, scalar_t b, scalar_t score_noise, scalar_t flip_noise) {
37   if(drand48() > flip_noise) {
38     return a + score_noise * (2.0 * drand48() - 1.0);
39   } else {
40     return b + score_noise * (2.0 * drand48() - 1.0);
41   }
42 }
43
44 int main(int argc, char **argv) {
45   int nb_locations = 7;
46   int nb_time_steps = 8;
47   int motion_amplitude = 1;
48
49   Tracker *tracker = new Tracker();
50
51   tracker->allocate(nb_time_steps, nb_locations);
52
53   // We define the spatial structures by stating what are the possible
54   // motions of targets, and what are the entrances and the
55   // exits.
56
57   // Here our example is a 1D space with motions from any location to
58   // any location less than motion_amplitude away, entrance at
59   // location 0 and exit at location nb_locations-1.
60
61   for(int l = 0; l < nb_locations; l++) {
62     for(int k = 0; k < nb_locations; k++) {
63       tracker->allowed_motion[l][k] = abs(l - k) <= motion_amplitude;
64     }
65     tracker->entrances[0] = 1;
66     tracker->exits[nb_locations - 1] = 1;
67   }
68
69   // We construct the graph corresponding to this structure
70
71   tracker->build_graph();
72
73   // Then, we specify for every location and time step what is the
74   // detection score there.
75
76   scalar_t flip_noise = 0.05;
77   scalar_t score_noise = 0.0;
78
79   // We first put a background noise, with negative scores at every
80   // location.
81
82   for(int t = 0; t < nb_time_steps; t++) {
83     for(int l = 0; l < nb_locations; l++) {
84       tracker->detection_scores[t][l] = detection_score(-1.0, 1.0, score_noise, flip_noise);
85     }
86   }
87
88   // Then we two targets with the typical local minimum:
89   //
90   // * Target A moves from location 0 to the middle, stays there for a
91   //   while, and comes back, and is strongly detected on the first
92   //   half
93   //
94   // * Target B moves from location nb_locations-1 to the middle, stay
95   //   there for a while, and comes back, and is strongly detected on
96   //   the second half
97
98   int la, lb; // Target locations
99   scalar_t sa, sb; // Target detection scores
100   for(int t = 0; t < nb_time_steps; t++) {
101     if(t < nb_time_steps/2) {
102       la = t;
103       lb = nb_locations - 1 - t;
104       sa = detection_score(10.0, -1.0, score_noise, flip_noise);
105       sb = detection_score( 1.0, -1.0, score_noise, flip_noise);
106     } else {
107       la = nb_time_steps - 1 - t;
108       lb = t - nb_time_steps + nb_locations;
109       sa = detection_score( 1.0, -1.0, score_noise, flip_noise);
110       sb = detection_score(10.0, -1.0, score_noise, flip_noise);
111     }
112
113     if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
114     if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
115
116     tracker->detection_scores[t][la] = sa;
117     tracker->detection_scores[t][lb] = sb;
118   }
119
120   { // Write down the tracker setting
121     ofstream out_tracker("tracker.dat");
122     tracker->write(&out_tracker);
123   }
124
125   // Does the tracking per se
126
127   tracker->track();
128
129   // Prints the detected trajectories
130
131   for(int t = 0; t < tracker->nb_trajectories(); t++) {
132     cout << "Trajectory "
133          << t
134          << " starting at " << tracker->trajectory_entrance_time(t)
135          << ", duration " << tracker->trajectory_duration(t)
136          << ", score " << tracker->trajectory_score(t)
137          << ", through nodes ";
138     for(int u = 0; u < tracker->trajectory_duration(t); u++) {
139       cout << " " << tracker->trajectory_location(t, u);
140     }
141     cout << endl;
142   }
143
144   delete tracker;
145
146   exit(EXIT_SUCCESS);
147 }