-
Notifications
You must be signed in to change notification settings - Fork 0
/
OFTracker.cpp
219 lines (175 loc) · 4.31 KB
/
OFTracker.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "OFTracker.h"
#define FEATURE_COLOR 0, 255, 255
OFTracker::OFTracker()
:
nfeatures(200),
hough(NULL),
mask(Mask()),
gray(cv::Mat()),
prev_gray(cv::Mat()),
win_size(10, 10),
term_crit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 40, 0.03),
initialized(false),
use_correction(false),
correct_in_X_frames(0),
num_of_non_correction_frames(0)
{
}
OFTracker::~OFTracker()
{
deInit();
}
/*
* Configures the tracker.
* Can be called at any time.
*/
void OFTracker::configure(int n, bool use_correction, int non_correction_steps)
{
nfeatures = n;
this->use_correction = use_correction;
if(use_correction)
{
num_of_non_correction_frames = non_correction_steps;
correct_in_X_frames = non_correction_steps;
}
}
/*
* Initialises mask and HoughHash.
* Must be called before using the tracker.
*/
void OFTracker::init(cv::Mat &frame)
{
mask.init(frame.size());
cv::cvtColor(frame, gray, CV_BGR2GRAY);
cv::cvtColor(frame, prev_gray, CV_BGR2GRAY);
this->frame = frame;
hough = new HoughHash();
correct_in_X_frames = num_of_non_correction_frames;
initialized = true;
}
/*
* Initialises mask and HoughHash.
* Must be called before using the tracker.
*/
void OFTracker::init(cv::Mat &frame, FlowBox &bb)
{
OFTracker::init(frame);
setMask(bb);
}
void OFTracker::deInit()
{
if (!initialized) return;
gray.release();
prev_gray.release();
delete hough;
initialized = false;
}
void OFTracker::reset()
{
deInit();
}
bool OFTracker::isInitialized() const
{
return initialized;
}
bool OFTracker::setMask(const FlowBox &bb)
{
if(!initialized) return false;
mask.set(bb);
return true;
}
bool OFTracker::setFrame(cv::Mat &frame)
{
if(!initialized) return false;
this->frame = frame;
cv::Mat tmp;
CV_SWAP(prev_gray, gray, tmp);
cv::cvtColor(frame, gray, CV_BGR2GRAY);
return true;
}
/*
* Finds trackable features in the area defined by our mask and bounding box
*/
bool OFTracker::findFeatures(Points &points, bool masked)
{
if(!initialized) return false;
cv::Mat msk;
if (masked) msk = mask.mask;
else msk = cv::Mat();
double quality = 0.01;
double min_distance = 3;
cv::goodFeaturesToTrack(gray, points, nfeatures, quality, min_distance, msk, 3, 0, 0.04);
if (points.size() == 0) return false;
cv::cornerSubPix(gray, points, win_size, cv::Size(-1,-1), term_crit);
return true;
}
/*
* Calculates the movement of all features in the bounding box between the current and previous step
*/
bool OFTracker::trackFeatures(Points &points_old, Points &points_new, Errors &error, Statuses &status)
{
if(!initialized) return false;
//hack, remove if possible
cv::Mat in(points_old), out(points_new);
cv::Mat stat(status);
cv::Mat err(error);
cv::calcOpticalFlowPyrLK(prev_gray, gray, in, out, stat, err, win_size, 3, term_crit, 0);
if(points_new.size() != points_old.size()) points_new.resize(points_old.size());
for(int i = 0; i < static_cast<int>(points_old.size()); i++)
{
points_new[i] = out.at<cv::Point2f>(i);
status[i] = stat.at<char>(i);
}
removeOutliers(points_new, status);
return true;
}
/*
* Removes all points that lie outside of the bounding box from the mask
*/
void OFTracker::removeOutliers(Points &points, Statuses &status)
{
for(int i = 0; i < static_cast<int>(points.size()); i++)
{
if(!status[i] || points[i].x >= gray.cols || points[i].x < 0 || points[i].y >= gray.rows || points[i].y < 0)
{
points[i] = cvPoint2D32f(-1, -1);
status[i] = 0;
}
}
}
/*
* Calculates the position and orientation of the bounding box for the next frame using the HoughHash
* and the optical flow of all features determined by track() and trackFeatures(). If enabled, correction
* is performed afterwards.
* This is where all the interesting stuff either happens or is initiated from.
*/
void OFTracker::next(cv::Mat &frame, FlowBox &bb)
{
if (!initialized) return;
cv::Point2f p1, p2;
cv::Point2f center = bb.getRotationCenter();
setMask(bb);
setFrame(frame);
track();
hough->reset();
int i = 0;
//iterate through point moves
while(iteratePoints(p1, p2))
{
if (mask.getValue(p1) == INSIDE_FlowBox && mask.getValue(p2))
{
hough->fill(p1 - center, p2 - center, 1);
i++;
}
}
if(i) bb.applyTransform(hough->getMaxTransform());
if(use_correction)
{
correct_in_X_frames--;
if (correct_in_X_frames == 0)
{
correct(bb);
correct_in_X_frames = num_of_non_correction_frames;
}
}
}