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
// //////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
Module['imread'] = function(imageSource) {
var img = null;
if (typeof imageSource === 'string') {
img = document.getElementById(imageSource);
} else {
img = imageSource;
}
var canvas = null;
var ctx = null;
if (img instanceof HTMLImageElement) {
canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
} else if (img instanceof HTMLCanvasElement) {
canvas = img;
ctx = canvas.getContext('2d');
} else {
throw new Error('Please input the valid canvas or img id.');
return;
}
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
return cv.matFromImageData(imgData);
};
Module['imshow'] = function(canvasSource, mat) {
var canvas = null;
if (typeof canvasSource === 'string') {
canvas = document.getElementById(canvasSource);
} else {
canvas = canvasSource;
}
if (!(canvas instanceof HTMLCanvasElement)) {
throw new Error('Please input the valid canvas element or id.');
return;
}
if (!(mat instanceof cv.Mat)) {
throw new Error('Please input the valid cv.Mat instance.');
return;
}
// convert the mat type to cv.CV_8U
var img = new cv.Mat();
var depth = mat.type()%8;
var scale = depth <= cv.CV_8S? 1.0 : (depth <= cv.CV_32S? 1.0/256.0 : 255.0);
var shift = (depth === cv.CV_8S || depth === cv.CV_16S)? 128.0 : 0.0;
mat.convertTo(img, cv.CV_8U, scale, shift);
// convert the img type to cv.CV_8UC4
switch (img.type()) {
case cv.CV_8UC1:
cv.cvtColor(img, img, cv.COLOR_GRAY2RGBA);
break;
case cv.CV_8UC3:
cv.cvtColor(img, img, cv.COLOR_RGB2RGBA);
break;
case cv.CV_8UC4:
break;
default:
throw new Error('Bad number of channels (Source image must have 1, 3 or 4 channels)');
return;
}
var imgData = new ImageData(new Uint8ClampedArray(img.data), img.cols, img.rows);
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = imgData.width;
canvas.height = imgData.height;
ctx.putImageData(imgData, 0, 0);
img.delete();
};
Module['VideoCapture'] = function(videoSource) {
var video = null;
if (typeof videoSource === 'string') {
video = document.getElementById(videoSource);
} else {
video = videoSource;
}
if (!(video instanceof HTMLVideoElement)) {
throw new Error('Please input the valid video element or id.');
return;
}
var canvas = document.createElement('canvas');
canvas.width = video.width;
canvas.height = video.height;
var ctx = canvas.getContext('2d');
this.video = video;
this.read = function(frame) {
if (!(frame instanceof cv.Mat)) {
throw new Error('Please input the valid cv.Mat instance.');
return;
}
if (frame.type() !== cv.CV_8UC4) {
throw new Error('Bad type of input mat: the type should be cv.CV_8UC4.');
return;
}
if (frame.cols !== video.width || frame.rows !== video.height) {
throw new Error('Bad size of input mat: the size should be same as the video.');
return;
}
ctx.drawImage(video, 0, 0, video.width, video.height);
frame.data.set(ctx.getImageData(0, 0, video.width, video.height).data);
};
};
function Range(start, end) {
this.start = typeof(start) === 'undefined' ? 0 : start;
this.end = typeof(end) === 'undefined' ? 0 : end;
}
Module['Range'] = Range;
function Point(x, y) {
this.x = typeof(x) === 'undefined' ? 0 : x;
this.y = typeof(y) === 'undefined' ? 0 : y;
}
Module['Point'] = Point;
function Size(width, height) {
this.width = typeof(width) === 'undefined' ? 0 : width;
this.height = typeof(height) === 'undefined' ? 0 : height;
}
Module['Size'] = Size;
function Rect() {
switch (arguments.length) {
case 0: {
// new cv.Rect()
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
break;
}
case 1: {
// new cv.Rect(rect)
var rect = arguments[0];
this.x = rect.x;
this.y = rect.y;
this.width = rect.width;
this.height = rect.height;
break;
}
case 2: {
// new cv.Rect(point, size)
var point = arguments[0];
var size = arguments[1];
this.x = point.x;
this.y = point.y;
this.width = size.width;
this.height = size.height;
break;
}
case 4: {
// new cv.Rect(x, y, width, height)
this.x = arguments[0];
this.y = arguments[1];
this.width = arguments[2];
this.height = arguments[3];
break;
}
default: {
throw new Error('Invalid arguments');
}
}
}
Module['Rect'] = Rect;
function RotatedRect() {
switch (arguments.length) {
case 0: {
this.center = {x: 0, y: 0};
this.size = {width: 0, height: 0};
this.angle = 0;
break;
}
case 3: {
this.center = arguments[0];
this.size = arguments[1];
this.angle = arguments[2];
break;
}
default: {
throw new Error('Invalid arguments');
}
}
}
RotatedRect.points = function(obj) {
return Module.rotatedRectPoints(obj);
};
RotatedRect.boundingRect = function(obj) {
return Module.rotatedRectBoundingRect(obj);
};
RotatedRect.boundingRect2f = function(obj) {
return Module.rotatedRectBoundingRect2f(obj);
};
Module['RotatedRect'] = RotatedRect;
function Scalar(v0, v1, v2, v3) {
this.push(typeof(v0) === 'undefined' ? 0 : v0);
this.push(typeof(v1) === 'undefined' ? 0 : v1);
this.push(typeof(v2) === 'undefined' ? 0 : v2);
this.push(typeof(v3) === 'undefined' ? 0 : v3);
}
Scalar.prototype = new Array; // eslint-disable-line no-array-constructor
Scalar.all = function(v) {
return new Scalar(v, v, v, v);
};
Module['Scalar'] = Scalar;
function MinMaxLoc() {
switch (arguments.length) {
case 0: {
this.minVal = 0;
this.maxVal = 0;
this.minLoc = new Point();
this.maxLoc = new Point();
break;
}
case 4: {
this.minVal = arguments[0];
this.maxVal = arguments[1];
this.minLoc = arguments[2];
this.maxLoc = arguments[3];
break;
}
default: {
throw new Error('Invalid arguments');
}
}
}
Module['MinMaxLoc'] = MinMaxLoc;
function Circle() {
switch (arguments.length) {
case 0: {
this.center = new Point();
this.radius = 0;
break;
}
case 2: {
this.center = arguments[0];
this.radius = arguments[1];
break;
}
default: {
throw new Error('Invalid arguments');
}
}
}
Module['Circle'] = Circle;
function TermCriteria() {
switch (arguments.length) {
case 0: {
this.type = 0;
this.maxCount = 0;
this.epsilon = 0;
break;
}
case 3: {
this.type = arguments[0];
this.maxCount = arguments[1];
this.epsilon = arguments[2];
break;
}
default: {
throw new Error('Invalid arguments');
}
}
}
Module['TermCriteria'] = TermCriteria;
Module['matFromArray'] = function(rows, cols, type, array) {
var mat = new cv.Mat(rows, cols, type);
switch (type) {
case cv.CV_8U:
case cv.CV_8UC1:
case cv.CV_8UC2:
case cv.CV_8UC3:
case cv.CV_8UC4: {
mat.data.set(array);
break;
}
case cv.CV_8S:
case cv.CV_8SC1:
case cv.CV_8SC2:
case cv.CV_8SC3:
case cv.CV_8SC4: {
mat.data8S.set(array);
break;
}
case cv.CV_16U:
case cv.CV_16UC1:
case cv.CV_16UC2:
case cv.CV_16UC3:
case cv.CV_16UC4: {
mat.data16U.set(array);
break;
}
case cv.CV_16S:
case cv.CV_16SC1:
case cv.CV_16SC2:
case cv.CV_16SC3:
case cv.CV_16SC4: {
mat.data16S.set(array);
break;
}
case cv.CV_32S:
case cv.CV_32SC1:
case cv.CV_32SC2:
case cv.CV_32SC3:
case cv.CV_32SC4: {
mat.data32S.set(array);
break;
}
case cv.CV_32F:
case cv.CV_32FC1:
case cv.CV_32FC2:
case cv.CV_32FC3:
case cv.CV_32FC4: {
mat.data32F.set(array);
break;
}
case cv.CV_64F:
case cv.CV_64FC1:
case cv.CV_64FC2:
case cv.CV_64FC3:
case cv.CV_64FC4: {
mat.data64F.set(array);
break;
}
default: {
throw new Error('Type is unsupported');
}
}
return mat;
};
Module['matFromImageData'] = function(imageData) {
var mat = new cv.Mat(imageData.height, imageData.width, cv.CV_8UC4);
mat.data.set(imageData.data);
return mat;
};