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/>.
25 #include "mtp_tracker.h"
31 void MTPTracker::free() {
32 delete[] _edge_lengths;
34 deallocate_array<scalar_t>(detection_scores);
35 deallocate_array<int>(allowed_motion);
40 void MTPTracker::allocate(int t, int l) {
46 detection_scores = allocate_array<scalar_t>(nb_time_steps, nb_locations);
47 allowed_motion = allocate_array<int>(nb_locations, nb_locations);
49 entrances = new int[nb_locations];
50 exits = new int[nb_locations];
52 for(int l = 0; l < nb_locations; l++) {
55 for(int m = 0; m < nb_locations; m++) {
56 allowed_motion[l][m] = 0;
60 for(int t = 0; t < nb_time_steps; t++) {
61 for(int l = 0; l < nb_locations; l++) {
62 detection_scores[t][l] = 0.0;
66 force_empty_first_frame = 0;
67 force_empty_last_frame = 0;
73 void MTPTracker::write(ostream *os) {
74 (*os) << nb_locations << " " << nb_time_steps << endl;
78 for(int l = 0; l < nb_locations; l++) {
79 for(int m = 0; m < nb_locations; m++) {
80 (*os) << allowed_motion[l][m];
81 if(m < nb_locations - 1) (*os) << " "; else (*os) << endl;
87 (*os) << force_empty_first_frame << " " << force_empty_last_frame << endl;
91 for(int l = 0; l < nb_locations; l++) {
92 (*os) << entrances[l];
93 if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
98 for(int l = 0; l < nb_locations; l++) {
100 if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
105 for(int t = 0; t < nb_time_steps; t++) {
106 for(int l = 0; l < nb_locations; l++) {
107 (*os) << detection_scores[t][l];
108 if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
113 void MTPTracker::read(istream *is) {
120 for(int l = 0; l < nb_locations; l++) {
121 for(int m = 0; m < nb_locations; m++) {
122 (*is) >> allowed_motion[l][m];
126 (*is) >> force_empty_first_frame >> force_empty_last_frame;
128 for(int l = 0; l < nb_locations; l++) {
129 (*is) >> entrances[l];
132 for(int l = 0; l < nb_locations; l++) {
136 for(int t = 0; t < nb_time_steps; t++) {
137 for(int l = 0; l < nb_locations; l++) {
138 (*is) >> detection_scores[t][l];
143 void MTPTracker::write_trajectories(ostream *os) {
144 (*os) << nb_trajectories() << endl;
145 for(int t = 0; t < nb_trajectories(); t++) {
147 << " " << trajectory_entrance_time(t)
148 << " " << trajectory_duration(t)
149 << " " << trajectory_score(t);
150 for(int u = 0; u < trajectory_duration(t); u++) {
151 (*os) << " " << trajectory_location(t, u);
157 MTPTracker::MTPTracker() {
161 detection_scores = 0;
171 MTPTracker::~MTPTracker() {
172 delete[] _edge_lengths;
174 deallocate_array<scalar_t>(detection_scores);
175 deallocate_array<int>(allowed_motion);
180 int MTPTracker::early_pair_node(int t, int l) {
181 return 1 + (2 * t + 0) * nb_locations + l;
184 int MTPTracker::late_pair_node(int t, int l) {
185 return 1 + (2 * t + 1) * nb_locations + l;
188 void MTPTracker::build_graph() {
189 // Delete the existing graph if there was one
190 delete[] _edge_lengths;
193 int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
195 for(int l = 0; l < nb_locations; l++) {
196 if(exits[l]) nb_exits++;
197 if(entrances[l]) nb_entrances++;
198 for(int m = 0; m < nb_locations; m++) {
199 if(allowed_motion[l][m]) nb_motions++;
203 int nb_vertices = 2 + 2 * nb_time_steps * nb_locations;
206 // The edges from the source to the entrances and from the exits
207 // to the sink (in every time frames but the first for the
208 // entrances, and last for the exits)
209 (nb_time_steps - 1) * (nb_exits + nb_entrances) +
210 // The edges for the motions, between every successive frames
211 (nb_time_steps - 1) * nb_motions +
212 // The edges inside the duplicated nodes
213 nb_locations * nb_time_steps;
215 // Edges from the source to the first frame
216 if(force_empty_first_frame) {
217 nb_edges += nb_entrances;
219 nb_edges += nb_locations;
222 // Edges from the last frame to the sink
223 if(force_empty_last_frame) {
224 nb_edges += nb_exits;
226 nb_edges += nb_locations;
229 int *node_from = new int[nb_edges];
230 int *node_to = new int[nb_edges];
232 int source = 0, sink = nb_vertices - 1;
235 _edge_lengths = new scalar_t[nb_edges];
237 // We put the in-node edges first, since these are the ones whose
238 // lengths we will have to change before tracking, according to the
241 for(int t = 0; t < nb_time_steps; t++) {
242 for(int l = 0; l < nb_locations; l++) {
243 node_from[e] = early_pair_node(t, l);
244 node_to[e] = late_pair_node(t, l);
249 // The edges from the source to the first time frame
251 for(int l = 0; l < nb_locations; l++) {
252 if(!force_empty_first_frame || entrances[l]) {
253 node_from[e] = source;
254 node_to[e] = 1 + l + 0 * nb_locations;
255 _edge_lengths[e] = 0.0;
260 // The edges from the last frame to the sink
262 for(int l = 0; l < nb_locations; l++) {
263 if(!force_empty_last_frame || exits[l]) {
264 node_from[e] = late_pair_node(nb_time_steps - 1, l);
266 _edge_lengths[e] = 0.0;
271 // The edges between frames, corresponding to allowed motions
273 for(int t = 0; t < nb_time_steps - 1; t++) {
274 for(int l = 0; l < nb_locations; l++) {
275 for(int k = 0; k < nb_locations; k++) {
276 if(allowed_motion[l][k]) {
277 node_from[e] = late_pair_node(t, l);
278 node_to[e] = early_pair_node(t+1, k);
279 _edge_lengths[e] = 0.0;
286 // The edges from the source to the entrances, and from the exits to
289 for(int t = 0; t < nb_time_steps; t++) {
290 for(int l = 0; l < nb_locations; l++) {
291 if(t > 0 && entrances[l]) {
292 node_from[e] = source;
293 node_to[e] = early_pair_node(t, l);
294 _edge_lengths[e] = 0.0;
297 if(t < nb_time_steps - 1 && exits[l]) {
298 node_from[e] = late_pair_node(t, l);
300 _edge_lengths[e] = 0.0;
306 // We are done, build the graph
308 _graph = new MTPGraph(nb_vertices, nb_edges,
316 void MTPTracker::print_graph_dot(ostream *os) {
318 for(int t = 0; t < nb_time_steps; t++) {
319 for(int l = 0; l < nb_locations; l++) {
320 _edge_lengths[e++] = - detection_scores[t][l];
323 _graph->print_dot(os);
326 void MTPTracker::track() {
330 for(int t = 0; t < nb_time_steps; t++) {
331 for(int l = 0; l < nb_locations; l++) {
332 _edge_lengths[e++] = - detection_scores[t][l];
336 _graph->find_best_paths(_edge_lengths);
337 _graph->retrieve_disjoint_paths();
340 for(int p = 0; p < _graph->nb_paths; p++) {
341 Path *path = _graph->paths[p];
342 cout << "PATH " << p << " [length " << path->nb_nodes << "] " << path->nodes[0];
343 for(int n = 1; n < path->nb_nodes; n++) {
344 cout << " -> " << path->nodes[n];
351 int MTPTracker::nb_trajectories() {
352 return _graph->nb_paths;
355 scalar_t MTPTracker::trajectory_score(int k) {
356 return -_graph->paths[k]->length;
359 int MTPTracker::trajectory_entrance_time(int k) {
360 return (_graph->paths[k]->nodes[1] - 1) / (2 * nb_locations);
363 int MTPTracker::trajectory_duration(int k) {
364 return (_graph->paths[k]->nb_nodes - 2) / 2;
367 int MTPTracker::trajectory_location(int k, int time_from_entry) {
368 return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % nb_locations;