nndeploy C++ API  0.2.0
nndeploy C++ API
trajectory.h
Go to the documentation of this file.
1 // Copyright (c) 2022 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 
15 // The code is based on:
16 // https://github.com/CnybTseng/JDE/blob/master/platforms/common/trajectory.cpp
17 // Ths copyright of CnybTseng/JDE is as follows:
18 // MIT License
19 // ----------------------------------------------------------------------
20 // Modified by nndeploy on 2025-05-30
21 // ----------------------------------------------------------------------
22 #ifndef _NNDEPLOY_TRACK_TRAJECTORY_H_
23 #define _NNDEPLOY_TRACK_TRAJECTORY_H_
24 
25 #include <opencv2/core/core.hpp>
26 #include <opencv2/highgui/highgui.hpp>
27 #include <opencv2/imgproc/imgproc.hpp>
28 #include <vector>
29 
30 #include "opencv2/video/tracking.hpp"
31 
32 namespace nndeploy {
33 namespace track {
34 
35 typedef enum { New = 0, Tracked = 1, Lost = 2, Removed = 3 } TrajectoryState;
36 
37 class Trajectory;
38 typedef std::vector<Trajectory> TrajectoryPool;
39 typedef std::vector<Trajectory>::iterator TrajectoryPoolIterator;
40 typedef std::vector<Trajectory *> TrajectoryPtrPool;
41 typedef std::vector<Trajectory *>::iterator TrajectoryPtrPoolIterator;
42 
43 class TKalmanFilter : public cv::KalmanFilter {
44  public:
45  TKalmanFilter(void);
46  virtual ~TKalmanFilter(void) {}
47  virtual void init(const cv::Mat &measurement);
48  virtual const cv::Mat &predict();
49  virtual const cv::Mat &correct(const cv::Mat &measurement);
50  virtual void project(cv::Mat *mean, cv::Mat *covariance) const;
51 
52  private:
53  float std_weight_position;
54  float std_weight_velocity;
55 };
56 
57 inline TKalmanFilter::TKalmanFilter(void) : cv::KalmanFilter(8, 4) {
58  cv::KalmanFilter::transitionMatrix = cv::Mat::eye(8, 8, CV_32F);
59  for (int i = 0; i < 4; ++i)
60  cv::KalmanFilter::transitionMatrix.at<float>(i, i + 4) = 1;
61  cv::KalmanFilter::measurementMatrix = cv::Mat::eye(4, 8, CV_32F);
62  std_weight_position = 1 / 20.f;
63  std_weight_velocity = 1 / 160.f;
64 }
65 
66 class Trajectory : public TKalmanFilter {
67  public:
68  Trajectory();
69  Trajectory(const cv::Vec4f &ltrb, float score, const cv::Mat &embedding);
70  Trajectory(const Trajectory &other);
71  Trajectory &operator=(const Trajectory &rhs);
72  virtual ~Trajectory(void) {}
73 
74  int next_id(int &nt);
75  virtual const cv::Mat &predict(void);
76  virtual void update(Trajectory *traj, int timestamp,
77  bool update_embedding = true);
78  virtual void activate(int &cnt, int timestamp);
79  virtual void reactivate(Trajectory *traj, int &cnt, int timestamp,
80  bool newid = false);
81  virtual void mark_lost(void);
82  virtual void mark_removed(void);
83 
85  const TrajectoryPool &b);
87  const TrajectoryPtrPool &b);
89  const TrajectoryPtrPool &b);
91  const TrajectoryPool &b);
93  const TrajectoryPool &b);
95  const TrajectoryPtrPool &b);
97  TrajectoryPool *b);
99  const TrajectoryPtrPool &b);
100 
101  friend cv::Mat embedding_distance(const TrajectoryPool &a,
102  const TrajectoryPool &b);
103  friend cv::Mat embedding_distance(const TrajectoryPtrPool &a,
104  const TrajectoryPtrPool &b);
105  friend cv::Mat embedding_distance(const TrajectoryPtrPool &a,
106  const TrajectoryPool &b);
107 
108  friend cv::Mat mahalanobis_distance(const TrajectoryPool &a,
109  const TrajectoryPool &b);
110  friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a,
111  const TrajectoryPtrPool &b);
112  friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a,
113  const TrajectoryPool &b);
114 
115  friend cv::Mat iou_distance(const TrajectoryPool &a, const TrajectoryPool &b);
116  friend cv::Mat iou_distance(const TrajectoryPtrPool &a,
117  const TrajectoryPtrPool &b);
118  friend cv::Mat iou_distance(const TrajectoryPtrPool &a,
119  const TrajectoryPool &b);
120 
121  private:
122  void update_embedding(const cv::Mat &embedding);
123 
124  public:
126  cv::Vec4f ltrb;
128  int id;
132  float score;
133 
134  private:
135  // int count=0;
136  cv::Vec4f xyah;
137  cv::Mat current_embedding;
138  float eta;
139  int length;
140 };
141 
142 inline cv::Vec4f ltrb2xyah(const cv::Vec4f &ltrb) {
143  cv::Vec4f xyah;
144  xyah[0] = (ltrb[0] + ltrb[2]) * 0.5f;
145  xyah[1] = (ltrb[1] + ltrb[3]) * 0.5f;
146  xyah[3] = ltrb[3] - ltrb[1];
147  xyah[2] = (ltrb[2] - ltrb[0]) / xyah[3];
148  return xyah;
149 }
150 
152  : state(New),
153  ltrb(cv::Vec4f()),
154  smooth_embedding(cv::Mat()),
155  id(0),
156  is_activated(false),
157  timestamp(0),
158  starttime(0),
159  score(0),
160  eta(0.9),
161  length(0) {}
162 
163 inline Trajectory::Trajectory(const cv::Vec4f &ltrb_, float score_,
164  const cv::Mat &embedding)
165  : state(New),
166  ltrb(ltrb_),
167  smooth_embedding(cv::Mat()),
168  id(0),
169  is_activated(false),
170  timestamp(0),
171  starttime(0),
172  score(score_),
173  eta(0.9),
174  length(0) {
175  xyah = ltrb2xyah(ltrb);
176  update_embedding(embedding);
177 }
178 
179 inline Trajectory::Trajectory(const Trajectory &other)
180  : state(other.state),
181  ltrb(other.ltrb),
182  id(other.id),
183  is_activated(other.is_activated),
184  timestamp(other.timestamp),
185  starttime(other.starttime),
186  xyah(other.xyah),
187  score(other.score),
188  eta(other.eta),
189  length(other.length) {
190  other.smooth_embedding.copyTo(smooth_embedding);
191  other.current_embedding.copyTo(current_embedding);
192  // copy state in KalmanFilter
193 
194  other.statePre.copyTo(cv::KalmanFilter::statePre);
195  other.statePost.copyTo(cv::KalmanFilter::statePost);
196  other.errorCovPre.copyTo(cv::KalmanFilter::errorCovPre);
197  other.errorCovPost.copyTo(cv::KalmanFilter::errorCovPost);
198 }
199 
201  this->state = rhs.state;
202  this->ltrb = rhs.ltrb;
203  rhs.smooth_embedding.copyTo(this->smooth_embedding);
204  this->id = rhs.id;
205  this->is_activated = rhs.is_activated;
206  this->timestamp = rhs.timestamp;
207  this->starttime = rhs.starttime;
208  this->xyah = rhs.xyah;
209  this->score = rhs.score;
210  rhs.current_embedding.copyTo(this->current_embedding);
211  this->eta = rhs.eta;
212  this->length = rhs.length;
213 
214  // copy state in KalmanFilter
215 
216  rhs.statePre.copyTo(cv::KalmanFilter::statePre);
217  rhs.statePost.copyTo(cv::KalmanFilter::statePost);
218  rhs.errorCovPre.copyTo(cv::KalmanFilter::errorCovPre);
219  rhs.errorCovPost.copyTo(cv::KalmanFilter::errorCovPost);
220 
221  return *this;
222 }
223 
224 inline int Trajectory::next_id(int &cnt) {
225  ++cnt;
226  return cnt;
227 }
228 
229 inline void Trajectory::mark_lost(void) { state = Lost; }
230 
231 inline void Trajectory::mark_removed(void) { state = Removed; }
232 
233 } // namespace track
234 } // namespace nndeploy
235 
236 #endif // _NNDEPLOY_TRACK_TRAJECTORY_H_
virtual void init(const cv::Mat &measurement)
virtual void project(cv::Mat *mean, cv::Mat *covariance) const
virtual const cv::Mat & correct(const cv::Mat &measurement)
virtual const cv::Mat & predict()
friend TrajectoryPool operator-(const TrajectoryPool &a, const TrajectoryPool &b)
friend TrajectoryPtrPool operator+(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b)
friend cv::Mat iou_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b)
friend TrajectoryPtrPool operator-(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b)
virtual const cv::Mat & predict(void)
virtual void reactivate(Trajectory *traj, int &cnt, int timestamp, bool newid=false)
friend TrajectoryPool & operator-=(TrajectoryPool &a, const TrajectoryPool &b)
friend cv::Mat embedding_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b)
virtual void activate(int &cnt, int timestamp)
virtual void mark_removed(void)
Definition: trajectory.h:231
friend TrajectoryPool & operator+=(TrajectoryPool &a, const TrajectoryPtrPool &b)
Trajectory & operator=(const Trajectory &rhs)
Definition: trajectory.h:200
friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a, const TrajectoryPool &b)
friend TrajectoryPtrPool operator+(const TrajectoryPtrPool &a, TrajectoryPool *b)
friend cv::Mat mahalanobis_distance(const TrajectoryPool &a, const TrajectoryPool &b)
virtual ~Trajectory(void)
Definition: trajectory.h:72
friend cv::Mat mahalanobis_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b)
friend TrajectoryPool operator+(const TrajectoryPool &a, const TrajectoryPtrPool &b)
friend cv::Mat embedding_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b)
friend cv::Mat embedding_distance(const TrajectoryPool &a, const TrajectoryPool &b)
virtual void update(Trajectory *traj, int timestamp, bool update_embedding=true)
friend cv::Mat iou_distance(const TrajectoryPtrPool &a, const TrajectoryPtrPool &b)
virtual void mark_lost(void)
Definition: trajectory.h:229
friend cv::Mat iou_distance(const TrajectoryPool &a, const TrajectoryPool &b)
friend TrajectoryPool operator+(const TrajectoryPool &a, const TrajectoryPool &b)
base::Status embedding(device::Tensor *input, device::Tensor *indice, device::Tensor *output)
std::vector< Trajectory >::iterator TrajectoryPoolIterator
Definition: trajectory.h:39
std::vector< Trajectory * >::iterator TrajectoryPtrPoolIterator
Definition: trajectory.h:41
std::vector< Trajectory > TrajectoryPool
Definition: trajectory.h:37
cv::Vec4f ltrb2xyah(const cv::Vec4f &ltrb)
Definition: trajectory.h:142
std::vector< Trajectory * > TrajectoryPtrPool
Definition: trajectory.h:40