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