Tracklet.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Created by wrede on 25.04.16.
  3. //
  4. #include "Tracklet.h"
  5. #include "../util/Logger.h"
  6. namespace core
  7. {
  8. void Tracklet::Print(std::ostream& os) const
  9. {
  10. os << "Tracklet{\n";
  11. for (auto obj : path_objects_)
  12. {
  13. os << *obj << std::endl;
  14. }
  15. os << "}";
  16. }
  17. Tracklet::Tracklet()
  18. : ObjectData(0)
  19. {
  20. path_objects_ = std::vector<ObjectDataPtr>();
  21. last_frame_index_ = 0;
  22. }
  23. size_t Tracklet::GetFirstFrameIndex() const
  24. {
  25. return GetFrameIndex();
  26. }
  27. size_t Tracklet::GetLastFrameIndex() const
  28. {
  29. return last_frame_index_;
  30. }
  31. void Tracklet::AddPathObject(ObjectDataPtr obj, bool overwrite)
  32. {
  33. // Prevent virtual objects to be added, they should be interpolated later from
  34. // the real detections
  35. if (!obj->IsVirtual())
  36. {
  37. bool inserted = false;
  38. if (!path_objects_.empty())
  39. {
  40. for (auto iter = path_objects_.begin();
  41. iter != path_objects_.end() && !inserted;
  42. ++iter)
  43. {
  44. // If the frame index is the same, either overwrite the value or do nothing
  45. // If the frame index is smaller than the stored, the new object should be
  46. // inserted here to accomplish a frame index order
  47. if ((*iter)->GetFrameIndex() == obj->GetFrameIndex())
  48. {
  49. if (overwrite)
  50. {
  51. iter = path_objects_.erase(iter);
  52. iter = path_objects_.insert(iter, obj);
  53. }
  54. inserted = true;
  55. }
  56. else if ((*iter)->GetFrameIndex() > obj->GetFrameIndex())
  57. {
  58. iter = path_objects_.insert(iter, obj);
  59. inserted = true;
  60. }
  61. }
  62. }
  63. // If the frame index is higher than all already stored object, the new object is
  64. // simple added at the end to preserve the ascending frame index order
  65. if (!inserted)
  66. {
  67. path_objects_.push_back(obj);
  68. }
  69. // Update the lowest and highest frame index fields
  70. SetFrameIndex(path_objects_.front()->GetFrameIndex());
  71. last_frame_index_ = path_objects_.back()->GetFrameIndex();
  72. }
  73. }
  74. ObjectDataPtr Tracklet::GetPathObject(size_t i)
  75. {
  76. return path_objects_[i];
  77. }
  78. double Tracklet::CompareTo(ObjectDataPtr obj) const
  79. {
  80. TrackletPtr tlt = std::static_pointer_cast<Tracklet>(obj);
  81. ObjectDataPtr this_obj = path_objects_[path_objects_.size() - 1];
  82. ObjectDataPtr that_obj = tlt->path_objects_[0];
  83. return path_objects_[path_objects_.size() - 1]->CompareTo(tlt->path_objects_[0]);
  84. }
  85. bool Tracklet::IsWithinConstraints(ObjectDataPtr obj,
  86. std::unordered_map<std::string, double> & constraints) const
  87. {
  88. TrackletPtr tlt = std::static_pointer_cast<Tracklet>(obj);
  89. return path_objects_[path_objects_.size() - 1]->
  90. IsWithinConstraints(tlt->path_objects_[0], constraints);
  91. }
  92. ObjectDataPtr Tracklet::Interpolate(ObjectDataPtr obj, double fraction) const
  93. {
  94. TrackletPtr tlt = std::static_pointer_cast<Tracklet>(obj);
  95. return path_objects_[path_objects_.size() - 1]->Interpolate(tlt->path_objects_[0], fraction);
  96. }
  97. void Tracklet::Visualize(cv::Mat& image, cv::Scalar& color) const
  98. {
  99. for (auto obj : path_objects_)
  100. {
  101. obj->Visualize(image, color);
  102. }
  103. }
  104. void Tracklet::Visualize(cv::Mat& image, cv::Scalar& color, size_t frame,
  105. size_t predecessor_count, size_t successor_count) const
  106. {
  107. // Prevent negative values because frame is unsigned
  108. predecessor_count = std::min(predecessor_count, frame);
  109. size_t start = (frame - predecessor_count > GetFirstFrameIndex()) ?
  110. frame - predecessor_count : GetFirstFrameIndex();
  111. size_t end = (frame + successor_count < GetLastFrameIndex()) ?
  112. frame + successor_count : GetLastFrameIndex();
  113. for (auto obj : path_objects_)
  114. {
  115. if (obj->GetFrameIndex() >= start && obj->GetFrameIndex() <= end)
  116. {
  117. obj->Visualize(image, color);
  118. }
  119. }
  120. }
  121. //TODO find a better method than linear interpolation
  122. void Tracklet::InterpolateMissingFrames()
  123. {
  124. for (size_t i = 1; i < path_objects_.size(); ++i)
  125. {
  126. size_t gap = path_objects_[i]->GetFrameIndex() - path_objects_[i - 1]->GetFrameIndex();
  127. if (gap > 1)
  128. {
  129. path_objects_.insert(path_objects_.begin() + i,
  130. path_objects_[i - 1]->Interpolate(path_objects_[i], 0.5));
  131. --i;
  132. }
  133. }
  134. }
  135. size_t Tracklet::GetPathObjectCount() const
  136. {
  137. return path_objects_.size();
  138. }
  139. void Tracklet::Flatten()
  140. {
  141. std::vector<ObjectDataPtr> new_path_objects;
  142. for (auto obj : path_objects_)
  143. {
  144. core::TrackletPtr tlt = std::static_pointer_cast<core::Tracklet>(obj);
  145. for (auto intern_obj : tlt->path_objects_)
  146. {
  147. new_path_objects.push_back(intern_obj);
  148. }
  149. }
  150. path_objects_ = new_path_objects;
  151. SetFrameIndex(path_objects_.front()->GetFrameIndex());
  152. last_frame_index_ = path_objects_.back()->GetFrameIndex();
  153. }
  154. void Tracklet::Combine(TrackletPtr other)
  155. {
  156. for (auto obj : other->path_objects_)
  157. {
  158. AddPathObject(obj);
  159. }
  160. }
  161. ObjectDataPtr Tracklet::GetFrameObject(size_t frame_index)
  162. {
  163. for (auto obj : path_objects_)
  164. {
  165. if (obj->GetFrameIndex() == frame_index)
  166. {
  167. return obj;
  168. }
  169. }
  170. return nullptr;
  171. }
  172. }