Berclaz.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // Created by wrede on 02.06.16.
  3. //
  4. #include "Berclaz.h"
  5. #include "../util/Parser.h"
  6. #include "../util/Logger.h"
  7. #include "KShortestPaths.h"
  8. namespace algo
  9. {
  10. Berclaz::Berclaz(int h_res, int v_res, int vicinity_size)
  11. {
  12. h_res_ = h_res;
  13. v_res_ = v_res;
  14. vicinity_size_ = vicinity_size;
  15. }
  16. void Berclaz::CreateGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, util::Grid& grid)
  17. {
  18. util::Logger::LogDebug("add vertices");
  19. // Add grid vertices
  20. for (int z = 0; z < grid.GetDepthCount(); ++z)
  21. {
  22. for (int y = 0; y < grid.GetHeightCount(); ++y)
  23. {
  24. for (int x = 0; x < grid.GetWidthCount(); ++x)
  25. {
  26. boost::add_vertex(grid.GetValue(x, y, z), graph);
  27. }
  28. }
  29. }
  30. // Add source and sink vertex
  31. source = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  32. sink = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  33. util::Logger::LogDebug("add edges");
  34. double min_weight = std::numeric_limits<double>::max();
  35. double max_weight = std::numeric_limits<double>::lowest();
  36. double min_score = std::numeric_limits<double>::max();
  37. double max_score = std::numeric_limits<double>::lowest();
  38. // Iterate all vertices but source and sink
  39. VertexIndexMap vertices = boost::get(boost::vertex_index, graph);
  40. VertexValueMap values = boost::get(boost::vertex_name, graph);
  41. int layer_size = grid.GetWidthCount() * grid.GetHeightCount();
  42. for (int z = 0; z < grid.GetDepthCount(); ++z)
  43. {
  44. for (int y = 0; y < grid.GetHeightCount(); ++y)
  45. {
  46. for (int x = 0; x < grid.GetWidthCount(); ++x)
  47. {
  48. // First vertex index
  49. int vi = x + y * grid.GetWidthCount() + z * layer_size;
  50. // Get the score, clamp it, prevent division by zero and
  51. // logarithm of zero
  52. double score = values[vi]->GetDetectionScore();
  53. if (score < min_score)
  54. min_score = score;
  55. if (score > max_score)
  56. max_score = score;
  57. if (score > MAX_SCORE_VALUE)
  58. {
  59. score = MAX_SCORE_VALUE;
  60. }
  61. else if (score < MIN_SCORE_VALUE)
  62. {
  63. score = MIN_SCORE_VALUE;
  64. }
  65. // Calculate the edge weight
  66. double weight = -std::log(score / (1 - score));
  67. if (weight < min_weight)
  68. min_weight = weight;
  69. if (weight > max_weight)
  70. max_weight = weight;
  71. // Connect with the next frame only if there is a next frame
  72. if (z < grid.GetDepthCount() - 1)
  73. {
  74. // Iterate all nearby cells in the next frame
  75. for (int ny = std::max(0, y - vicinity_size_);
  76. ny < std::min(grid.GetHeightCount(), y + vicinity_size_ + 1);
  77. ++ny)
  78. {
  79. for (int nx = std::max(0, x - vicinity_size_);
  80. nx < std::min(grid.GetWidthCount(), x + vicinity_size_ + 1);
  81. ++nx)
  82. {
  83. // Second vertex index
  84. int vj = nx + ny * grid.GetWidthCount() + (z + 1) * layer_size;
  85. // Connect to nearby cells
  86. boost::add_edge(vertices[vi], vertices[vj], weight, graph);
  87. }
  88. }
  89. boost::add_edge(vertices[vi], sink, VIRTUAL_EDGE_WEIGHT, graph);
  90. }
  91. else
  92. {
  93. boost::add_edge(vertices[vi], sink, weight, graph);
  94. }
  95. // Connect with source and sink
  96. boost::add_edge(source, vertices[vi], VIRTUAL_EDGE_WEIGHT, graph);
  97. }
  98. }
  99. }
  100. util::Logger::LogDebug("weight " + std::to_string(min_weight) + " to " + std::to_string(max_weight));
  101. util::Logger::LogDebug("score " + std::to_string(min_score) + " to " + std::to_string(max_score));
  102. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  103. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  104. }
  105. void Berclaz::ExtractTracks(DirectedGraph& graph, MultiPredecessorMap& map, Vertex origin,
  106. std::vector<core::TrackletPtr>& tracks)
  107. {
  108. VertexValueMap values = boost::get(boost::vertex_name, graph);
  109. // Move along all paths in reverse, starting at the origin
  110. for (Vertex first : map[origin])
  111. {
  112. core::TrackletPtr tracklet(new core::Tracklet());
  113. // The paths are node disjoint, so there should always be only one
  114. // node to proceed to
  115. for (Vertex u = first, v = (*map[u].begin());
  116. u != v; u = v, v = (*map[v].begin()))
  117. {
  118. tracklet->AddPathObject(values[u]);
  119. }
  120. tracks.push_back(tracklet);
  121. }
  122. }
  123. void Berclaz::Run(core::DetectionSequence& sequence,
  124. size_t batch_size, size_t max_track_count,
  125. std::vector<core::TrackletPtr> & tracks, util::Filter2D & filter)
  126. {
  127. size_t batch_count = 0;
  128. for (size_t i = sequence.GetFrameOffset(); i < sequence.GetFrameCount(); i += batch_size)
  129. {
  130. util::Logger::LogDebug("batch offset: " + std::to_string(i));
  131. util::Grid grid = util::Parser::ParseGrid(sequence, i, i + batch_size,
  132. 0.0, 1.0, h_res_, 0.0, 1.0, v_res_);
  133. grid.Convolve2D(filter);
  134. util::Logger::LogDebug("create graph");
  135. DirectedGraph graph;
  136. Vertex source, sink;
  137. CreateGraph(graph, source, sink, grid);
  138. util::Logger::LogDebug("run ksp");
  139. KShortestPaths ksp(graph, source, sink);
  140. ksp.Run(max_track_count);
  141. util::Logger::LogDebug("get paths");
  142. std::vector<std::vector<Vertex>> paths;
  143. ksp.GetPaths(paths);
  144. util::Logger::LogDebug("extract tracks");
  145. VertexValueMap values = boost::get(boost::vertex_name, graph);
  146. util::Logger::LogDebug("");
  147. for (auto p : paths)
  148. {
  149. std::string path_string = "";
  150. core::TrackletPtr tlt(new core::Tracklet());
  151. for (auto v : p)
  152. {
  153. path_string += std::to_string(v) + " ";
  154. tlt->AddPathObject(values[v]);
  155. }
  156. tracks.push_back(tlt);
  157. util::Logger::LogDebug(path_string);
  158. }
  159. util::Logger::LogDebug("");
  160. batch_count++;
  161. }
  162. // Only connect tracks if the sequence was split
  163. if (batch_count > 1)
  164. {
  165. //TODO find a better way to connect tracks (n-stage)
  166. util::Logger::LogDebug("connect tracks");
  167. ConnectTracks(tracks);
  168. }
  169. }
  170. void Berclaz::ConnectTracks(std::vector<core::TrackletPtr>& tracks)
  171. {
  172. for (size_t i = 0; i < tracks.size(); ++i)
  173. {
  174. // find the best matching tracklet
  175. double best_value = std::numeric_limits<double>::max();
  176. size_t best_index = 0;
  177. for (size_t k = i + 1; k < tracks.size(); ++k)
  178. {
  179. if (tracks[i]->GetLastFrameIndex() < tracks[k]->GetFirstFrameIndex())
  180. {
  181. double value = tracks[i]->CompareTo(tracks[k]);
  182. if (value < best_value)
  183. {
  184. best_index = k;
  185. }
  186. }
  187. }
  188. // if a match was found
  189. if (best_index != 0)
  190. {
  191. // merge the two tracks
  192. tracks[i]->Combine(tracks[best_index]);
  193. tracks.erase(tracks.begin() + best_index);
  194. }
  195. }
  196. }
  197. }