forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc4-pedestrian-detector.cpp
More file actions
291 lines (262 loc) · 9.42 KB
/
c4-pedestrian-detector.cpp
File metadata and controls
291 lines (262 loc) · 9.42 KB
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
#include "c4-pedestrian-detector.h"
/*****************************************/
// Pedestrian_ICRA.cpp
/*****************************************/
// ---------------------------------------------------------------------
// Helper functions
// compute the Sobel image "ct" from "original"
void ComputeCT(IntImage<double>& original,IntImage<int>& ct)
{
ct.Create(original.nrow,original.ncol);
for(int i=2; i<original.nrow-2; i++)
{
double* p1 = original.p[i-1];
double* p2 = original.p[i];
double* p3 = original.p[i+1];
int* ctp = ct.p[i];
for(int j=2; j<original.ncol-2; j++)
{
int index = 0;
if(p2[j]<=p1[j-1]) index += 0x80;
if(p2[j]<=p1[j]) index += 0x40;
if(p2[j]<=p1[j+1]) index += 0x20;
if(p2[j]<=p2[j-1]) index += 0x10;
if(p2[j]<=p2[j+1]) index += 0x08;
if(p2[j]<=p3[j-1]) index += 0x04;
if(p2[j]<=p3[j]) index += 0x02;
if(p2[j]<=p3[j+1]) index ++;
ctp[j] = index;
}
}
}
// Load SVM models -- linear SVM trained using LIBLINEAR
double UseSVM_CD_FastEvaluationStructure(const char* modelfile, const int m, Array2dC<double>& result)
{
std::ifstream in(modelfile);
if(in.good()==false)
{
std::cout<<"SVM model "<<modelfile<<" can not be loaded."<<std::endl;
exit(-1);
}
std::string buffer;
std::getline(in,buffer); // first line
std::getline(in,buffer); // second line
std::getline(in,buffer); // third line
in>>buffer;
assert(buffer=="nr_feature");
int num_dim = m;
in>>num_dim;
assert(num_dim>0 && num_dim==m);
std::getline(in,buffer); // end of line 4
in>>buffer;
assert(buffer=="bias");
int bias;
in>>bias;
std::getline(in,buffer); //end of line 5;
in>>buffer;
assert(buffer=="w");
std::getline(in,buffer); //end of line 6
result.Create(1,num_dim);
for(int i=0; i<num_dim; i++) in>>result.buf[i];
double rho = 0;
if(bias>=0) in>>rho;
in.close();
return rho;
}
// Load SVM models -- Histogram Intersectin Kernel SVM trained by libHIK
double UseSVM_CD_FastEvaluationStructure(const char* modelfile, const int m, const int upper_bound, Array2dC<double>& result)
{
std::ifstream fs(modelfile, std::fstream::binary);
if( !fs.is_open() )
{
std::cout << "SVM model " << modelfile << " can not be loaded." << std::endl;
exit(-1);
}
// Header
int rows, cols, type, channels;
fs.read((char*)&rows, sizeof(int)); // rows
fs.read((char*)&cols, sizeof(int)); // cols
fs.read((char*)&type, sizeof(int)); // type
fs.read((char*)&channels, sizeof(int)); // channels
// Data
cv::Mat mat(rows, cols, type);
fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);
int num_dim = m;
result.Create(num_dim, upper_bound);
for(int i=0; i<num_dim; i++)
for (int j = 0; j < upper_bound; j++)
{
result.p[i][j]= mat.at<double>(i, j);
}
return -0.00455891;
}
// End of Helper functions
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Functions that load the two classifiers
void LoadCascade(std::string cascade1, std::string cascade2, DetectionScanner& ds)
{
std::vector<NodeDetector::NodeType> types;
std::vector<int> upper_bounds;
std::vector<std::string> filenames;
types.push_back(NodeDetector::CD_LIN); // first node
upper_bounds.push_back(100);
filenames.push_back(cascade1);
types.push_back(NodeDetector::CD_HIK); // second node
upper_bounds.push_back(353);
filenames.push_back(cascade2);
ds.LoadDetector(types,upper_bounds,filenames);
// You can adjust these parameters for different speed, accuracy etc
ds.cascade->nodes[0]->thresh += 0.8;
ds.cascade->nodes[1]->thresh -= 0.095;
}
void DetectionScanner::LoadDetector(std::vector<NodeDetector::NodeType>& types,std::vector<int>& upper_bounds,std::vector<std::string>& filenames)
{
size_t depth = types.size();
assert(depth>0 && depth==upper_bounds.size() && depth==filenames.size());
if(cascade)
delete cascade;
cascade = new CascadeDetector;
assert(xdiv>0 && ydiv>0);
for(size_t i=0; i<depth; i++)
cascade->AddNode(types[i],(xdiv-EXT)*(ydiv-EXT)*baseflength,upper_bounds[i],filenames[i].c_str());
hist.Create(1,baseflength*(xdiv-EXT)*(ydiv-EXT));
}
void NodeDetector::Load(const NodeType _type,const int _featurelength,const int _upper_bound,const int _index,const char* _filename)
{
type = _type;
index = _index;
filename = _filename;
featurelength = _featurelength;
upper_bound = _upper_bound;
if(type==CD_LIN)
thresh = UseSVM_CD_FastEvaluationStructure(_filename,_featurelength,classifier);
else if(type==CD_HIK)
thresh = UseSVM_CD_FastEvaluationStructure(_filename,_featurelength,upper_bound,classifier);
if(type==CD_LIN) type = LINEAR;
if(type==CD_HIK) type = HISTOGRAM;
}
void CascadeDetector::AddNode(const NodeDetector::NodeType _type,const int _featurelength,const int _upper_bound,const char* _filename)
{
if(length==size)
{
int newsize = size * 2;
NodeDetector** p = new NodeDetector*[newsize];
assert(p!=NULL);
std::copy(nodes,nodes+size,p);
size = newsize;
delete[] nodes;
nodes = p;
}
nodes[length] = new NodeDetector(_type,_featurelength,_upper_bound,length,_filename);
length++;
}
// End of functions that load the two classifiers
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Detection functions
// initialization -- compute the Census Tranform image for CENTRIST
void DetectionScanner::InitImage(IntImage<double>& original)
{
image = original;
image.Sobel(sobel,false,false);
ComputeCT(sobel,ct);
}
// combine the (xdiv-1)*(ydiv-1) integral images into a single one
void DetectionScanner::InitIntegralImages(const int stepsize)
{
if(cascade->nodes[0]->type!=NodeDetector::LINEAR)
return; // No need to prepare integral images
const int hd = height/xdiv*2-2;
const int wd = width/ydiv*2-2;
scores.Create(ct.nrow,ct.ncol);
scores.Zero(cascade->nodes[0]->thresh/hd/wd);
double* linearweights = cascade->nodes[0]->classifier.buf;
for(int i=0; i<xdiv-EXT; i++)
{
const int xoffset = height/xdiv*i;
for(int j=0; j<ydiv-EXT; j++)
{
const int yoffset = width/ydiv*j;
for(int x=2; x<ct.nrow-2-xoffset; x++)
{
int* ctp = ct.p[x+xoffset]+yoffset;
double* tempp = scores.p[x];
for(int y=2; y<ct.ncol-2-yoffset; y++)
tempp[y] += linearweights[ctp[y]];
}
linearweights += baseflength;
}
}
scores.CalcIntegralImageInPlace();
for(int i=2; i<ct.nrow-2-height; i+=stepsize)
{
double* p1 = scores.p[i];
double* p2 = scores.p[i+hd];
for(int j=2; j<ct.ncol-2-width; j+=stepsize)
p1[j] += (p2[j+wd] - p2[j] - p1[j+wd]);
}
}
// Resize the input image and then re-compute Sobel image etc
void DetectionScanner::ResizeImage()
{
image.Resize(sobel,ratio);
image.Swap(sobel);
image.Sobel(sobel,false,false);
ComputeCT(sobel,ct);
}
// The function that does the real detection
int DetectionScanner::FastScan(IntImage<double>& original,std::vector<cv::Rect>& results,const int stepsize)
{
if(original.nrow<height+5 || original.ncol<width+5) return 0;
const int hd = height/xdiv;
const int wd = width/ydiv;
InitImage(original);
results.clear();
hist.Create(1,baseflength*(xdiv-EXT)*(ydiv-EXT));
NodeDetector* node = cascade->nodes[1];
double** pc = node->classifier.p;
int oheight = original.nrow, owidth = original.ncol;
cv::Rect rect;
while(image.nrow>=height && image.ncol>=width)
{
InitIntegralImages(stepsize);
for(int i=2; i+height<image.nrow-2; i+=stepsize)
{
const double* sp = scores.p[i];
for(int j=2; j+width<image.ncol-2; j+=stepsize)
{
if(sp[j]<=0) continue;
int* p = hist.buf;
hist.Zero();
for(int k=0; k<xdiv-EXT; k++)
{
for(int t=0; t<ydiv-EXT; t++)
{
for(int x=i+k*hd+1; x<i+(k+1+EXT)*hd-1; x++)
{
int* ctp = ct.p[x];
for(int y=j+t*wd+1; y<j+(t+1+EXT)*wd-1; y++)
p[ctp[y]]++;
}
p += baseflength;
}
}
double score = node->thresh;
for(int k=0; k<node->classifier.nrow; k++) score += pc[k][hist.buf[k]];
if(score>0)
{
rect.y = i * oheight / image.nrow;
rect.height = (oheight * height) / image.nrow + 1;
rect.x = j * owidth / image.ncol;
rect.width = (width * owidth) /image.ncol + 1;
results.push_back(rect);
}
}
}
ResizeImage();
}
return 0;
}
// End of Detection functions
// ---------------------------------------------------------------------