Tracklet.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. return path_objects_[path_objects_.size() - 1]->CompareTo(tlt->path_objects_[0]);
  82. }
  83. bool Tracklet::IsWithinConstraints(ObjectDataPtr obj,
  84. std::unordered_map<std::string, double> & constraints) const
  85. {
  86. TrackletPtr tlt = std::static_pointer_cast<Tracklet>(obj);
  87. return path_objects_[path_objects_.size() - 1]->
  88. IsWithinConstraints(tlt->path_objects_[0], constraints);
  89. }
  90. ObjectDataPtr Tracklet::Interpolate(ObjectDataPtr obj, double fraction) const
  91. {
  92. TrackletPtr tlt = std::static_pointer_cast<Tracklet>(obj);
  93. return path_objects_[path_objects_.size() - 1]->Interpolate(tlt->path_objects_[0], fraction);
  94. }
  95. void Tracklet::Visualize(cv::Mat& image, cv::Scalar& color) const
  96. {
  97. for (auto obj : path_objects_)
  98. {
  99. obj->Visualize(image, color);
  100. }
  101. }
  102. void Tracklet::Visualize(cv::Mat& image, cv::Scalar& color, size_t frame,
  103. size_t predecessor_count, size_t successor_count) const
  104. {
  105. // Prevent negative values because frame is unsigned
  106. predecessor_count = std::min(predecessor_count, frame);
  107. size_t start = (frame - predecessor_count > GetFirstFrameIndex()) ?
  108. frame - predecessor_count : GetFirstFrameIndex();
  109. size_t end = (frame + successor_count < GetLastFrameIndex()) ?
  110. frame + successor_count : GetLastFrameIndex();
  111. for (auto obj : path_objects_)
  112. {
  113. if (obj->GetFrameIndex() >= start && obj->GetFrameIndex() <= end)
  114. {
  115. obj->Visualize(image, color);
  116. }
  117. }
  118. }
  119. //TODO find a better method than linear interpolation
  120. void Tracklet::InterpolateMissingFrames()
  121. {
  122. for (size_t i = 1; i < path_objects_.size(); ++i)
  123. {
  124. size_t gap = path_objects_[i]->GetFrameIndex() - path_objects_[i - 1]->GetFrameIndex();
  125. if (gap > 1)
  126. {
  127. path_objects_.insert(path_objects_.begin() + i,
  128. path_objects_[i - 1]->Interpolate(path_objects_[i], 0.5));
  129. --i;
  130. }
  131. }
  132. }
  133. size_t Tracklet::GetPathObjectCount() const
  134. {
  135. return path_objects_.size();
  136. }
  137. void Tracklet::Flatten()
  138. {
  139. std::vector<ObjectDataPtr> new_path_objects;
  140. for (auto obj : path_objects_)
  141. {
  142. core::TrackletPtr tlt =
  143. std::static_pointer_cast<core::Tracklet>(obj);
  144. for (auto intern_obj : tlt->path_objects_)
  145. {
  146. new_path_objects.push_back(intern_obj);
  147. }
  148. }
  149. path_objects_ = new_path_objects;
  150. SetFrameIndex(path_objects_.front()->GetFrameIndex());
  151. last_frame_index_ = path_objects_.back()->GetFrameIndex();
  152. }
  153. void Tracklet::Combine(TrackletPtr other)
  154. {
  155. for (auto obj : other->path_objects_)
  156. {
  157. AddPathObject(obj);
  158. }
  159. }
  160. ObjectDataPtr Tracklet::GetFrameObject(size_t frame_index)
  161. {
  162. for (auto obj : path_objects_)
  163. {
  164. if (obj->GetFrameIndex() == frame_index)
  165. {
  166. return obj;
  167. }
  168. }
  169. return nullptr;
  170. }
  171. }