52bc4003933873a06695304f4554a6a64ff7b78c
[mtp.git] / mtp_example.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 // Multi-Tracked Path
20
21 #include <iostream>
22 #include <fstream>
23
24 using namespace std;
25
26 #include "tracker.h"
27
28 //////////////////////////////////////////////////////////////////////
29
30 scalar_t detection_score(scalar_t a, scalar_t b, scalar_t score_noise, scalar_t flip_noise) {
31   if(drand48() > flip_noise) {
32     return a + score_noise * (2.0 * drand48() - 1.0);
33   } else {
34     return b + score_noise * (2.0 * drand48() - 1.0);
35   }
36 }
37
38 int main(int argc, char **argv) {
39   int nb_locations = 7;
40   int nb_time_steps = 8;
41   int motion_amplitude = 1;
42
43   Tracker *tracker = new Tracker();
44
45   tracker->allocate(nb_time_steps, nb_locations);
46
47   // We define the spatial structures by stating what are the possible
48   // motions of targets, and what are the entrances and the
49   // exits.
50
51   // Here our example is a 1D space with motions from any location to
52   // any location less than motion_amplitude away, entrance at
53   // location 0 and exit at location nb_locations-1.
54
55   for(int l = 0; l < nb_locations; l++) {
56     for(int k = 0; k < nb_locations; k++) {
57       tracker->allowed_motion[l][k] = abs(l - k) <= motion_amplitude;
58     }
59     tracker->entrances[0] = 1;
60     tracker->exits[nb_locations - 1] = 1;
61   }
62
63   // We construct the graph corresponding to this structure
64
65   tracker->build_graph();
66
67   // Then, we specify for every location and time step what is the
68   // detection score there.
69
70   scalar_t flip_noise = 0.05;
71   scalar_t score_noise = 0.0;
72
73   // We first put a background noise, with negative scores at every
74   // location.
75
76   for(int t = 0; t < nb_time_steps; t++) {
77     for(int l = 0; l < nb_locations; l++) {
78       tracker->detection_scores[t][l] = detection_score(-1.0, 1.0, score_noise, flip_noise);
79     }
80   }
81
82   // Then we two targets with the typical local minimum:
83   //
84   // * Target A moves from location 0 to the middle, stays there for a
85   //   while, and comes back, and is strongly detected on the first
86   //   half
87   //
88   // * Target B moves from location nb_locations-1 to the middle, stay
89   //   there for a while, and comes back, and is strongly detected on
90   //   the second half
91
92   int la, lb; // Target locations
93   scalar_t sa, sb; // Target detection scores
94   for(int t = 0; t < nb_time_steps; t++) {
95     if(t < nb_time_steps/2) {
96       la = t;
97       lb = nb_locations - 1 - t;
98       sa = detection_score(10.0, -1.0, score_noise, flip_noise);
99       sb = detection_score( 1.0, -1.0, score_noise, flip_noise);
100     } else {
101       la = nb_time_steps - 1 - t;
102       lb = t - nb_time_steps + nb_locations;
103       sa = detection_score( 1.0, -1.0, score_noise, flip_noise);
104       sb = detection_score(10.0, -1.0, score_noise, flip_noise);
105     }
106
107     if(la > nb_locations/2 - 1) la = nb_locations/2 - 1;
108     if(lb < nb_locations/2 + 1) lb = nb_locations/2 + 1;
109
110     tracker->detection_scores[t][la] = sa;
111     tracker->detection_scores[t][lb] = sb;
112   }
113
114   { // Write down the tracker setting
115     ofstream out_tracker("tracker.dat");
116     tracker->write(&out_tracker);
117   }
118
119   // Does the tracking per se
120
121   tracker->track();
122
123   // Prints the detected trajectories
124
125   for(int t = 0; t < tracker->nb_trajectories(); t++) {
126     cout << "Trajectory "
127          << t
128          << " starting at " << tracker->trajectory_entrance_time(t)
129          << ", duration " << tracker->trajectory_duration(t)
130          << ", score " << tracker->trajectory_score(t)
131          << ", through nodes ";
132     for(int u = 0; u < tracker->trajectory_duration(t); u++) {
133       cout << " " << tracker->trajectory_location(t, u);
134     }
135     cout << endl;
136   }
137
138   delete tracker;
139
140   exit(EXIT_SUCCESS);
141 }