-
Notifications
You must be signed in to change notification settings - Fork 45
/
HaarDetector.js
426 lines (379 loc) · 16.8 KB
/
HaarDetector.js
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
*
* HAAR Feature Detector
* @package FILTER.js
*
**/
!function(FILTER, undef) {
"use strict";
var stdMath = Math, Abs = stdMath.abs, Max = stdMath.max, Min = stdMath.min,
Floor = stdMath.floor, Round = stdMath.round, Sqrt = stdMath.sqrt,
TypedArray = FILTER.Util.Array.typed, TypedObj = FILTER.Util.Array.typed_obj,
MAX_FEATURES = 10, push = Array.prototype.push;
function by_area(r1, r2) {return r2.area-r1.area;}
/*
HAAR Feature Detector (Viola-Jones-Lienhart algorithm)
adapted from: https://github.com/foo123/HAAR.js
based on:
1. [Rapid Object Detection using a Boosted Cascade of Simple Features, Viola, Jones 2001](http://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf)
2. [An Extended Set of Haar-like Features for Rapid Object Detection, Lienhart, Maydt 2002](http://www.lienhart.de/Prof._Dr._Rainer_Lienhart/Source_Code_files/ICIP2002.pdf)
*/
FILTER.Create({
name: "HaarDetectorFilter"
// parameters
,_update: false // filter by itself does not alter image data, just processes information
,hasMeta: true
,noreuse: false
,haardata: null
,tolerance: 0.2
,baseScale: 1.0
,scaleIncrement: 1.25
,stepIncrement: 0.5
,minNeighbors: 1
,doCannyPruning: true
,cannyLow: 20
,cannyHigh: 100
,_haarchanged: false
// this is the filter constructor
,init: function(haardata, baseScale, scaleIncrement, stepIncrement, minNeighbors, doCannyPruning, tolerance) {
var self = this;
self.haardata = haardata || null;
self.baseScale = null == baseScale ? 1.0 : (+baseScale);
self.scaleIncrement = null == scaleIncrement ? 1.25 : (+scaleIncrement);
self.stepIncrement = null == stepIncrement ? 0.5 : (+stepIncrement);
self.minNeighbors = null == minNeighbors ? 1 : (+minNeighbors);
self.doCannyPruning = undef === doCannyPruning ? true : (!!doCannyPruning);
self.tolerance = null == tolerance ? 0.2 : (+tolerance);
self._haarchanged = !!self.haardata;
}
// support worker serialize/unserialize interface
,path: FILTER.Path
,dispose: function() {
var self = this;
self.haardata = null;
self.$super('dispose');
return self;
}
,haar: function(haardata) {
var self = this;
self.haardata = haardata;
self._haarchanged = true;
return self;
}
,params: function(params) {
var self = this;
if (params)
{
if (params.haardata)
{
self.haardata = params.haardata;
self._haarchanged = true;
}
if (null != params.baseScale) self.baseScale = +params.baseScale;
if (null != params.scaleIncrement) self.scaleIncrement = +params.scaleIncrement;
if (null != params.stepIncrement) self.stepIncrement = +params.stepIncrement;
if (null != params.minNeighbors) self.minNeighbors = +params.minNeighbors;
if (undef !== params.doCannyPruning) self.doCannyPruning = !!params.doCannyPruning;
if (null != params.tolerance) self.tolerance = +params.tolerance;
if (null != params.cannyLow) self.cannyLow = +params.cannyLow;
if (null != params.cannyHigh) self.cannyHigh = +params.cannyHigh;
if (null != params.selection) self.selection = params.selection || null;
if (undef !== params.noreuse) self.noreuse = !!params.noreuse;
}
return self;
}
,serialize: function() {
var self = this, json;
json = {
//haardata: null
baseScale: self.baseScale
,scaleIncrement: self.scaleIncrement
,stepIncrement: self.stepIncrement
,minNeighbors: self.minNeighbors
,doCannyPruning: self.doCannyPruning
,tolerance: self.tolerance
,cannyLow: self.cannyLow
,cannyHigh: self.cannyHigh
,noreuse: self.noreuse
};
// avoid unnecessary (large) data transfer
if (self._haarchanged)
{
json.haardata = TypedObj(self.haardata);
self._haarchanged = false;
}
return json;
}
,unserialize: function(params) {
var self = this;
if (params.haardata) self.haardata = TypedObj(params.haardata, 1);
self.baseScale = params.baseScale;
self.scaleIncrement = params.scaleIncrement;
self.stepIncrement = params.stepIncrement;
self.minNeighbors = params.minNeighbors;
self.doCannyPruning = params.doCannyPruning;
self.tolerance = params.tolerance;
self.cannyLow = params.cannyLow;
self.cannyHigh = params.cannyHigh;
self.noreuse = params.noreuse;
return self;
}
// detected objects are passed as filter metadata (if filter is run in parallel thread)
,metaData: function(serialisation) {
return serialisation && FILTER.isWorker ? TypedObj(this.meta) : this.meta;
}
,setMetaData: function(meta, serialisation) {
this.meta = serialisation && ("string" === typeof meta) ? TypedObj(meta, 1) : meta;
return this;
}
// this is the filter actual apply method routine
,apply: function(im, w, h, metaData) {
var self = this;
self.meta = {objects: []};
if (!self.haardata || !w || !h) return im;
var imLen = im.length, imSize = imLen>>>2,
selection = self.selection || null,
A32F = FILTER.Array32F,
SAT=null, SAT2=null, RSAT=null, GSAT=null,
x1, y1, x2, y2, xf, yf,
features, FilterUtil = FILTER.Util.Filter;
if (selection)
{
if (selection[4])
{
// selection is relative, make absolute
xf = w-1;
yf = h-1;
}
else
{
// selection is absolute
xf = 1;
yf = 1;
}
x1 = Min(w-1, Max(0, selection[0]*xf));
y1 = Min(h-1, Max(0, selection[1]*yf));
x2 = Min(w-1, Max(0, selection[2]*xf));
y2 = Min(h-1, Max(0, selection[3]*yf));
}
else
{
x1 = 0; y1 = 0;
x2 = w-1; y2 = h-1;
}
// NOTE: assume image is already grayscale
if (!self.noreuse && metaData && metaData.haarfilter_SAT)
{
SAT = metaData.haarfilter_SAT;
SAT2 = metaData.haarfilter_SAT2;
RSAT = metaData.haarfilter_RSAT;
}
else
{
// pre-compute <del>grayscale,</del> SAT, RSAT and SAT2
FilterUtil.sat(im, w, h, 2, 0, SAT=new A32F(imSize), SAT2=new A32F(imSize), RSAT=new A32F(imSize));
if (!self.noreuse && metaData)
{
metaData.haarfilter_SAT = SAT;
metaData.haarfilter_SAT2 = SAT2;
metaData.haarfilter_RSAT = RSAT;
}
}
// pre-compute integral canny gradient edges if needed
if (self.doCannyPruning)
{
if (!self.noreuse && metaData && metaData.haarfilter_GSAT)
{
GSAT = metaData.haarfilter_GSAT;
}
else
{
GSAT = FilterUtil.gradient(im, w, h, 2, 0, 1, 1);
if (!self.noreuse && metaData) metaData.haarfilter_GSAT = GSAT;
}
}
// synchronous detection loop
features = new Array(MAX_FEATURES); features.count = 0;
FilterUtil.haar_detect(features, w, h, x1, y1, x2, y2, self.haardata, self.baseScale, self.scaleIncrement, self.stepIncrement, SAT, RSAT, SAT2, GSAT, self.cannyLow, self.cannyHigh);
// truncate if needed
if (features.length > features.count) features.length = features.count;
// return results as meta
self.meta.objects = FilterUtil.merge_features(features, self.minNeighbors, self.tolerance).sort(by_area);
SAT = null; SAT2 = null; RSAT = null; GSAT = null;
// return im back
return im;
}
});
// private methods
function haar_detect(feats, w, h, sel_x1, sel_y1, sel_x2, sel_y2,
haar, baseScale, scaleIncrement, stepIncrement,
SAT, RSAT, SAT2, GSAT, cL, cH)
{
var thresholdEdgesDensity = null != GSAT,
selw = (sel_x2-sel_x1+1)|0, selh = (sel_y2-sel_y1+1)|0,
imSize = w*h, imArea1 = imSize-1,
haar_stages = haar.stages, sl = haar_stages.length,
sizex = haar.size1, sizey = haar.size2,
scale, maxScale, xstep, ystep, xsize, ysize,
startx, starty, startty, //minScale,
x, y, ty, tyw, tys, p0, p1, p2, p3, xl, yl, s, t,
bx, by, swh, inv_area,
total_x, total_x2, vnorm, edges_density, pass,
stage, threshold, trees, tl,
t, cur_node_ind, features, feature,
rects, nb_rects, thresholdf,
rect_sum, kr, r, x1, y1, x2, y2,
x3, y3, x4, y4, rw, rh, yw, yh, sum
//,satsum = FILTER.Util.Filter.satsum
;
bx=w-1; by=imSize-w;
startx = sel_x1|0; starty = sel_y1|0;
maxScale = Min(selw/*w*//sizex, selh/*h*//sizey);
//minScale = Max(selw/w, selh/h);
for (scale=baseScale/* *minScale*/; scale<=maxScale; scale*=scaleIncrement)
{
// Viola-Jones Single Scale Detector
xsize = (scale * sizex)|0;
xstep = (xsize * stepIncrement)|0;
ysize = (scale * sizey)|0;
ystep = (ysize * stepIncrement)|0;
//ysize = xsize; ystep = xstep;
tyw = ysize*w; tys = ystep*w;
startty = starty*tys;
xl = startx+selw-xsize; yl = starty+selh-ysize;
swh = xsize*ysize; //inv_area = 1.0/swh;
for (y=starty,ty=startty; y<yl; y+=ystep,ty+=tys)
{
for (x=startx; x<xl; x+=xstep)
{
p0 = x-1 + ty-w; p1 = p0 + xsize;
p2 = p0 + tyw; p3 = p2 + xsize;
// clamp
p0 = Min(imArea1,Max(0,p0));
p1 = Min(imArea1,Max(0,p1));
p2 = Min(imArea1,Max(0,p2));
p3 = Min(imArea1,Max(0,p3));
//x1 = x+xsize-1; y1 = y+ysize-1;
if (thresholdEdgesDensity)
{
// prune search space based on canny edges density
// i.e too few = no feature, too much = noise
// avoid overflow
edges_density = /*satsum(GSAT, w, h, x, y, x1, y1)/swh;*/(GSAT[p3] - GSAT[p2] - GSAT[p1] + GSAT[p0]) / swh;
if (edges_density < cL || edges_density > cH) continue;
}
// pre-compute some values for speed
// avoid overflow
total_x = /*satsum(SAT, w, h, x, y, x1, y1)/swh;*/(SAT[p3] - SAT[p2] - SAT[p1] + SAT[p0]) / swh;
// avoid overflow
total_x2 = /*satsum(SAT2, w, h, x, y, x1, y1)/swh;*/(SAT2[p3] - SAT2[p2] - SAT2[p1] + SAT2[p0]) / swh;
vnorm = total_x2 - total_x * total_x;
//vnorm = 1 < vnorm ? Sqrt(vnorm) : vnorm /*1*/;
if (0 >= vnorm) continue;
vnorm = Sqrt(vnorm);
pass = false;
for (s=0; s<sl; ++s)
{
// Viola-Jones HAAR-Stage evaluator
stage = haar_stages[s];
threshold = stage.thres;
trees = stage.trees; tl = trees.length;
sum = 0;
for (t=0; t<tl; ++t)
{
//
// inline the tree and leaf evaluators to avoid function calls per-loop (faster)
//
// Viola-Jones HAAR-Tree evaluator
features = trees[t].feats;
cur_node_ind = 0;
while (true)
{
feature = features[cur_node_ind];
// Viola-Jones HAAR-Leaf evaluator
rects = feature.rects;
nb_rects = rects.length;
thresholdf = feature.thres;
rect_sum = 0;
if (feature.tilt)
{
// tilted rectangle feature, Lienhart et al. extension
for (kr=0; kr<nb_rects; ++kr)
{
r = rects[kr];
// this produces better/larger features, possible rounding effects??
x1 = x + (scale * r[0])|0;
y1 = (y-1 + (scale * r[1])|0) * w;
x2 = x + (scale * (r[0] + r[2]))|0;
y2 = (y-1 + (scale * (r[1] + r[2]))|0) * w;
x3 = x + (scale * (r[0] - r[3]))|0;
y3 = (y-1 + (scale * (r[1] + r[3]))|0) * w;
x4 = x + (scale * (r[0] + r[2] - r[3]))|0;
y4 = (y-1 + (scale * (r[1] + r[2] + r[3]))|0) * w;
// clamp
x1 = Min(bx,Max(0,x1));
x2 = Min(bx,Max(0,x2));
x3 = Min(bx,Max(0,x3));
x4 = Min(bx,Max(0,x4));
y1 = Min(by,Max(0,y1));
y2 = Min(by,Max(0,y2));
y3 = Min(by,Max(0,y3));
y4 = Min(by,Max(0,y4));
// RSAT(x-h+w, y+w+h-1) + RSAT(x, y-1) - RSAT(x-h, y+h-1) - RSAT(x+w, y+w-1)
// x4 y4 x1 y1 x3 y3 x2 y2
rect_sum += r[4] * (RSAT[x4 + y4] - RSAT[x3 + y3] - RSAT[x2 + y2] + RSAT[x1 + y1]);
}
}
else
{
// orthogonal rectangle feature, Viola-Jones original
for (kr=0; kr<nb_rects; ++kr)
{
r = rects[kr];
// this produces better/larger features, possible rounding effects??
x1 = x-1 + (scale * r[0])|0;
x2 = x-1 + (scale * (r[0] + r[2]))|0;
y1 = w * (y-1 + (scale * r[1])|0);
y2 = w * (y-1 + (scale * (r[1] + r[3]))|0);
// clamp
x1 = Min(bx,Max(0,x1));
x2 = Min(bx,Max(0,x2));
y1 = Min(by,Max(0,y1));
y2 = Min(by,Max(0,y2));
// SAT(x-1, y-1) + SAT(x+w-1, y+h-1) - SAT(x-1, y+h-1) - SAT(x+w-1, y-1)
// x1 y1 x2 y2 x1 y1 x2 y1
rect_sum += r[4] * /*satsum(SAT, w, h, x+(scale * r[0])|0, y+(scale * r[1])|0, x+(scale * (r[0] + r[2]))|0, y+(scale * (r[1] + r[3]))|0);*/(SAT[x2 + y2] - SAT[x1 + y2] - SAT[x2 + y1] + SAT[x1 + y1]);
}
}
/*where = rect_sum * inv_area < thresholdf * vnorm ? 0 : 1;*/
// END Viola-Jones HAAR-Leaf evaluator
if (rect_sum < swh * thresholdf * vnorm)
{
if (feature.has_l) {sum += feature.l_val; break;}
else {cur_node_ind = feature.l_node;}
}
else
{
if (feature.has_r) {sum += feature.r_val; break;}
else {cur_node_ind = feature.r_node;}
}
}
// END Viola-Jones HAAR-Tree evaluator
}
pass = sum > threshold;
// END Viola-Jones HAAR-Stage evaluator
if (!pass) break;
}
if (pass)
{
// expand
if (feats.count === feats.length) push.apply(feats, new Array(MAX_FEATURES));
// x, y, width, height
feats[feats.count++] = {x:x, y:y, width:xsize, height:ysize, score:sum};
}
}
}
}
}
FILTER.Util.Filter.haar_detect = haar_detect;
}(FILTER);