forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCtracker.cpp
More file actions
193 lines (175 loc) · 5.57 KB
/
Ctracker.cpp
File metadata and controls
193 lines (175 loc) · 5.57 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "Ctracker.h"
using namespace cv;
using namespace std;
size_t CTrack::NextTrackID=0;
// ---------------------------------------------------------------------------
// Track constructor.
// The track begins from initial point (pt)
// ---------------------------------------------------------------------------
CTrack::CTrack(Point2f pt, float dt, float Accel_noise_mag)
{
track_id=NextTrackID;
NextTrackID++;
// Every track have its own Kalman filter,
// it user for next point position prediction.
KF = new TKalmanFilter(pt,dt,Accel_noise_mag);
// Here stored points coordinates, used for next position prediction.
prediction=pt;
skipped_frames=0;
}
// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
CTrack::~CTrack()
{
// Free resources.
delete KF;
}
// ---------------------------------------------------------------------------
// Tracker. Manage tracks. Create, remove, update.
// ---------------------------------------------------------------------------
CTracker::CTracker(float _dt, float _Accel_noise_mag, double _dist_thres, int _maximum_allowed_skipped_frames,int _max_trace_length)
{
dt=_dt;
Accel_noise_mag=_Accel_noise_mag;
dist_thres=_dist_thres;
maximum_allowed_skipped_frames=_maximum_allowed_skipped_frames;
max_trace_length=_max_trace_length;
}
// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
void CTracker::Update(vector<Point2d>& detections)
{
// -----------------------------------
// If there is no tracks yet, then every point begins its own track.
// -----------------------------------
if(tracks.size()==0)
{
// If no tracks yet
for(int i=0;i<detections.size();i++)
{
CTrack* tr=new CTrack(detections[i],dt,Accel_noise_mag);
tracks.push_back(tr);
}
}
// -----------------------------------
// Çäåñü òðåêè óæå åñòü â ëþáîì ñëó÷àå
// -----------------------------------
int N=tracks.size(); // òðåêè
int M=detections.size(); // äåòåêòû
// Ìàòðèöà ðàññòîÿíèé îò N-íîãî òðåêà äî M-íîãî äåòåêòà.
vector< vector<double> > Cost(N,vector<double>(M));
vector<int> assignment; // íàçíà÷åíèÿ
// -----------------------------------
// Òðåêè óæå åñòü, ñîñòàâèì ìàòðèöó ðàññòîÿíèé
// -----------------------------------
double dist;
for(int i=0;i<tracks.size();i++)
{
// Point2d prediction=tracks[i]->prediction;
// cout << prediction << endl;
for(int j=0;j<detections.size();j++)
{
Point2d diff=(tracks[i]->prediction-detections[j]);
dist=sqrtf(diff.x*diff.x+diff.y*diff.y);
Cost[i][j]=dist;
}
}
// -----------------------------------
// Solving assignment problem (tracks and predictions of Kalman filter)
// -----------------------------------
AssignmentProblemSolver APS;
APS.Solve(Cost,assignment,AssignmentProblemSolver::optimal);
// -----------------------------------
// clean assignment from pairs with large distance
// -----------------------------------
// Not assigned tracks
vector<int> not_assigned_tracks;
for(int i=0;i<assignment.size();i++)
{
if(assignment[i]!=-1)
{
if(Cost[i][assignment[i]]>dist_thres)
{
assignment[i]=-1;
// Mark unassigned tracks, and increment skipped frames counter,
// when skipped frames counter will be larger than threshold, track will be deleted.
not_assigned_tracks.push_back(i);
}
}
else
{
// If track have no assigned detect, then increment skipped frames counter.
tracks[i]->skipped_frames++;
}
}
// -----------------------------------
// If track didn't get detects long time, remove it.
// -----------------------------------
for(int i=0;i<tracks.size();i++)
{
if(tracks[i]->skipped_frames>maximum_allowed_skipped_frames)
{
delete tracks[i];
tracks.erase(tracks.begin()+i);
assignment.erase(assignment.begin()+i);
i--;
}
}
// -----------------------------------
// Search for unassigned detects
// -----------------------------------
vector<int> not_assigned_detections;
vector<int>::iterator it;
for(int i=0;i<detections.size();i++)
{
it=find(assignment.begin(), assignment.end(), i);
if(it==assignment.end())
{
not_assigned_detections.push_back(i);
}
}
// -----------------------------------
// and start new tracks for them.
// -----------------------------------
if(not_assigned_detections.size()!=0)
{
for(int i=0;i<not_assigned_detections.size();i++)
{
CTrack* tr=new CTrack(detections[not_assigned_detections[i]],dt,Accel_noise_mag);
tracks.push_back(tr);
}
}
// Update Kalman Filters state
for(int i=0;i<assignment.size();i++)
{
// If track updated less than one time, than filter state is not correct.
tracks[i]->KF->GetPrediction();
if(assignment[i]!=-1) // If we have assigned detect, then update using its coordinates,
{
tracks[i]->skipped_frames=0;
tracks[i]->prediction=tracks[i]->KF->Update(detections[assignment[i]],1);
}else // if not continue using predictions
{
tracks[i]->prediction=tracks[i]->KF->Update(Point2f(0,0),0);
}
if(tracks[i]->trace.size()>max_trace_length)
{
tracks[i]->trace.erase(tracks[i]->trace.begin(),tracks[i]->trace.end()-max_trace_length);
}
tracks[i]->trace.push_back(tracks[i]->prediction);
tracks[i]->KF->LastResult=tracks[i]->prediction;
}
}
// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
CTracker::~CTracker(void)
{
for(int i=0;i<tracks.size();i++)
{
delete tracks[i];
}
tracks.clear();
}