-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
378 lines (329 loc) · 11.7 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <deque>
#include <time.h>
#include <cstdlib>
#include <sys/time.h>
int getTimeDifferenceInMS(const struct timeval & lhs, const struct timeval & rhs) {
int difference = 0;
difference = (lhs.tv_sec - rhs.tv_sec) * 1000;
if (difference < 0) {
difference = -difference;
}
int usecdiff = (lhs.tv_usec - rhs.tv_usec) / 1000;
if (usecdiff < 0) {
usecdiff = -usecdiff;
}
difference += usecdiff;
//std::cout<<"DIF="<<difference<<std::endl;
return difference;
}
class TraceEntry {
public:
cv::Point point;
timeval time;
};
class Trackable {
private:
int traceTTL;
std::deque<TraceEntry*> trace;
public:
Trackable() : traceTTL(1000) {
}
Trackable(int traceTTL) : traceTTL(traceTTL) {
}
~Trackable() {
while (!trace.empty()) {
delete trace.front();
trace.erase(trace.begin());
}
}
void setTraceTTL(int ms) {
traceTTL = ms;
}
int getTraceTTL() {
return traceTTL;
}
cv::Point getLastLocalization() {
if (!trace.empty()) {
return trace.back()->point;
}
return *(new cv::Point(0, 0));
}
void signal(cv::Point point) {
timeval t;
gettimeofday(&t, NULL);
TraceEntry * entry = new TraceEntry;
entry->point = point;
entry->time = t;
trace.push_back(entry);
}
void refresh() {
timeval now;
gettimeofday(&now, NULL);
while(!trace.empty()) {
TraceEntry * lastEntry = trace.front();
if (getTimeDifferenceInMS(now, lastEntry->time) > traceTTL) {
trace.pop_front();
delete lastEntry;
std::cout<<"Removed last entry of a trace"<<std::endl;
} else {
return;
}
}
}
bool hasAnyPoints() {
return !trace.empty();
}
std::vector<cv::Point> getTracePoints() {
std::vector<cv::Point> ret;
for (std::deque<TraceEntry*>::iterator it = trace.begin(); it != trace.end(); it++) {
TraceEntry * entry = *it;
ret.push_back(entry->point);
}
return ret;
}
};
class ObjectTracker {
private:
int traceTTL;
double distanceThreshold;
std::deque<Trackable*> objects;
double getDistance(cv::Point lhs, cv::Point rhs) {
double xDist = lhs.x - rhs.x;
double yDist = rhs.y - rhs.y;
return xDist*xDist + yDist*yDist;
}
std::vector<std::vector<cv::Point > >::iterator findClosest(cv::Point point, std::vector<std::vector<cv::Point> > & contours) {
std::vector<std::vector<cv::Point> >::iterator closest = contours.end();
double minDistance = 1e9;
for (std::vector<std::vector<cv::Point> >::iterator it = contours.begin(); it != contours.end(); it++) {
cv::Point cog = getCOG(*it);
double distance = getDistance(cog, point);
if (distance < minDistance) {
minDistance = distance;
closest = it;
}
}
return closest;
}
cv::Point getCOG(const std::vector<cv::Point> & contour) {
cv::Point ret;
ret.x = 0;
ret.y = 0;
for (std::vector<cv::Point>::const_iterator it = contour.begin(); it != contour.end(); it++) {
ret.x += (*it).x;
ret.y += (*it).y;
}
ret.x /= contour.size();
ret.y /= contour.size();
return ret;
}
void appendToExisting(std::vector<std::vector<cv::Point> > & contours) {
for (std::deque<Trackable*>::iterator it = objects.begin(); it != objects.end(); it++) {
Trackable * trackable = *it;
std::vector<std::vector<cv::Point > >::iterator closest = findClosest(trackable->getLastLocalization(), contours);
if (closest != contours.end()) {
std::vector<cv::Point> contour = *closest;
cv::Point cog = getCOG(contour);
if (getDistance(cog, trackable->getLastLocalization()) < distanceThreshold) {
trackable->signal(cog);
contours.erase(closest);
}
}
}
}
void createNew(std::vector<std::vector<cv::Point> > & contours) {
for (std::vector<std::vector<cv::Point> >::iterator it = contours.begin(); it != contours.end(); it++) {
Trackable * trackable = new Trackable(traceTTL);
cv::Point cog = getCOG(*it);
trackable->signal(cog);
objects.push_back(trackable);
}
}
public:
ObjectTracker(int traceTTL, double distanceTreshold) : traceTTL(traceTTL), distanceThreshold(distanceTreshold) {
}
~ObjectTracker() {
while (!objects.empty()) {
delete objects.front();
objects.erase(objects.begin());
}
}
void setTraceTTL(int ms) {
traceTTL = ms;
}
int getTraceTTL() {
return traceTTL;
}
void setDistanceTreshold(double distance) {
distanceThreshold = distance;
}
double getDistanceTreshold() {
return distanceThreshold;
}
void refresh() {
while (!objects.empty()) {
Trackable * trackable = objects.front();
trackable->refresh();
if (!trackable->hasAnyPoints()) {
objects.pop_front();
std::cout<<"Removed object with no points left"<<std::endl;
} else {
return;
}
delete trackable;
}
}
void record(const std::vector<std::vector<cv::Point> > & contours) {
std::vector<std::vector<cv::Point> > copyOfContours(contours);
appendToExisting(copyOfContours);
createNew(copyOfContours);
}
const std::deque<Trackable*> & getTracked() {
return objects;
}
int getTrackedCount() {
return objects.size();
}
};
void findRectangles(const std::vector<std::vector<cv::Point> > & contours, std::vector<std::vector<cv::Point> > & rectangles) {
rectangles.clear();
for (std::vector<std::vector<cv::Point> >::const_iterator it = contours.begin(); it != contours.end(); it++) {
std::vector<cv::Point> contour = *it;
double minX = 1e6;
double maxX = -1e6;
double minY = 1e6;
double maxY = -1e6;
for (std::vector<cv::Point>::iterator pit = contour.begin(); pit != contour.end(); pit++) {
cv::Point point = *pit;
minX = point.x < minX ? point.x : minX;
maxX = point.x > maxX ? point.x : maxX;
minY = point.y < minY ? point.y : minY;
maxY = point.y > maxY ? point.y : maxY;
}
std::vector<cv::Point> rect;
rect.push_back(*(new cv::Point(minX, minY)));
rect.push_back(*(new cv::Point(minX, maxY)));
rect.push_back(*(new cv::Point(maxX, maxY)));
rect.push_back(*(new cv::Point(maxX, minY)));
rectangles.push_back(rect);
}
}
void filterSmall(std::vector<std::vector<cv::Point> > & rectangles, double minArea) {
std::vector<std::vector<cv::Point> >::iterator it = rectangles.begin();
while (it != rectangles.end()) {
std::vector<cv::Point> rect = *it;
double xrange = rect[2].x - rect[0].x;
double yrange = rect[1].y - rect[0].y;
if (xrange * yrange < minArea) {
it = rectangles.erase(it);
} else {
it++;
}
}
}
void drawTraces(cv::Mat & img, ObjectTracker * objectTracker) {
std::deque<Trackable*> tracked = objectTracker->getTracked();
if (tracked.empty()) {
return;
}
cv::Point ** curves = new cv::Point *[tracked.size()];
int * curveLengths = new int[tracked.size()];
int cid = 0;
for (std::deque<Trackable*>::iterator it = tracked.begin(); it != tracked.end(); it++) {
Trackable * trackable = *it;
std::vector<cv::Point> trace = trackable->getTracePoints();
curves[cid] = new cv::Point[trace.size()];
for (int i = 0; i < trace.size(); i++) {
curves[cid][i].x = trace[i].x;
curves[cid][i].y = trace[i].y;
}
curveLengths[cid] = trace.size();
//std::cout<<"Current pos: "<<trackable->getLastLocalization()<<" Trace length: "<<trace.size()<<std::endl;
cid++;
}
cv::polylines(img, (const cv::Point **) curves, curveLengths, cid, false, cvScalar(0, 255, 255), 2);
for (int i = 0; i < cid; i++) {
delete[] curves[i];
}
delete[] curves;
delete[] curveLengths;
}
int main(int argc, char *argv[])
{
timeval now, prev;
gettimeofday(&now, NULL);
gettimeofday(&prev, NULL);
std::cout<<"Entering crossroad"<<std::endl;
std::string * filename = 0;
if (argc > 1)
{
filename = new std::string(argv[1]);
}
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
cv::VideoCapture * capture;
//capture->set(CV_CAP_PROP_FRAME_WIDTH,64);
//capture->set(CV_CAP_PROP_FRAME_HEIGHT,64);
if (filename != NULL)
{
std::cout<<"Reading from "<<*filename<<std::endl;
capture = new cv::VideoCapture(*filename);
}
else
{
std::cout<<"Reading from default camera"<<std::endl;
capture = new cv::VideoCapture(0);
capture->set(CV_CAP_PROP_FRAME_WIDTH, 160);
capture->set(CV_CAP_PROP_FRAME_HEIGHT, 120);
}
if (!capture->isOpened())
{
std::cout<<"Could not open video source\n";
return -1;
}
cv::BackgroundSubtractorMOG2 bg(3, 16, true);
std::vector<std::vector<cv::Point> > contours;
std::vector<std::vector<cv::Point> > rectangles;
cv::namedWindow("Frame", CV_WINDOW_AUTOSIZE);
//cv::namedWindow("Background", CV_WINDOW_AUTOSIZE);
//cv::namedWindow("Foreground", CV_WINDOW_AUTOSIZE);
ObjectTracker * objectTracker = new ObjectTracker(5000, 10000.);
while (true)
{
*capture >> frame;
if (frame.empty()) {
std::cout<<"Frame empty"<<std::endl;
}
bg(frame, fore, 0.01);
bg.getBackgroundImage(back);
cv::erode(fore, fore, cv::Mat());
cv::dilate(fore, fore, cv::Mat());
cv::findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS);
findRectangles(contours, rectangles);
filterSmall(rectangles, 100 * 100);
objectTracker->refresh();
objectTracker->record(rectangles);
cv::drawContours(frame, rectangles, -1, cv::Scalar(0, 0, 255), 2);
drawTraces(frame, objectTracker);
cv::imshow("Frame", frame);
//cv::imshow("Background", back);
//cv::imshow("Foreground", fore);
if (cv::waitKey(30) >= 0) break;
gettimeofday(&now, NULL);
double sec = (double)getTimeDifferenceInMS(now, prev) / 1000.;
gettimeofday(&prev, NULL);
std::cout<<1./sec<<" fps"<<std::endl;
std::cout<<"Total objects present: "<<objectTracker->getTrackedCount()<<std::endl;
}
if (filename != NULL)
{
delete filename;
}
delete capture;
delete objectTracker;
return 0;
}