trajectory.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The code is based on:
  15. // https://github.com/CnybTseng/JDE/blob/master/platforms/common/trajectory.h
  16. // Ths copyright of CnybTseng/JDE is as follows:
  17. // MIT License
  18. #pragma once
  19. #include <vector>
  20. #include <opencv2/opencv.hpp>
  21. namespace PaddleDetection {
  22. typedef enum
  23. {
  24. New = 0,
  25. Tracked = 1,
  26. Lost = 2,
  27. Removed = 3
  28. } TrajectoryState;
  29. class Trajectory;
  30. typedef std::vector<Trajectory> TrajectoryPool;
  31. typedef std::vector<Trajectory>::iterator TrajectoryPoolIterator;
  32. typedef std::vector<Trajectory *>TrajectoryPtrPool;
  33. typedef std::vector<Trajectory *>::iterator TrajectoryPtrPoolIterator;
  34. class TKalmanFilter : public cv::KalmanFilter
  35. {
  36. public:
  37. TKalmanFilter(void);
  38. virtual ~TKalmanFilter(void) {}
  39. virtual void init(const cv::Mat &measurement);
  40. virtual const cv::Mat &predict();
  41. virtual const cv::Mat &correct(const cv::Mat &measurement);
  42. virtual void project(cv::Mat &mean, cv::Mat &covariance) const;
  43. private:
  44. float std_weight_position;
  45. float std_weight_velocity;
  46. };
  47. inline TKalmanFilter::TKalmanFilter(void) : cv::KalmanFilter(8, 4)
  48. {
  49. cv::KalmanFilter::transitionMatrix = cv::Mat::eye(8, 8, CV_32F);
  50. for (int i = 0; i < 4; ++i)
  51. cv::KalmanFilter::transitionMatrix.at<float>(i, i + 4) = 1;
  52. cv::KalmanFilter::measurementMatrix = cv::Mat::eye(4, 8, CV_32F);
  53. std_weight_position = 1/20.f;
  54. std_weight_velocity = 1/160.f;
  55. }
  56. class Trajectory : public TKalmanFilter
  57. {
  58. public:
  59. Trajectory();
  60. Trajectory(cv::Vec4f &ltrb, float score, const cv::Mat &embedding);
  61. Trajectory(const Trajectory &other);
  62. Trajectory &operator=(const Trajectory &rhs);
  63. virtual ~Trajectory(void) {};
  64. static int next_id();
  65. virtual const cv::Mat &predict(void);
  66. virtual void update(Trajectory &traj, int timestamp, bool update_embedding=true);
  67. virtual void activate(int timestamp);
  68. virtual void reactivate(Trajectory &traj, int timestamp, bool newid=false);
  69. virtual void mark_lost(void);
  70. virtual void mark_removed(void);
  71. friend TrajectoryPool operator+(const TrajectoryPool &a, const TrajectoryPool &b);
  72. friend TrajectoryPool operator+(const TrajectoryPool &a, const TrajectoryPtrPool &b);
  73. friend TrajectoryPool &operator+=(TrajectoryPool &a, const TrajectoryPtrPool &b);
  74. friend TrajectoryPool operator-(const TrajectoryPool &a, const TrajectoryPool &b);
  75. friend TrajectoryPool &operator-=(TrajectoryPool &a, const TrajectoryPool &b);
  76. friend TrajectoryPtrPool operator+(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b);
  77. friend TrajectoryPtrPool operator+(const TrajectoryPtrPool &a, TrajectoryPool &b);
  78. friend TrajectoryPtrPool operator-(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b);
  79. friend cv::Mat embedding_distance(const TrajectoryPool &a, const TrajectoryPool &b);
  80. friend cv::Mat embedding_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b);
  81. friend cv::Mat embedding_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b);
  82. friend cv::Mat mahalanobis_distance(const TrajectoryPool &a, const TrajectoryPool &b);
  83. friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b);
  84. friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b);
  85. friend cv::Mat iou_distance(const TrajectoryPool &a, const TrajectoryPool &b);
  86. friend cv::Mat iou_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b);
  87. friend cv::Mat iou_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b);
  88. private:
  89. void update_embedding(const cv::Mat &embedding);
  90. public:
  91. TrajectoryState state;
  92. cv::Vec4f ltrb;
  93. cv::Mat smooth_embedding;
  94. int id;
  95. bool is_activated;
  96. int timestamp;
  97. int starttime;
  98. float score;
  99. private:
  100. static int count;
  101. cv::Vec4f xyah;
  102. cv::Mat current_embedding;
  103. float eta;
  104. int length;
  105. };
  106. inline cv::Vec4f ltrb2xyah(cv::Vec4f &ltrb)
  107. {
  108. cv::Vec4f xyah;
  109. xyah[0] = (ltrb[0] + ltrb[2]) * 0.5f;
  110. xyah[1] = (ltrb[1] + ltrb[3]) * 0.5f;
  111. xyah[3] = ltrb[3] - ltrb[1];
  112. xyah[2] = (ltrb[2] - ltrb[0]) / xyah[3];
  113. return xyah;
  114. }
  115. inline Trajectory::Trajectory() :
  116. state(New), ltrb(cv::Vec4f()), smooth_embedding(cv::Mat()), id(0),
  117. is_activated(false), timestamp(0), starttime(0), score(0), eta(0.9), length(0)
  118. {
  119. }
  120. inline Trajectory::Trajectory(cv::Vec4f &ltrb_, float score_, const cv::Mat &embedding) :
  121. state(New), ltrb(ltrb_), smooth_embedding(cv::Mat()), id(0),
  122. is_activated(false), timestamp(0), starttime(0), score(score_), eta(0.9), length(0)
  123. {
  124. xyah = ltrb2xyah(ltrb);
  125. update_embedding(embedding);
  126. }
  127. inline Trajectory::Trajectory(const Trajectory &other):
  128. state(other.state), ltrb(other.ltrb), id(other.id), is_activated(other.is_activated),
  129. timestamp(other.timestamp), starttime(other.starttime), xyah(other.xyah),
  130. score(other.score), eta(other.eta), length(other.length)
  131. {
  132. other.smooth_embedding.copyTo(smooth_embedding);
  133. other.current_embedding.copyTo(current_embedding);
  134. // copy state in KalmanFilter
  135. other.statePre.copyTo(cv::KalmanFilter::statePre);
  136. other.statePost.copyTo(cv::KalmanFilter::statePost);
  137. other.errorCovPre.copyTo(cv::KalmanFilter::errorCovPre);
  138. other.errorCovPost.copyTo(cv::KalmanFilter::errorCovPost);
  139. }
  140. inline Trajectory &Trajectory::operator=(const Trajectory &rhs)
  141. {
  142. this->state = rhs.state;
  143. this->ltrb = rhs.ltrb;
  144. rhs.smooth_embedding.copyTo(this->smooth_embedding);
  145. this->id = rhs.id;
  146. this->is_activated = rhs.is_activated;
  147. this->timestamp = rhs.timestamp;
  148. this->starttime = rhs.starttime;
  149. this->xyah = rhs.xyah;
  150. this->score = rhs.score;
  151. rhs.current_embedding.copyTo(this->current_embedding);
  152. this->eta = rhs.eta;
  153. this->length = rhs.length;
  154. // copy state in KalmanFilter
  155. rhs.statePre.copyTo(cv::KalmanFilter::statePre);
  156. rhs.statePost.copyTo(cv::KalmanFilter::statePost);
  157. rhs.errorCovPre.copyTo(cv::KalmanFilter::errorCovPre);
  158. rhs.errorCovPost.copyTo(cv::KalmanFilter::errorCovPost);
  159. return *this;
  160. }
  161. inline int Trajectory::next_id()
  162. {
  163. ++count;
  164. return count;
  165. }
  166. inline void Trajectory::mark_lost(void)
  167. {
  168. state = Lost;
  169. }
  170. inline void Trajectory::mark_removed(void)
  171. {
  172. state = Removed;
  173. }
  174. } // namespace PaddleDetection