Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/js/src/helpers.js
16337 views
1
// //////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
41
Module['imread'] = function(imageSource) {
42
var img = null;
43
if (typeof imageSource === 'string') {
44
img = document.getElementById(imageSource);
45
} else {
46
img = imageSource;
47
}
48
var canvas = null;
49
var ctx = null;
50
if (img instanceof HTMLImageElement) {
51
canvas = document.createElement('canvas');
52
canvas.width = img.width;
53
canvas.height = img.height;
54
ctx = canvas.getContext('2d');
55
ctx.drawImage(img, 0, 0, img.width, img.height);
56
} else if (img instanceof HTMLCanvasElement) {
57
canvas = img;
58
ctx = canvas.getContext('2d');
59
} else {
60
throw new Error('Please input the valid canvas or img id.');
61
return;
62
}
63
64
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
65
return cv.matFromImageData(imgData);
66
};
67
68
Module['imshow'] = function(canvasSource, mat) {
69
var canvas = null;
70
if (typeof canvasSource === 'string') {
71
canvas = document.getElementById(canvasSource);
72
} else {
73
canvas = canvasSource;
74
}
75
if (!(canvas instanceof HTMLCanvasElement)) {
76
throw new Error('Please input the valid canvas element or id.');
77
return;
78
}
79
if (!(mat instanceof cv.Mat)) {
80
throw new Error('Please input the valid cv.Mat instance.');
81
return;
82
}
83
84
// convert the mat type to cv.CV_8U
85
var img = new cv.Mat();
86
var depth = mat.type()%8;
87
var scale = depth <= cv.CV_8S? 1.0 : (depth <= cv.CV_32S? 1.0/256.0 : 255.0);
88
var shift = (depth === cv.CV_8S || depth === cv.CV_16S)? 128.0 : 0.0;
89
mat.convertTo(img, cv.CV_8U, scale, shift);
90
91
// convert the img type to cv.CV_8UC4
92
switch (img.type()) {
93
case cv.CV_8UC1:
94
cv.cvtColor(img, img, cv.COLOR_GRAY2RGBA);
95
break;
96
case cv.CV_8UC3:
97
cv.cvtColor(img, img, cv.COLOR_RGB2RGBA);
98
break;
99
case cv.CV_8UC4:
100
break;
101
default:
102
throw new Error('Bad number of channels (Source image must have 1, 3 or 4 channels)');
103
return;
104
}
105
var imgData = new ImageData(new Uint8ClampedArray(img.data), img.cols, img.rows);
106
var ctx = canvas.getContext('2d');
107
ctx.clearRect(0, 0, canvas.width, canvas.height);
108
canvas.width = imgData.width;
109
canvas.height = imgData.height;
110
ctx.putImageData(imgData, 0, 0);
111
img.delete();
112
};
113
114
Module['VideoCapture'] = function(videoSource) {
115
var video = null;
116
if (typeof videoSource === 'string') {
117
video = document.getElementById(videoSource);
118
} else {
119
video = videoSource;
120
}
121
if (!(video instanceof HTMLVideoElement)) {
122
throw new Error('Please input the valid video element or id.');
123
return;
124
}
125
var canvas = document.createElement('canvas');
126
canvas.width = video.width;
127
canvas.height = video.height;
128
var ctx = canvas.getContext('2d');
129
this.video = video;
130
this.read = function(frame) {
131
if (!(frame instanceof cv.Mat)) {
132
throw new Error('Please input the valid cv.Mat instance.');
133
return;
134
}
135
if (frame.type() !== cv.CV_8UC4) {
136
throw new Error('Bad type of input mat: the type should be cv.CV_8UC4.');
137
return;
138
}
139
if (frame.cols !== video.width || frame.rows !== video.height) {
140
throw new Error('Bad size of input mat: the size should be same as the video.');
141
return;
142
}
143
ctx.drawImage(video, 0, 0, video.width, video.height);
144
frame.data.set(ctx.getImageData(0, 0, video.width, video.height).data);
145
};
146
};
147
148
function Range(start, end) {
149
this.start = typeof(start) === 'undefined' ? 0 : start;
150
this.end = typeof(end) === 'undefined' ? 0 : end;
151
}
152
153
Module['Range'] = Range;
154
155
function Point(x, y) {
156
this.x = typeof(x) === 'undefined' ? 0 : x;
157
this.y = typeof(y) === 'undefined' ? 0 : y;
158
}
159
160
Module['Point'] = Point;
161
162
function Size(width, height) {
163
this.width = typeof(width) === 'undefined' ? 0 : width;
164
this.height = typeof(height) === 'undefined' ? 0 : height;
165
}
166
167
Module['Size'] = Size;
168
169
function Rect() {
170
switch (arguments.length) {
171
case 0: {
172
// new cv.Rect()
173
this.x = 0;
174
this.y = 0;
175
this.width = 0;
176
this.height = 0;
177
break;
178
}
179
case 1: {
180
// new cv.Rect(rect)
181
var rect = arguments[0];
182
this.x = rect.x;
183
this.y = rect.y;
184
this.width = rect.width;
185
this.height = rect.height;
186
break;
187
}
188
case 2: {
189
// new cv.Rect(point, size)
190
var point = arguments[0];
191
var size = arguments[1];
192
this.x = point.x;
193
this.y = point.y;
194
this.width = size.width;
195
this.height = size.height;
196
break;
197
}
198
case 4: {
199
// new cv.Rect(x, y, width, height)
200
this.x = arguments[0];
201
this.y = arguments[1];
202
this.width = arguments[2];
203
this.height = arguments[3];
204
break;
205
}
206
default: {
207
throw new Error('Invalid arguments');
208
}
209
}
210
}
211
212
Module['Rect'] = Rect;
213
214
function RotatedRect() {
215
switch (arguments.length) {
216
case 0: {
217
this.center = {x: 0, y: 0};
218
this.size = {width: 0, height: 0};
219
this.angle = 0;
220
break;
221
}
222
case 3: {
223
this.center = arguments[0];
224
this.size = arguments[1];
225
this.angle = arguments[2];
226
break;
227
}
228
default: {
229
throw new Error('Invalid arguments');
230
}
231
}
232
}
233
234
RotatedRect.points = function(obj) {
235
return Module.rotatedRectPoints(obj);
236
};
237
238
RotatedRect.boundingRect = function(obj) {
239
return Module.rotatedRectBoundingRect(obj);
240
};
241
242
RotatedRect.boundingRect2f = function(obj) {
243
return Module.rotatedRectBoundingRect2f(obj);
244
};
245
246
Module['RotatedRect'] = RotatedRect;
247
248
function Scalar(v0, v1, v2, v3) {
249
this.push(typeof(v0) === 'undefined' ? 0 : v0);
250
this.push(typeof(v1) === 'undefined' ? 0 : v1);
251
this.push(typeof(v2) === 'undefined' ? 0 : v2);
252
this.push(typeof(v3) === 'undefined' ? 0 : v3);
253
}
254
255
Scalar.prototype = new Array; // eslint-disable-line no-array-constructor
256
257
Scalar.all = function(v) {
258
return new Scalar(v, v, v, v);
259
};
260
261
Module['Scalar'] = Scalar;
262
263
function MinMaxLoc() {
264
switch (arguments.length) {
265
case 0: {
266
this.minVal = 0;
267
this.maxVal = 0;
268
this.minLoc = new Point();
269
this.maxLoc = new Point();
270
break;
271
}
272
case 4: {
273
this.minVal = arguments[0];
274
this.maxVal = arguments[1];
275
this.minLoc = arguments[2];
276
this.maxLoc = arguments[3];
277
break;
278
}
279
default: {
280
throw new Error('Invalid arguments');
281
}
282
}
283
}
284
285
Module['MinMaxLoc'] = MinMaxLoc;
286
287
function Circle() {
288
switch (arguments.length) {
289
case 0: {
290
this.center = new Point();
291
this.radius = 0;
292
break;
293
}
294
case 2: {
295
this.center = arguments[0];
296
this.radius = arguments[1];
297
break;
298
}
299
default: {
300
throw new Error('Invalid arguments');
301
}
302
}
303
}
304
305
Module['Circle'] = Circle;
306
307
function TermCriteria() {
308
switch (arguments.length) {
309
case 0: {
310
this.type = 0;
311
this.maxCount = 0;
312
this.epsilon = 0;
313
break;
314
}
315
case 3: {
316
this.type = arguments[0];
317
this.maxCount = arguments[1];
318
this.epsilon = arguments[2];
319
break;
320
}
321
default: {
322
throw new Error('Invalid arguments');
323
}
324
}
325
}
326
327
Module['TermCriteria'] = TermCriteria;
328
329
Module['matFromArray'] = function(rows, cols, type, array) {
330
var mat = new cv.Mat(rows, cols, type);
331
switch (type) {
332
case cv.CV_8U:
333
case cv.CV_8UC1:
334
case cv.CV_8UC2:
335
case cv.CV_8UC3:
336
case cv.CV_8UC4: {
337
mat.data.set(array);
338
break;
339
}
340
case cv.CV_8S:
341
case cv.CV_8SC1:
342
case cv.CV_8SC2:
343
case cv.CV_8SC3:
344
case cv.CV_8SC4: {
345
mat.data8S.set(array);
346
break;
347
}
348
case cv.CV_16U:
349
case cv.CV_16UC1:
350
case cv.CV_16UC2:
351
case cv.CV_16UC3:
352
case cv.CV_16UC4: {
353
mat.data16U.set(array);
354
break;
355
}
356
case cv.CV_16S:
357
case cv.CV_16SC1:
358
case cv.CV_16SC2:
359
case cv.CV_16SC3:
360
case cv.CV_16SC4: {
361
mat.data16S.set(array);
362
break;
363
}
364
case cv.CV_32S:
365
case cv.CV_32SC1:
366
case cv.CV_32SC2:
367
case cv.CV_32SC3:
368
case cv.CV_32SC4: {
369
mat.data32S.set(array);
370
break;
371
}
372
case cv.CV_32F:
373
case cv.CV_32FC1:
374
case cv.CV_32FC2:
375
case cv.CV_32FC3:
376
case cv.CV_32FC4: {
377
mat.data32F.set(array);
378
break;
379
}
380
case cv.CV_64F:
381
case cv.CV_64FC1:
382
case cv.CV_64FC2:
383
case cv.CV_64FC3:
384
case cv.CV_64FC4: {
385
mat.data64F.set(array);
386
break;
387
}
388
default: {
389
throw new Error('Type is unsupported');
390
}
391
}
392
return mat;
393
};
394
395
Module['matFromImageData'] = function(imageData) {
396
var mat = new cv.Mat(imageData.height, imageData.width, cv.CV_8UC4);
397
mat.data.set(imageData.data);
398
return mat;
399
};
400
401