23 #include "fastdeploy/fastdeploy_model.h" 24 #include <opencv2/core/core.hpp> 25 #include <opencv2/highgui/highgui.hpp> 26 #include <opencv2/imgproc/imgproc.hpp> 27 #include "opencv2/video/tracking.hpp" 33 typedef enum { New = 0, Tracked = 1, Lost = 2, Removed = 3 } TrajectoryState;
36 typedef std::vector<Trajectory> TrajectoryPool;
37 typedef std::vector<Trajectory>::iterator TrajectoryPoolIterator;
38 typedef std::vector<Trajectory *> TrajectoryPtrPool;
39 typedef std::vector<Trajectory *>::iterator TrajectoryPtrPoolIterator;
41 class FASTDEPLOY_DECL TKalmanFilter :
public cv::KalmanFilter {
44 virtual ~TKalmanFilter(
void) {}
45 virtual void init(
const cv::Mat &measurement);
46 virtual const cv::Mat &predict();
47 virtual const cv::Mat &correct(
const cv::Mat &measurement);
48 virtual void project(cv::Mat *mean, cv::Mat *covariance)
const;
51 float std_weight_position;
52 float std_weight_velocity;
55 inline TKalmanFilter::TKalmanFilter(
void) : cv::KalmanFilter(8, 4) {
56 cv::KalmanFilter::transitionMatrix = cv::Mat::eye(8, 8, CV_32F);
57 for (
int i = 0; i < 4; ++i)
58 cv::KalmanFilter::transitionMatrix.at<float>(i, i + 4) = 1;
59 cv::KalmanFilter::measurementMatrix = cv::Mat::eye(4, 8, CV_32F);
60 std_weight_position = 1 / 20.f;
61 std_weight_velocity = 1 / 160.f;
64 class FASTDEPLOY_DECL Trajectory :
public TKalmanFilter {
67 Trajectory(
const cv::Vec4f <rb,
float score,
const cv::Mat &embedding);
68 Trajectory(
const Trajectory &other);
69 Trajectory &operator=(
const Trajectory &rhs);
70 virtual ~Trajectory(
void) {}
73 virtual const cv::Mat &predict(
void);
74 virtual void update(Trajectory *traj,
76 bool update_embedding =
true);
77 virtual void activate(
int &cnt,
int timestamp);
78 virtual void reactivate(Trajectory *traj,
int &cnt,
79 int timestamp,
bool newid =
false);
80 virtual void mark_lost(
void);
81 virtual void mark_removed(
void);
83 friend TrajectoryPool operator+(
const TrajectoryPool &a,
84 const TrajectoryPool &b);
85 friend TrajectoryPool operator+(
const TrajectoryPool &a,
86 const TrajectoryPtrPool &b);
87 friend TrajectoryPool &operator+=(TrajectoryPool &a,
88 const TrajectoryPtrPool &b);
89 friend TrajectoryPool operator-(
const TrajectoryPool &a,
90 const TrajectoryPool &b);
91 friend TrajectoryPool &operator-=(TrajectoryPool &a,
92 const TrajectoryPool &b);
93 friend TrajectoryPtrPool operator+(
const TrajectoryPtrPool &a,
94 const TrajectoryPtrPool &b);
95 friend TrajectoryPtrPool operator+(
const TrajectoryPtrPool &a,
97 friend TrajectoryPtrPool operator-(
const TrajectoryPtrPool &a,
98 const TrajectoryPtrPool &b);
100 friend cv::Mat embedding_distance(
const TrajectoryPool &a,
101 const TrajectoryPool &b);
102 friend cv::Mat embedding_distance(
const TrajectoryPtrPool &a,
103 const TrajectoryPtrPool &b);
104 friend cv::Mat embedding_distance(
const TrajectoryPtrPool &a,
105 const TrajectoryPool &b);
107 friend cv::Mat mahalanobis_distance(
const TrajectoryPool &a,
108 const TrajectoryPool &b);
109 friend cv::Mat mahalanobis_distance(
const TrajectoryPtrPool &a,
110 const TrajectoryPtrPool &b);
111 friend cv::Mat mahalanobis_distance(
const TrajectoryPtrPool &a,
112 const TrajectoryPool &b);
114 friend cv::Mat iou_distance(
const TrajectoryPool &a,
const TrajectoryPool &b);
115 friend cv::Mat iou_distance(
const TrajectoryPtrPool &a,
116 const TrajectoryPtrPool &b);
117 friend cv::Mat iou_distance(
const TrajectoryPtrPool &a,
118 const TrajectoryPool &b);
121 void update_embedding(
const cv::Mat &embedding);
124 TrajectoryState state;
126 cv::Mat smooth_embedding;
136 cv::Mat current_embedding;
141 inline cv::Vec4f ltrb2xyah(
const cv::Vec4f <rb) {
143 xyah[0] = (ltrb[0] + ltrb[2]) * 0.5f;
144 xyah[1] = (ltrb[1] + ltrb[3]) * 0.5f;
145 xyah[3] = ltrb[3] - ltrb[1];
146 xyah[2] = (ltrb[2] - ltrb[0]) / xyah[3];
150 inline Trajectory::Trajectory()
153 smooth_embedding(cv::Mat()),
162 inline Trajectory::Trajectory(
const cv::Vec4f <rb_,
164 const cv::Mat &embedding)
167 smooth_embedding(cv::Mat()),
175 xyah = ltrb2xyah(ltrb);
176 update_embedding(embedding);
179 inline Trajectory::Trajectory(
const Trajectory &other)
180 : state(other.state),
183 is_activated(other.is_activated),
184 timestamp(other.timestamp),
185 starttime(other.starttime),
189 length(other.length) {
190 other.smooth_embedding.copyTo(smooth_embedding);
191 other.current_embedding.copyTo(current_embedding);
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);
200 inline Trajectory &Trajectory::operator=(
const Trajectory &rhs) {
201 this->state = rhs.state;
202 this->ltrb = rhs.ltrb;
203 rhs.smooth_embedding.copyTo(this->smooth_embedding);
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);
212 this->length = rhs.length;
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);
224 inline int Trajectory::next_id(
int &cnt) {
229 inline void Trajectory::mark_lost(
void) { state = Lost; }
231 inline void Trajectory::mark_removed(
void) { state = Removed; }
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16