-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.cpp
213 lines (169 loc) · 4.56 KB
/
Tracker.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
#include "Tracker.h"
#include "DIP.h"
#include "IOManager.h"
using namespace std;
using namespace cv;
TrackingClass::TrackingClass(TrackingClass::Tracking ttype, TrackingClass::Background btype, const cv::Mat &background)
{
trackingType = ttype;
backgroundType = btype;
if (backgroundType == Background::firstFrameBS)
{
cvtColor(background, bg, COLOR_BGR2GRAY);
Mat resized(cvRound(bg.cols / scale), cvRound(bg.rows / scale), CV_8UC1);
resize(bg, bg, resized.size());
bgModel.release();
}
else if (backgroundType == Background::opencvBS)
{
bgModel = createBackgroundSubtractorMOG2().dynamicCast<BackgroundSubtractor>();
mask.create(Size(cvRound(background.cols / scale), cvRound(background.rows / scale)), background.type());
}
}
void TrackingClass::track(cv::Mat & image)
{
if (backgroundType == Background::firstFrameBS)
{
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
resize(gray_image, gray_image, bg.size());
mask = DIP::maskFirstFrame(gray_image, bg);
resize(mask, mask, image.size());
}
else if (backgroundType == Background::opencvBS)
{
Rect ROI(50, 150, (image.cols - 100), (image.rows - 150));
Mat frameMask(image.size(), CV_8UC1, Scalar::all(0));
frameMask(ROI).setTo(Scalar::all(255));
Mat result;
image.copyTo(result, frameMask);
resize(result, result, Size(image.cols / 4, image.rows / 4));
bgModel->apply(result, mask, true ? -1 : 0);
DIP::maskOpencvBS(mask);
bgModel->getBackgroundImage(bg);
resize(mask, mask, image.size());
}
if (trackingType == Tracking::contours)
{
std::vector<Rect> detectedObjects;
Mat contours_temp;
mask.copyTo(contours_temp);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(contours_temp, contours, hierarchy, 0, 2);
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) < 1000)
{
contours.erase(contours.begin() + i);
}
}
for (int i = 0; i < contours.size(); i++)
{
detectedObjects.push_back(boundingRect(contours.at(i)));
}
for (unsigned int i = 0; i < trackedObjects.size(); i++)
{
trackedObjects[i].match(detectedObjects);
}
for (unsigned int i = 0; i < detectedObjects.size(); i++)
{
ObjectTracked object(detectedObjects[i]);
trackedObjects.push_back(object);
}
int activeCount = 0;
for (unsigned int i = 0; i <trackedObjects.size(); i++)
{
if (trackedObjects[i].good())
activeCount++;
trackedObjects[i].draw(image, Point(20, (50 * activeCount)));
}
if (activeCount == 0)
{
putText(image, "No objects detected", Point(20, 50), 1, 3, Scalar(255, 255, 255), 2);
}
}
else if (trackingType == Tracking::opencvTracker)
{
std::vector<Rect> detectedObjects;
Mat contoursTemp;
mask.copyTo(contoursTemp);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(contoursTemp, contours, hierarchy, 0, 2);
// find contours of objects big enough
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) < 1000)
{
contours.erase(contours.begin() + i);
}
}
for (int i = 0; i < contours.size(); i++)
{
detectedObjects.push_back(boundingRect(contours.at(i)));
}
//activate tracker once we have 3 objects
if (!active)
{
if (detectedObjects.size() < 3)
return;
else
{
active = 1;
Mat copy = image.clone();
DIP::prepare(copy);
for (int i = 0; i < detectedObjects.size(); i++)
{
Ptr<Tracker> tr = TrackerBoosting::create();
tr->init(copy, detectedObjects[i]);
tracker.push_back(tr);
}
}
}
else
{
Mat copy = image.clone();
DIP::prepare(copy);
//update trackers
vector<Rect> TrackingRects;
for (int i = 0; i < tracker.size(); i++)
{
Rect2d r;
// uzywany tracker nie zwraca informacji o zgubieniu obiektu
if (tracker[i]->update(image, r))
TrackingRects.push_back(r);
else
{
tracker[i].release();
tracker.erase(tracker.begin() + i);
}
}
for (unsigned int i = 0; i < trackedObjects.size(); i++)
{
trackedObjects[i].match(TrackingRects);
}
for (unsigned int i = 0; i < TrackingRects.size(); i++)
{
ObjectTracked object(TrackingRects[i]);
trackedObjects.push_back(object);
}
int activeCount = 0;
for (unsigned int i = 0; i <trackedObjects.size(); i++)
{
if (trackedObjects[i].good())
activeCount++;
trackedObjects[i].draw(image, Point(20, (50 * activeCount)));
}
}
}
IOManager::sendObjectData(trackedObjects, "filename");
}
cv::Mat TrackingClass::returnMask()
{
return mask;
}
TrackingClass::~TrackingClass()
{
destroyAllWindows();
}