Tracklet.cpp 6.9 KB

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