-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTrack.cs
More file actions
136 lines (112 loc) · 3.45 KB
/
STrack.cs
File metadata and controls
136 lines (112 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using MathNet.Numerics.LinearAlgebra;
namespace ByteTrackCSharp
{
public enum STrackState
{
New = 0,
Tracked = 1,
Lost = 2,
Removed = 3,
};
public class STrack
{
KalmanFilter kalman_filter_;
Matrix<float> mean_;
Matrix<float> covariance_;
Rect rect_;
STrackState state_;
bool is_activated_;
float score_;
public STrack(Rect rect, float score)
{
kalman_filter_ = new KalmanFilter();
mean_ = MatrixUtil.StateMean();
covariance_ = MatrixUtil.StateCov();
rect_ = rect;
state_ = STrackState.New;
is_activated_ = false;
score_ = score;
TrackId = 0;
FrameId = 0;
StartFrameId = 0;
TrackletLength = 0;
}
public Rect getRect() => rect_;
public STrackState getSTrackState() => state_;
public bool isActivated() => is_activated_;
public float getScore() => score_;
public int TrackId { get; private set; }
public int FrameId { get; private set; }
public int StartFrameId { get; private set; }
public int TrackletLength { get; private set; }
public void activate(int frame_id, int track_id)
{
activateKalmanFilters();
updateRect();
state_ = STrackState.Tracked;
if (frame_id == 1)
{
is_activated_ = true;
}
TrackId = track_id;
FrameId = frame_id;
StartFrameId = frame_id;
TrackletLength = 0;
}
public void reActivate(STrack new_track, int frame_id, int new_track_id = -1)
{
updateKalmanFilters(new_track);
updateRect();
state_ = STrackState.Tracked;
is_activated_ = true;
score_ = new_track.getScore();
if (0 <= new_track_id)
{
TrackId = new_track_id;
}
FrameId = frame_id;
TrackletLength = 0;
}
public virtual void predict()
{
if (state_ != STrackState.Tracked)
{
mean_[0, 7] = 0;
}
kalman_filter_.predict(ref mean_, ref covariance_);
}
public virtual void update(STrack new_track, int frame_id)
{
updateKalmanFilters(new_track);
updateRect();
state_ = STrackState.Tracked;
is_activated_ = true;
score_ = new_track.getScore();
FrameId = frame_id;
TrackletLength++;
}
public void markAsLost()
{
state_ = STrackState.Lost;
}
public void markAsRemoved()
{
state_ = STrackState.Removed;
}
public void updateRect()
{
rect_.setWidth(mean_[0, 2] * mean_[0, 3]);
rect_.SetHeight(mean_[0, 3]);
rect_.setX(mean_[0, 0] - rect_.width() / 2);
rect_.setY(mean_[0, 1] - rect_.height() / 2);
}
protected virtual void activateKalmanFilters()
{
kalman_filter_.initiate(ref mean_, ref covariance_, rect_.getXyah());
}
protected virtual void updateKalmanFilters(STrack new_track)
{
kalman_filter_.update(ref mean_, ref covariance_, new_track.getRect().getXyah());
}
}
}