-
Notifications
You must be signed in to change notification settings - Fork 4
/
colortracker.js
427 lines (366 loc) · 12.7 KB
/
colortracker.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
427
(function () {
function rectIntersects(x0, y0, x1, y1, x2, y2, x3, y3) {
return !(x2 > x1 || x3 < x0 || y2 > y1 || y3 < y0);
};
/**
* ColorTracker utility to track colored blobs in a frame using color
* difference evaluation.
* @constructor
* @param {string|Array.<string>} opt_colors Optional colors to track.
*/
var tracker = function (opt_colors) {
if (typeof opt_colors === 'string') {
opt_colors = [opt_colors];
}
if (opt_colors) {
opt_colors.forEach(function (color) {
if (!tracker.getColor(color)) {
throw new Error('Color not valid, try `new tracker("magenta")`.');
}
});
this.setColors(opt_colors);
}
};
/**
* Holds the known colors.
* @type {Object.<string, function>}
* @private
* @static
*/
tracker.knownColors_ = {};
/**
* Caches coordinates values of the neighbours surrounding a pixel.
* @type {Object.<number, Int32Array>}
* @private
* @static
*/
tracker.neighbours_ = {};
/**
* Registers a color as known color.
* @param {string} name The color name.
* @param {function} fn The color function to test if the passed (r,g,b) is
* the desired color.
* @static
*/
tracker.registerColor = function (name, fn) {
tracker.knownColors_[name] = fn;
};
/**
* Gets the known color function that is able to test whether an (r,g,b) is
* the desired color.
* @param {string} name The color name.
* @return {function} The known color test function.
* @static
*/
tracker.getColor = function (name) {
return tracker.knownColors_[name];
};
/**
* Holds the colors to be tracked by the `ColorTracker` instance.
* @default ['magenta']
* @type {Array.<string>}
*/
tracker.prototype.colors = ['magenta'];
/**
* Holds the minimum dimension to classify a rectangle.
* @default 20
* @type {number}
*/
tracker.prototype.minDimension = 20;
/**
* Holds the maximum dimension to classify a rectangle.
* @default Infinity
* @type {number}
*/
tracker.prototype.maxDimension = Infinity;
/**
* Holds the minimum group size to be classified as a rectangle.
* @default 30
* @type {number}
*/
tracker.prototype.minGroupSize = 30;
/**
* Calculates the central coordinate from the cloud points. The cloud points
* are all points that matches the desired color.
* @param {Array.<number>} cloud Major row order array containing all the
* points from the desired color, e.g. [x1, y1, c2, y2, ...].
* @param {number} total Total numbers of pixels of the desired color.
* @return {object} Object containing the x, y and estimated z coordinate of
* the blog extracted from the cloud points.
* @private
*/
tracker.prototype.calculateDimensions_ = function (cloud, total) {
var maxx = -1;
var maxy = -1;
var minx = Infinity;
var miny = Infinity;
for (var c = 0; c < total; c += 2) {
var x = cloud[c];
var y = cloud[c + 1];
if (x < minx) {
minx = x;
}
if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
}
if (y > maxy) {
maxy = y;
}
}
return {
width: maxx - minx,
height: maxy - miny,
x: minx,
y: miny
};
};
/**
* Gets the colors being tracked by the `ColorTracker` instance.
* @return {Array.<string>}
*/
tracker.prototype.getColors = function () {
return this.colors;
};
/**
* Gets the minimum dimension to classify a rectangle.
* @return {number}
*/
tracker.prototype.getMinDimension = function () {
return this.minDimension;
};
/**
* Gets the maximum dimension to classify a rectangle.
* @return {number}
*/
tracker.prototype.getMaxDimension = function () {
return this.maxDimension;
};
/**
* Gets the minimum group size to be classified as a rectangle.
* @return {number}
*/
tracker.prototype.getMinGroupSize = function () {
return this.minGroupSize;
};
/**
* Gets the eight offset values of the neighbours surrounding a pixel.
* @param {number} width The image width.
* @return {array} Array with the eight offset values of the neighbours
* surrounding a pixel.
* @private
*/
tracker.prototype.getNeighboursForWidth_ = function (width) {
if (tracker.neighbours_[width]) {
return tracker.neighbours_[width];
}
var neighbours = new Int32Array(8);
neighbours[0] = -width * 4;
neighbours[1] = -width * 4 + 4;
neighbours[2] = 4;
neighbours[3] = width * 4 + 4;
neighbours[4] = width * 4;
neighbours[5] = width * 4 - 4;
neighbours[6] = -4;
neighbours[7] = -width * 4 - 4;
tracker.neighbours_[width] = neighbours;
return neighbours;
};
/**
* Unites groups whose bounding box intersect with each other.
* @param {Array.<Object>} rects
* @private
*/
tracker.prototype.mergeRectangles_ = function (rects) {
var intersects;
var results = [];
var minDimension = this.getMinDimension();
var maxDimension = this.getMaxDimension();
for (var r = 0; r < rects.length; r++) {
var r1 = rects[r];
intersects = true;
for (var s = r + 1; s < rects.length; s++) {
var r2 = rects[s];
if (rectIntersects(r1.x, r1.y, r1.x + r1.width, r1.y + r1.height, r2.x, r2.y, r2.x + r2.width, r2.y + r2.height)) {
intersects = false;
var x1 = Math.min(r1.x, r2.x);
var y1 = Math.min(r1.y, r2.y);
var x2 = Math.max(r1.x + r1.width, r2.x + r2.width);
var y2 = Math.max(r1.y + r1.height, r2.y + r2.height);
r2.height = y2 - y1;
r2.width = x2 - x1;
r2.x = x1;
r2.y = y1;
break;
}
}
if (intersects) {
if (r1.width >= minDimension && r1.height >= minDimension) {
if (r1.width <= maxDimension && r1.height <= maxDimension) {
results.push(r1);
}
}
}
}
return results;
};
/**
* Sets the colors to be tracked by the `ColorTracker` instance.
* @param {Array.<string>} colors
*/
tracker.prototype.setColors = function (colors) {
this.colors = colors;
};
/**
* Sets the minimum dimension to classify a rectangle.
* @param {number} minDimension
*/
tracker.prototype.setMinDimension = function (minDimension) {
this.minDimension = minDimension;
};
/**
* Sets the maximum dimension to classify a rectangle.
* @param {number} maxDimension
*/
tracker.prototype.setMaxDimension = function (maxDimension) {
this.maxDimension = maxDimension;
};
/**
* Sets the minimum group size to be classified as a rectangle.
* @param {number} minGroupSize
*/
tracker.prototype.setMinGroupSize = function (minGroupSize) {
this.minGroupSize = minGroupSize;
};
/**
* Tracks the `Video` frames. This method is called for each video frame in
* order to emit `track` event.
* @param {Uint8ClampedArray} pixels The pixels data to track.
* @param {number} width The pixels canvas width.
* @param {number} height The pixels canvas height.
*/
tracker.prototype.track = function (pixels, width, height) {
var self = this;
var colors = this.getColors();
if (!colors) {
throw new Error('Colors not specified, try `new tracker("magenta")`.');
}
var results = [];
colors.forEach(function (color) {
results = results.concat(self.trackColor_(pixels, width, height, color));
});
return new Promise(function(resolve, reject) {
resolve(results);
});
};
/**
* Find the given color in the given matrix of pixels using Flood fill
* algorithm to determines the area connected to a given node in a
* multi-dimensional array.
* @param {Uint8ClampedArray} pixels The pixels data to track.
* @param {number} width The pixels canvas width.
* @param {number} height The pixels canvas height.
* @param {string} color The color to be found
* @private
*/
tracker.prototype.trackColor_ = function (pixels, width, height, color) {
var colorFn = tracker.knownColors_[color];
var currGroup = new Int32Array(pixels.length >> 2);
var currGroupSize;
var currI;
var currJ;
var currW;
var marked = new Int8Array(pixels.length);
var minGroupSize = this.getMinGroupSize();
var neighboursW = this.getNeighboursForWidth_(width);
var queue = new Int32Array(pixels.length);
var queuePosition;
var results = [];
var w = -4;
if (!colorFn) {
return results;
}
for (var i = 0; i < height; i++) {
for (var j = 0; j < width; j++) {
w += 4;
if (marked[w]) {
continue;
}
currGroupSize = 0;
queuePosition = -1;
queue[++queuePosition] = w;
queue[++queuePosition] = i;
queue[++queuePosition] = j;
marked[w] = 1;
while (queuePosition >= 0) {
currJ = queue[queuePosition--];
currI = queue[queuePosition--];
currW = queue[queuePosition--];
if (colorFn(pixels[currW], pixels[currW + 1], pixels[currW + 2], pixels[currW + 3], currW, currI, currJ)) {
currGroup[currGroupSize++] = currJ;
currGroup[currGroupSize++] = currI;
for (var k = 0; k < neighboursW.length; k++) {
var otherW = currW + neighboursW[k];
var otherI = currI + neighboursI[k];
var otherJ = currJ + neighboursJ[k];
if (!marked[otherW] && otherI >= 0 && otherI < height && otherJ >= 0 && otherJ < width) {
queue[++queuePosition] = otherW;
queue[++queuePosition] = otherI;
queue[++queuePosition] = otherJ;
marked[otherW] = 1;
}
}
}
}
if (currGroupSize >= minGroupSize) {
var data = this.calculateDimensions_(currGroup, currGroupSize);
if (data) {
data.color = color;
results.push(data);
}
}
}
}
return this.mergeRectangles_(results);
};
// Default colors
//===================
tracker.registerColor('cyan', function (r, g, b) {
var thresholdGreen = 50,
thresholdBlue = 70,
dx = r - 0,
dy = g - 255,
dz = b - 255;
if ((g - r) >= thresholdGreen && (b - r) >= thresholdBlue) {
return true;
}
return dx * dx + dy * dy + dz * dz < 6400;
});
tracker.registerColor('magenta', function (r, g, b) {
var threshold = 50,
dx = r - 255,
dy = g - 0,
dz = b - 255;
if ((r - g) >= threshold && (b - g) >= threshold) {
return true;
}
return dx * dx + dy * dy + dz * dz < 19600;
});
tracker.registerColor('yellow', function (r, g, b) {
var threshold = 50,
dx = r - 255,
dy = g - 255,
dz = b - 0;
if ((r - b) >= threshold && (g - b) >= threshold) {
return true;
}
return dx * dx + dy * dy + dz * dz < 10000;
});
// Caching neighbour i/j offset values.
//=====================================
var neighboursI = new Int32Array([-1, -1, 0, 1, 1, 1, 0, -1]);
var neighboursJ = new Int32Array([0, 1, 1, 1, 0, -1, -1, -1]);
module.exports = tracker;
}());