-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathDetector.cpp
144 lines (120 loc) · 4.88 KB
/
Detector.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
/* Copyright 2011 Ben Pryke.
This file is part of Ben Pryke's TLD Implementation available under the
terms of the GNU General Public License as published by the Free Software
Foundation. This software is provided without warranty of ANY kind. */
#include "Detector.h"
Detector::Detector(int frameWidth, int frameHeight, double *bb, Classifier *classifier) {
width = frameWidth;
height = frameHeight;
initBBWidth = (float)bb[2];
initBBHeight = (float)bb[3];
this->classifier = classifier;
}
double Detector::bbOverlap(double *bb1, double *bb2) {
// Check whether the bounding-boxes overlap at all
if (bb1[0] > bb2[0] + bb2[2]) {
return 0;
}
else if (bb1[1] > bb2[1] + bb2[3]) {
return 0;
}
else if (bb2[0] > bb1[0] + bb1[2]) {
return 0;
}
else if (bb2[1] > bb1[1] + bb1[3]) {
return 0;
}
// If we got this far, the bounding-boxes overlap
double overlapWidth = min(bb1[0] + bb1[2], bb2[0] + bb2[2]) - max(bb1[0], bb2[0]);
double overlapHeight = min(bb1[1] + bb1[3], bb2[1] + bb2[3]) - max(bb1[1], bb2[1]);
double overlapArea = overlapWidth * overlapHeight;
double bb1Area = bb1[2] * bb1[3];
double bb2Area = bb2[2] * bb2[3];
return overlapArea / (bb1Area + bb2Area - overlapArea);
}
vector<double *> *Detector::detect(IntegralImage *frame, double *tbb) {
// Set the width and height that are used as 1 * scale.
// If tbb is NULL, we are not tracking and use the first-frame
// bounding-box size, otherwise we use the tracked bounding-box size
float baseWidth, baseHeight;
if (tbb != NULL) {
baseWidth = (float)tbb[2];
baseHeight = (float)tbb[3];
//initBBWidth = baseWidth;
//initBBHeight = baseHeight;
} else {
baseWidth = initBBWidth;
baseHeight = initBBHeight;
}
if (baseWidth < 40 || baseHeight < 40) {
return new vector<double *>();
}
// Using the sliding-window approach, find positive matches to our object
// Vector of positive patch matches' bounding-boxes
vector<double *> *bbs = new vector<double *>();
// Minimum and maximum scales for the bounding-box, the number of scale
// iterations to make, and the amount to increment scale by each iteration
float minScale = 0.5f;
float maxScale = 1.5f;
int iterationsScale = 6;
float scaleInc = (maxScale - minScale) / (iterationsScale - 1);
// Loop through a range of bounding-box scales
for (float scale = minScale; scale <= maxScale; scale += scaleInc) {
int minX = 0;
int currentWidth = (int)(scale * baseWidth);
int maxX = width - currentWidth;
int iterationsX = 30;
int incX = (maxX - minX) / (iterationsX - 1);
// If bounding-box width >= frame width, make only 1 iteration of the
// following for loop
if (incX <= 0) {
maxX = 0;
incX = 1;
}
// Loop through all bounding-box top-left x-positions
for (int x = minX; x <= maxX; x += incX) {
// Same for y
int minY = 0;
int currentHeight = (int)(scale * baseHeight);
int maxY = height - currentHeight;
int iterationsY = 30;
int incY = (maxY - minY) / (iterationsY - 1);
// If bounding-box height >= frame height, make only 1 iteration
// of the following for loop
if (incY <= 0) {
maxY = 0;
incY = 1;
}
// Loop through all bounding-box top-left x-positions
for (int y = minY; y <= maxY; y += incY) {
// Classify the patch
float p = classifier->classify(frame, x, y, currentWidth, currentHeight);
// Store the patch data in an array
// [x, y, width, height, confidence, overlapping], where
// overlapping is 1 if the bounding-box overlaps with the
// tracked bounding box, otherwise 0
double *bb = new double[6];
bb[0] = (double)x;
bb[1] = (double)y;
bb[2] = (double)currentWidth;
bb[3] = (double)currentHeight;
bb[4] = (double)p;
if (tbb != NULL && bbOverlap(bb, tbb) > MIN_LEARNING_OVERLAP) {
bb[5] = 1;
} else {
bb[5] = 0;
}
// If positive, or negative and overlapping with the tracked
// bounding-box, add this bounding-box to our return list
if (p > 0.5f || bb[5] == 1) {
bbs->push_back(bb);
} else {
delete [] bb;
}
}
}
}
return bbs;
}
Detector::~Detector() {
}