-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtldTracker.hpp
executable file
·270 lines (240 loc) · 7.47 KB
/
tldTracker.hpp
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
/***********************************************************************
$FILENAME : tldTracker.hpp
$TITLE : TLD tracker class definition
$DATE : 7 Nov 2017
$VERSION : 1.0.0
$DESCRIPTION : Defines the TLD tracker class
$AUTHOR : Armin Zare Zadeh (ali.a.zarezadeh @ gmail.com)
************************************************************************/
#ifndef TLD_CPU_OCL_H_
#define TLD_CPU_OCL_H_
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#ifdef USE_OCL_
#include "opencv2/ocl/ocl.hpp"
#endif
#include <fstream>
#include <list>
#include "Constants.h"
#include "main.hpp"
using namespace std;
using namespace cv;
#ifdef USE_OCL_
using namespace cv::ocl;
#endif
typedef std::list<Mat> PX;
typedef std::list<Mat> PEX;
typedef std::list<Mat> NEX;
class trackerApp;
namespace tld
{
#ifndef USE_OCL_
// CPU Version
namespace cpu
{
class LucasKanade
{
public:
LucasKanade ( InputArray img );
virtual ~LucasKanade ();
void lk( InputArray imgI, InputArray imgJ, InputArray ptsI, InputArray ptsJ, OutputArray out );
private:
// void loadImageFromMatlab(const mxArray *mxImage, IplImage *image);
void euclideanDistance (InputArray point1, InputArray point2, OutputArray match);
void normCrossCorrelation(InputArray points0, InputArray points1, InputArray status, OutputArray match, const int winsize);
int win_size;
int nPts;
//CvPoint2D32f* points[3];
vector<Point2f> points[3];
Mat IMG_I;
Mat IMG_J;
Mat PYR_I;
Mat PYR_J;
};
class Fern
{
public:
Fern ( );
virtual ~Fern ();
void init( InputArray img, InputArray grid, InputArray features, InputArray scales );
void update( InputArray X, InputArray Y, const float Margin, const int bootstrap );
void update( InputArray X, InputArray Y, const float Margin, const int bootstrap, InputArray idx );
void evaluate( InputArray X, OutputArray out );
void detect( InputArray img_gray, InputArray img_blur, const int maxBBox, const float minVar, OutputArray conf, OutputArray patt );
void getPattern( InputArray img_gray, InputArray img_blur, InputArray idx, const float minVar, OutputArray pattern, OutputArray status );
private:
void integralImage(InputArray image);
void update_(InputArray x, const int col, const int C, const int N);
float measureForest(InputArray idx, const int col);
int measureTreeOffset (InputArray img, const int idx_bbox, const int idx_tree);
float bboxVarOffset(const int col);
float measureBBoxOffset (InputArray blur, const int idx_bbox, const float minVar, OutputArray tPatt, const int col);
float randdouble ( );
void saveWEIGHT(void);
void saveOFF(const int BBOX_a_x, const int BBOX_a_y, const int BBOX_d_x, const int BBOX_d_y, const int OFF_0_x, const int OFF_0_y, const int OFF_1_x, const int OFF_1_y);
#ifdef PROFILING_ON_
clock_t start, stop;
float elapsedTime;
time_t nowis;
tm* localtm;
char timeBuffer[256];
#endif
float thrN;
int nTREES;
int nFEAT;
int nSCALE;
int iHEIGHT;
int iWIDTH;
Mat BBOX;
Mat OFF;
Mat integral_img;
Mat integral_img2;
Mat WEIGHT;
Mat nP;
Mat nN;
int nBIT; // number of bits per feature
int num_features_bit;
};
class tracker {
public:
tracker( trackerApp* );
virtual ~tracker();
void process();
public:
// Median frame by frame Tracker & Detector
struct{
Rect bbox_rect;
Mat curBBox;
Mat prvBBox;
float curConf;
float prvConf;
int curSize;
int prvSize;
int prvValid;
int curValid;
Mat xFI;
Mat xFII;
Mat xFJ;
Mat xFJJ;
int numM; // numMxnumN grid of points within BBox
int numN;
int numMN;
Mat idxF_;
int* idxF;
Mat tBB;
int tValid;
float tConf;
Mat dBB;
Mat dConf;
int DTLen;
int DT;
int DTNum;
int TR;
#ifdef FERN_OPENCV_ON_
Mat object;
bool DTFound;
vector<int> pairs;
vector<Point2f> dst_corners;
vector<KeyPoint> objKeypoints, imgKeypoints;
#endif
}TRDT;
private:
void init ( );
void initFirstFrame ( );
void display ( );
void generateFeatures ( const int nTREES, const int nFEAT, OutputArray features );
void generatePositiveData ( InputArray overlap, const int flag, OutputArray pX, OutputArray pEx, OutputArray bbP );
void generateNegativeData ( InputArray overlap, OutputArray nX, OutputArray nEx );
void splitNegativeData ( InputArray nX, InputArray nEx, OutputArray nX1, OutputArray nX2, OutputArray nEx1, OutputArray nEx2 );
void trainNearestNeighbor ( InputArray pEx, InputArray nEx );
void NearestNeighbor_1D (InputArray x, const int col, const int pex_isempty, const int nex_isempty, OutputArray isin, float &conf1, float &conf2);
void NearestNeighbor ( InputArray x, OutputArray isin, OutputArray conf1, OutputArray conf2 );
void getPattern ( InputArray bb, OutputArray pattern );
void getPattern_1D ( InputArray bb, const int col, OutputArray pattern );
void patch2Pattern ( InputArray patch, const int *patchsize, OutputArray pattern, const int col);
void processFrame ( );
void tracking ( );
void detection ( );
void learning ( );
#ifdef FERN_OPENCV_ON_
void ObjectDetectorTrain( );
void ObjectDetector( );
#endif
#ifdef PROFILING_ON_
clock_t start, stop;
float elapsedTime;
time_t nowis;
tm* localtm;
char timeBuffer[256];
#endif
trackerApp* m_pApp;
Mat tmpBlurImg;
bool initDone_;
int count_;
Size imgsize;
public:
#ifdef PROFILING_ON_
ofstream profilingLogFile;
#endif
// Fern DETECTOR
struct {
Mat grid;
int nGrid; // grid length
Mat scales;
struct {
Mat x;
int type;
}features;
// Temporal structures
struct {
Mat conf;
Mat patt;
}tmp; // temporary storage for confidence and pattern calculations
struct{
Mat bb; // bounding boxes
Mat patt; // corresponding codes of the Ensemble Classifier
Mat idx; // indexes of detected bounding boxes within the scanning grid
Mat conf1; // Relative Similarity (for final nearest neighbor classifier)
Mat conf2; // Conservative Similarity (for integration with tracker)
Mat isin; // detected (isin=1) or rejected (isin=0) by nearest neighbor classifier
Mat patch; // Corresponding patches
int num_dt;
}dt;
}detector;
private:
// TRAINER
struct {
Mat X; // training data for fern
Mat pEx; // training data for NearestNeighbor
Mat Y;
Mat nEx;
float var; // Variance threshold
Mat pex;
Mat nex;
}trainer;
Mat patchPatt1D;
Mat isin_NN1D;
LucasKanade *lucaskanade;
Fern *fern;
#ifdef FERN_OPENCV_ON_
LDetector ldetector;
PlanarObjectDetector detector_;
int blurKSize;
double sigma;
#endif
};
}
#endif
#ifdef USE_OCL_
// OpenCL (OCL) Version
namespace ocl
{
class tracker {
public:
tracker( trackerApp* );
virtual ~tracker();
};
}
#endif
}
#endif /* TLD_CPU_OCL_H_ */