Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/js/test/test_mat.js
16339 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
42
// //////////////////////////////////////////////////////////////////////////////////////
43
// Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
44
//
45
// LICENSE AGREEMENT
46
// Copyright (c) 2015 The Regents of the University of California (Regents)
47
//
48
// Redistribution and use in source and binary forms, with or without
49
// modification, are permitted provided that the following conditions are met:
50
// 1. Redistributions of source code must retain the above copyright
51
// notice, this list of conditions and the following disclaimer.
52
// 2. Redistributions in binary form must reproduce the above copyright
53
// notice, this list of conditions and the following disclaimer in the
54
// documentation and/or other materials provided with the distribution.
55
// 3. Neither the name of the University nor the
56
// names of its contributors may be used to endorse or promote products
57
// derived from this software without specific prior written permission.
58
//
59
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
60
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
61
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
62
// DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
63
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
65
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
66
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
68
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69
//
70
71
if (typeof module !== 'undefined' && module.exports) {
72
// The envrionment is Node.js
73
var cv = require('./opencv.js'); // eslint-disable-line no-var
74
}
75
76
QUnit.module('Core', {});
77
78
QUnit.test('test_mat_creation', function(assert) {
79
// Mat constructors.
80
// Mat::Mat(int rows, int cols, int type)
81
{
82
let mat = new cv.Mat(10, 20, cv.CV_8UC3);
83
84
assert.equal(mat.type(), cv.CV_8UC3);
85
assert.equal(mat.depth(), cv.CV_8U);
86
assert.equal(mat.channels(), 3);
87
assert.ok(mat.empty() === false);
88
89
let size = mat.size();
90
assert.equal(size.height, 10);
91
assert.equal(size.width, 20);
92
93
mat.delete();
94
}
95
96
// Mat::Mat(const Mat &)
97
{
98
// Copy from another Mat
99
let mat1 = new cv.Mat(10, 20, cv.CV_8UC3);
100
let mat2 = new cv.Mat(mat1);
101
102
assert.equal(mat2.type(), mat1.type());
103
assert.equal(mat2.depth(), mat1.depth());
104
assert.equal(mat2.channels(), mat1.channels());
105
assert.equal(mat2.empty(), mat1.empty());
106
107
let size1 = mat1.size;
108
let size2 = mat2.size();
109
assert.ok(size1[0] === size2[0]);
110
assert.ok(size1[1] === size2[1]);
111
112
mat1.delete();
113
mat2.delete();
114
}
115
116
// Mat::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
117
{
118
// 10 * 10 and one channel
119
let data = cv._malloc(10 * 10 * 1);
120
let mat = new cv.Mat(10, 10, cv.CV_8UC1, data, 0);
121
122
assert.equal(mat.type(), cv.CV_8UC1);
123
assert.equal(mat.depth(), cv.CV_8U);
124
assert.equal(mat.channels(), 1);
125
assert.ok(mat.empty() === false);
126
127
let size = mat.size();
128
assert.ok(size.height === 10);
129
assert.ok(size.width === 10);
130
131
mat.delete();
132
}
133
134
// Mat::Mat(int rows, int cols, int type, const Scalar& scalar)
135
{
136
// 2 * 2 8UC4 mat
137
let mat = new cv.Mat(2, 2, cv.CV_8UC4, [0, 1, 2, 3]);
138
139
for (let r = 0; r < mat.rows; r++) {
140
for (let c = 0; c < mat.cols; c++) {
141
let element = mat.ptr(r, c);
142
assert.equal(element[0], 0);
143
assert.equal(element[1], 1);
144
assert.equal(element[2], 2);
145
assert.equal(element[3], 3);
146
}
147
}
148
149
mat.delete();
150
}
151
152
// Mat::create(int, int, int)
153
{
154
let mat = new cv.Mat();
155
mat.create(10, 5, cv.CV_8UC3);
156
let size = mat.size();
157
158
assert.ok(mat.type() === cv.CV_8UC3);
159
assert.ok(size.height === 10);
160
assert.ok(size.width === 5);
161
assert.ok(mat.channels() === 3);
162
163
mat.delete();
164
}
165
// Mat::create(Size, int)
166
{
167
let mat = new cv.Mat();
168
mat.create({height: 10, width: 5}, cv.CV_8UC4);
169
let size = mat.size();
170
171
assert.ok(mat.type() === cv.CV_8UC4);
172
assert.ok(size.height === 10);
173
assert.ok(size.width === 5);
174
assert.ok(mat.channels() === 4);
175
176
mat.delete();
177
}
178
// clone
179
{
180
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
181
let mat2 = mat.clone();
182
183
assert.equal(mat.channels, mat2.channels);
184
assert.equal(mat.size().height, mat2.size().height);
185
assert.equal(mat.size().width, mat2.size().width);
186
187
assert.deepEqual(mat.data, mat2.data);
188
189
190
mat.delete();
191
mat2.delete();
192
}
193
// copyTo
194
{
195
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
196
let mat2 = new cv.Mat();
197
mat.copyTo(mat2);
198
199
assert.equal(mat.channels, mat2.channels);
200
assert.equal(mat.size().height, mat2.size().height);
201
assert.equal(mat.size().width, mat2.size().width);
202
203
assert.deepEqual(mat.data, mat2.data);
204
205
206
mat.delete();
207
mat2.delete();
208
}
209
// copyTo1
210
{
211
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
212
let mat2 = new cv.Mat();
213
let mask = new cv.Mat(5, 5, cv.CV_8UC1, new cv.Scalar(1));
214
mat.copyTo(mat2, mask);
215
216
assert.equal(mat.channels, mat2.channels);
217
assert.equal(mat.size().height, mat2.size().height);
218
assert.equal(mat.size().width, mat2.size().width);
219
220
assert.deepEqual(mat.data, mat2.data);
221
222
223
mat.delete();
224
mat2.delete();
225
mask.delete();
226
}
227
228
// matFromArray
229
{
230
let arrayC1 = [0, -1, 2, -3];
231
let arrayC2 = [0, -1, 2, -3, 4, -5, 6, -7];
232
let arrayC3 = [0, -1, 2, -3, 4, -5, 6, -7, 9, -9, 10, -11];
233
let arrayC4 = [0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, 13, 14, 15];
234
235
let mat8UC1 = cv.matFromArray(2, 2, cv.CV_8UC1, arrayC1);
236
let mat8UC2 = cv.matFromArray(2, 2, cv.CV_8UC2, arrayC2);
237
let mat8UC3 = cv.matFromArray(2, 2, cv.CV_8UC3, arrayC3);
238
let mat8UC4 = cv.matFromArray(2, 2, cv.CV_8UC4, arrayC4);
239
240
let mat8SC1 = cv.matFromArray(2, 2, cv.CV_8SC1, arrayC1);
241
let mat8SC2 = cv.matFromArray(2, 2, cv.CV_8SC2, arrayC2);
242
let mat8SC3 = cv.matFromArray(2, 2, cv.CV_8SC3, arrayC3);
243
let mat8SC4 = cv.matFromArray(2, 2, cv.CV_8SC4, arrayC4);
244
245
let mat16UC1 = cv.matFromArray(2, 2, cv.CV_16UC1, arrayC1);
246
let mat16UC2 = cv.matFromArray(2, 2, cv.CV_16UC2, arrayC2);
247
let mat16UC3 = cv.matFromArray(2, 2, cv.CV_16UC3, arrayC3);
248
let mat16UC4 = cv.matFromArray(2, 2, cv.CV_16UC4, arrayC4);
249
250
let mat16SC1 = cv.matFromArray(2, 2, cv.CV_16SC1, arrayC1);
251
let mat16SC2 = cv.matFromArray(2, 2, cv.CV_16SC2, arrayC2);
252
let mat16SC3 = cv.matFromArray(2, 2, cv.CV_16SC3, arrayC3);
253
let mat16SC4 = cv.matFromArray(2, 2, cv.CV_16SC4, arrayC4);
254
255
let mat32SC1 = cv.matFromArray(2, 2, cv.CV_32SC1, arrayC1);
256
let mat32SC2 = cv.matFromArray(2, 2, cv.CV_32SC2, arrayC2);
257
let mat32SC3 = cv.matFromArray(2, 2, cv.CV_32SC3, arrayC3);
258
let mat32SC4 = cv.matFromArray(2, 2, cv.CV_32SC4, arrayC4);
259
260
let mat32FC1 = cv.matFromArray(2, 2, cv.CV_32FC1, arrayC1);
261
let mat32FC2 = cv.matFromArray(2, 2, cv.CV_32FC2, arrayC2);
262
let mat32FC3 = cv.matFromArray(2, 2, cv.CV_32FC3, arrayC3);
263
let mat32FC4 = cv.matFromArray(2, 2, cv.CV_32FC4, arrayC4);
264
265
let mat64FC1 = cv.matFromArray(2, 2, cv.CV_64FC1, arrayC1);
266
let mat64FC2 = cv.matFromArray(2, 2, cv.CV_64FC2, arrayC2);
267
let mat64FC3 = cv.matFromArray(2, 2, cv.CV_64FC3, arrayC3);
268
let mat64FC4 = cv.matFromArray(2, 2, cv.CV_64FC4, arrayC4);
269
270
assert.deepEqual(mat8UC1.data, new Uint8Array(arrayC1));
271
assert.deepEqual(mat8UC2.data, new Uint8Array(arrayC2));
272
assert.deepEqual(mat8UC3.data, new Uint8Array(arrayC3));
273
assert.deepEqual(mat8UC4.data, new Uint8Array(arrayC4));
274
275
assert.deepEqual(mat8SC1.data8S, new Int8Array(arrayC1));
276
assert.deepEqual(mat8SC2.data8S, new Int8Array(arrayC2));
277
assert.deepEqual(mat8SC3.data8S, new Int8Array(arrayC3));
278
assert.deepEqual(mat8SC4.data8S, new Int8Array(arrayC4));
279
280
assert.deepEqual(mat16UC1.data16U, new Uint16Array(arrayC1));
281
assert.deepEqual(mat16UC2.data16U, new Uint16Array(arrayC2));
282
assert.deepEqual(mat16UC3.data16U, new Uint16Array(arrayC3));
283
assert.deepEqual(mat16UC4.data16U, new Uint16Array(arrayC4));
284
285
assert.deepEqual(mat16SC1.data16S, new Int16Array(arrayC1));
286
assert.deepEqual(mat16SC2.data16S, new Int16Array(arrayC2));
287
assert.deepEqual(mat16SC3.data16S, new Int16Array(arrayC3));
288
assert.deepEqual(mat16SC4.data16S, new Int16Array(arrayC4));
289
290
assert.deepEqual(mat32SC1.data32S, new Int32Array(arrayC1));
291
assert.deepEqual(mat32SC2.data32S, new Int32Array(arrayC2));
292
assert.deepEqual(mat32SC3.data32S, new Int32Array(arrayC3));
293
assert.deepEqual(mat32SC4.data32S, new Int32Array(arrayC4));
294
295
assert.deepEqual(mat32FC1.data32F, new Float32Array(arrayC1));
296
assert.deepEqual(mat32FC2.data32F, new Float32Array(arrayC2));
297
assert.deepEqual(mat32FC3.data32F, new Float32Array(arrayC3));
298
assert.deepEqual(mat32FC4.data32F, new Float32Array(arrayC4));
299
300
assert.deepEqual(mat64FC1.data64F, new Float64Array(arrayC1));
301
assert.deepEqual(mat64FC2.data64F, new Float64Array(arrayC2));
302
assert.deepEqual(mat64FC3.data64F, new Float64Array(arrayC3));
303
assert.deepEqual(mat64FC4.data64F, new Float64Array(arrayC4));
304
305
mat8UC1.delete();
306
mat8UC2.delete();
307
mat8UC3.delete();
308
mat8UC4.delete();
309
mat8SC1.delete();
310
mat8SC2.delete();
311
mat8SC3.delete();
312
mat8SC4.delete();
313
mat16UC1.delete();
314
mat16UC2.delete();
315
mat16UC3.delete();
316
mat16UC4.delete();
317
mat16SC1.delete();
318
mat16SC2.delete();
319
mat16SC3.delete();
320
mat16SC4.delete();
321
mat32SC1.delete();
322
mat32SC2.delete();
323
mat32SC3.delete();
324
mat32SC4.delete();
325
mat32FC1.delete();
326
mat32FC2.delete();
327
mat32FC3.delete();
328
mat32FC4.delete();
329
mat64FC1.delete();
330
mat64FC2.delete();
331
mat64FC3.delete();
332
mat64FC4.delete();
333
}
334
335
// matFromImageData
336
{
337
// Only test in browser
338
if (typeof window === 'undefined') {
339
return;
340
}
341
let canvas = window.document.createElement('canvas');
342
canvas.width = 2;
343
canvas.height = 2;
344
let ctx = canvas.getContext('2d');
345
ctx.fillStyle='#FF0000';
346
ctx.fillRect(0, 0, 1, 1);
347
ctx.fillRect(1, 1, 1, 1);
348
349
let imageData = ctx.getImageData(0, 0, 2, 2);
350
let mat = cv.matFromImageData(imageData);
351
352
assert.deepEqual(mat.data, new Uint8Array(imageData.data));
353
354
mat.delete();
355
}
356
357
// Mat(mat)
358
{
359
let mat = new cv.Mat(2, 2, cv.CV_8UC4, new cv.Scalar(1, 0, 1, 0));
360
let mat1 = new cv.Mat(mat);
361
let mat2 = mat;
362
363
assert.equal(mat.rows, mat1.rows);
364
assert.equal(mat.cols, mat1.cols);
365
assert.equal(mat.type(), mat1.type());
366
assert.deepEqual(mat.data, mat1.data);
367
368
mat.delete();
369
370
assert.equal(mat1.isDeleted(), false);
371
assert.equal(mat2.isDeleted(), true);
372
373
mat1.delete();
374
}
375
376
// mat.setTo
377
{
378
let mat = new cv.Mat(2, 2, cv.CV_8UC4);
379
let s = [0, 1, 2, 3];
380
381
mat.setTo(s);
382
383
assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s));
384
assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
385
assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s));
386
assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
387
388
let s1 = [0, 0, 0, 0];
389
mat.setTo(s1);
390
let mask = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 0, 1]);
391
mat.setTo(s, mask);
392
393
assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s1));
394
assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
395
assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s1));
396
assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));
397
398
mat.delete();
399
mask.delete();
400
}
401
});
402
403
QUnit.test('test_mat_ptr', function(assert) {
404
const RValue = 3;
405
const GValue = 7;
406
const BValue = 197;
407
408
// cv.CV_8UC1 + Mat::ptr(int).
409
{
410
let mat = new cv.Mat(10, 10, cv.CV_8UC1);
411
let view = mat.data;
412
413
// Alter matrix[2, 1].
414
let step = 10;
415
view[2 * step + 1] = RValue;
416
417
// Access matrix[2, 1].
418
view = mat.ptr(2);
419
420
assert.equal(view[1], RValue);
421
422
mat.delete();
423
}
424
425
// cv.CV_8UC3 + Mat::ptr(int).
426
{
427
let mat = new cv.Mat(10, 10, cv.CV_8UC3);
428
let view = mat.data;
429
430
// Alter matrix[2, 1].
431
let step = 3 * 10;
432
view[2 * step + 3] = RValue;
433
view[2 * step + 3 + 1] = GValue;
434
view[2 * step + 3 + 2] = BValue;
435
436
// Access matrix[2, 1].
437
view = mat.ptr(2);
438
439
assert.equal(view[3], RValue);
440
assert.equal(view[3 + 1], GValue);
441
assert.equal(view[3 + 2], BValue);
442
443
mat.delete();
444
}
445
446
// cv.CV_8UC3 + Mat::ptr(int, int).
447
{
448
let mat = new cv.Mat(10, 10, cv.CV_8UC3);
449
let view = mat.data;
450
451
// Alter matrix[2, 1].
452
let step = 3 * 10;
453
view[2 * step + 3] = RValue;
454
view[2 * step + 3 + 1] = GValue;
455
view[2 * step + 3 + 2] = BValue;
456
457
// Access matrix[2, 1].
458
view = mat.ptr(2, 1);
459
460
assert.equal(view[0], RValue);
461
assert.equal(view[1], GValue);
462
assert.equal(view[2], BValue);
463
464
mat.delete();
465
}
466
467
const RValueF32 = 3.3;
468
const GValueF32 = 7.3;
469
const BValueF32 = 197.3;
470
const EPSILON = 0.001;
471
472
// cv.CV_32FC1 + Mat::ptr(int).
473
{
474
let mat = new cv.Mat(10, 10, cv.CV_32FC1);
475
let view = mat.data32F;
476
477
// Alter matrix[2, 1].
478
let step = 10;
479
view[2 * step + 1] = RValueF32;
480
481
// Access matrix[2, 1].
482
view = mat.floatPtr(2);
483
484
assert.ok(Math.abs(view[1] - RValueF32) < EPSILON);
485
486
mat.delete();
487
}
488
489
// cv.CV_32FC3 + Mat::ptr(int).
490
{
491
let mat = new cv.Mat(10, 10, cv.CV_32FC3);
492
let view = mat.data32F;
493
494
// Alter matrix[2, 1].
495
let step = mat.step1(0);
496
view[2 * step + 3] = RValueF32;
497
view[2 * step + 3 + 1] = GValueF32;
498
view[2 * step + 3 + 2] = BValueF32;
499
500
// Access matrix[2, 1].
501
view = mat.floatPtr(2);
502
503
assert.ok(Math.abs(view[3] - RValueF32) < EPSILON);
504
assert.ok(Math.abs(view[3 + 1] - GValueF32) < EPSILON);
505
assert.ok(Math.abs(view[3 + 2] - BValueF32) < EPSILON);
506
507
mat.delete();
508
}
509
510
// cv.CV_32FC3 + Mat::ptr(int, int).
511
{
512
let mat = new cv.Mat(10, 10, cv.CV_32FC3);
513
let view = mat.data32F;
514
515
// Alter matrix[2, 1].
516
let step = mat.step1(0);
517
view[2 * step + 3] = RValueF32;
518
view[2 * step + 3 + 1] = GValueF32;
519
view[2 * step + 3 + 2] = BValueF32;
520
521
// Access matrix[2, 1].
522
view = mat.floatPtr(2, 1);
523
524
assert.ok(Math.abs(view[0] - RValueF32) < EPSILON);
525
assert.ok(Math.abs(view[1] - GValueF32) < EPSILON);
526
assert.ok(Math.abs(view[2] - BValueF32) < EPSILON);
527
528
mat.delete();
529
}
530
});
531
532
QUnit.test('test_mat_zeros', function(assert) {
533
let zeros = new Uint8Array(10*10).fill(0);
534
// Mat::zeros(int, int, int)
535
{
536
let mat = cv.Mat.zeros(10, 10, cv.CV_8UC1);
537
let view = mat.data;
538
539
assert.deepEqual(view, zeros);
540
541
mat.delete();
542
}
543
544
// Mat::zeros(Size, int)
545
{
546
let mat = cv.Mat.zeros({height: 10, width: 10}, cv.CV_8UC1);
547
let view = mat.data;
548
549
assert.deepEqual(view, zeros);
550
551
mat.delete();
552
}
553
});
554
555
QUnit.test('test_mat_ones', function(assert) {
556
let ones = new Uint8Array(10*10).fill(1);
557
// Mat::ones(int, int, int)
558
{
559
let mat = cv.Mat.ones(10, 10, cv.CV_8UC1);
560
let view = mat.data;
561
562
assert.deepEqual(view, ones);
563
}
564
// Mat::ones(Size, int)
565
{
566
let mat = cv.Mat.ones({height: 10, width: 10}, cv.CV_8UC1);
567
let view = mat.data;
568
569
assert.deepEqual(view, ones);
570
}
571
});
572
573
QUnit.test('test_mat_eye', function(assert) {
574
let eye4by4 = new Uint8Array([1, 0, 0, 0,
575
0, 1, 0, 0,
576
0, 0, 1, 0,
577
0, 0, 0, 1]);
578
// Mat::eye(int, int, int)
579
{
580
let mat = cv.Mat.eye(4, 4, cv.CV_8UC1);
581
let view = mat.data;
582
583
assert.deepEqual(view, eye4by4);
584
}
585
586
// Mat::eye(Size, int)
587
{
588
let mat = cv.Mat.eye({height: 4, width: 4}, cv.CV_8UC1);
589
let view = mat.data;
590
591
assert.deepEqual(view, eye4by4);
592
}
593
});
594
595
QUnit.test('test_mat_miscs', function(assert) {
596
// Mat::col(int)
597
{
598
let mat = cv.matFromArray(2, 2, cv.CV_8UC2, [1, 2, 3, 4, 5, 6, 7, 8]);
599
let col = mat.col(1);
600
601
assert.equal(col.isContinuous(), false);
602
assert.equal(col.ptr(0, 0)[0], 3);
603
assert.equal(col.ptr(0, 0)[1], 4);
604
assert.equal(col.ptr(1, 0)[0], 7);
605
assert.equal(col.ptr(1, 0)[1], 8);
606
607
col.delete();
608
mat.delete();
609
}
610
611
// Mat::row(int)
612
{
613
let mat = cv.Mat.zeros(5, 5, cv.CV_8UC2);
614
let row = mat.row(1);
615
let view = row.data;
616
assert.equal(view[0], 0);
617
assert.equal(view[4], 0);
618
619
row.delete();
620
mat.delete();
621
}
622
623
// Mat::convertTo(Mat, int, double, double)
624
{
625
let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
626
let grayMat = cv.Mat.zeros(5, 5, cv.CV_8UC1);
627
628
mat.convertTo(grayMat, cv.CV_8U, 2, 1);
629
// dest = 2 * source(x, y) + 1.
630
let view = grayMat.data;
631
assert.equal(view[0], (1 * 2) + 1);
632
633
mat.convertTo(grayMat, cv.CV_8U);
634
// dest = 1 * source(x, y) + 0.
635
assert.equal(view[0], 1);
636
637
mat.convertTo(grayMat, cv.CV_8U, 2);
638
// dest = 2 * source(x, y) + 0.
639
assert.equal(view[0], 2);
640
641
grayMat.delete();
642
mat.delete();
643
}
644
645
// split
646
{
647
const R =7;
648
const G =13;
649
const B =29;
650
651
let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
652
let view = mat.data;
653
view[0] = R;
654
view[1] = G;
655
view[2] = B;
656
657
let bgrPlanes = new cv.MatVector();
658
cv.split(mat, bgrPlanes);
659
assert.equal(bgrPlanes.size(), 3);
660
661
let rMat = bgrPlanes.get(0);
662
view = rMat.data;
663
assert.equal(view[0], R);
664
665
let gMat = bgrPlanes.get(1);
666
view = gMat.data;
667
assert.equal(view[0], G);
668
669
let bMat = bgrPlanes.get(2);
670
view = bMat.data;
671
assert.equal(view[0], B);
672
673
mat.delete();
674
rMat.delete();
675
gMat.delete();
676
bgrPlanes.delete();
677
bMat.delete();
678
}
679
680
// elemSize
681
{
682
let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
683
assert.equal(mat.elemSize(), 3);
684
assert.equal(mat.elemSize1(), 1);
685
686
let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
687
assert.equal(mat2.elemSize(), 1);
688
assert.equal(mat2.elemSize1(), 1);
689
690
let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
691
assert.equal(mat3.elemSize(), 2 * 3);
692
assert.equal(mat3.elemSize1(), 2);
693
694
mat.delete();
695
mat2.delete();
696
mat3.delete();
697
}
698
699
// step
700
{
701
let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
702
assert.equal(mat.step[0], 15);
703
assert.equal(mat.step[1], 3);
704
705
let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
706
assert.equal(mat2.step[0], 5);
707
assert.equal(mat2.step[1], 1);
708
709
let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
710
assert.equal(mat3.step[0], 30);
711
assert.equal(mat3.step[1], 6);
712
713
mat.delete();
714
mat2.delete();
715
mat3.delete();
716
}
717
718
// dot
719
{
720
let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
721
let mat2 = cv.Mat.eye(5, 5, cv.CV_8UC1);
722
723
assert.equal(mat.dot(mat), 25);
724
assert.equal(mat.dot(mat2), 5);
725
assert.equal(mat2.dot(mat2), 5);
726
727
mat.delete();
728
mat2.delete();
729
}
730
731
// mul
732
{
733
const FACTOR = 5;
734
let mat = cv.Mat.ones(4, 4, cv.CV_8UC1);
735
let mat2 = cv.Mat.eye(4, 4, cv.CV_8UC1);
736
737
let expected = new Uint8Array([FACTOR, 0, 0, 0,
738
0, FACTOR, 0, 0,
739
0, 0, FACTOR, 0,
740
0, 0, 0, FACTOR]);
741
let mat3 = mat.mul(mat2, FACTOR);
742
743
assert.deepEqual(mat3.data, expected);
744
745
mat.delete();
746
mat2.delete();
747
mat3.delete();
748
}
749
});
750
751
752
QUnit.test('test mat access', function(assert) {
753
// test memory view
754
{
755
let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
756
let dataPtr = cv._malloc(8);
757
758
let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
759
dataHeap.set(new Uint8Array(data.buffer));
760
761
let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
762
763
764
let unsignedCharView = new Uint8Array(data.buffer);
765
let charView = new Int8Array(data.buffer);
766
let shortView = new Int16Array(data.buffer);
767
let unsignedShortView = new Uint16Array(data.buffer);
768
let intView = new Int32Array(data.buffer);
769
let float32View = new Float32Array(data.buffer);
770
let float64View = new Float64Array(data.buffer);
771
772
773
assert.deepEqual(unsignedCharView, mat.data);
774
assert.deepEqual(charView, mat.data8S);
775
assert.deepEqual(shortView, mat.data16S);
776
assert.deepEqual(unsignedShortView, mat.data16U);
777
assert.deepEqual(intView, mat.data32S);
778
assert.deepEqual(float32View, mat.data32F);
779
assert.deepEqual(float64View, mat.data64F);
780
}
781
782
// test ucharAt(i)
783
{
784
let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
785
let dataPtr = cv._malloc(8);
786
787
let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
788
dataHeap.set(new Uint8Array(data.buffer));
789
790
let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);
791
792
assert.equal(mat.ucharAt(0), 0);
793
assert.equal(mat.ucharAt(1), 0);
794
assert.equal(mat.ucharAt(2), 0);
795
assert.equal(mat.ucharAt(3), 255);
796
assert.equal(mat.ucharAt(4), 0);
797
assert.equal(mat.ucharAt(5), 1);
798
assert.equal(mat.ucharAt(6), 2);
799
assert.equal(mat.ucharAt(7), 3);
800
}
801
802
// test ushortAt(i)
803
{
804
let data = new Uint16Array([0, 1000, 65000, 255, 0, 1, 2, 3]);
805
let dataPtr = cv._malloc(16);
806
807
let dataHeap = new Uint16Array(cv.HEAPU8.buffer, dataPtr, 8);
808
dataHeap.set(new Uint16Array(data.buffer));
809
810
let mat = new cv.Mat(8, 1, cv.CV_16SC1, dataPtr, 0);
811
812
assert.equal(mat.ushortAt(0), 0);
813
assert.equal(mat.ushortAt(1), 1000);
814
assert.equal(mat.ushortAt(2), 65000);
815
assert.equal(mat.ushortAt(3), 255);
816
assert.equal(mat.ushortAt(4), 0);
817
assert.equal(mat.ushortAt(5), 1);
818
assert.equal(mat.ushortAt(6), 2);
819
assert.equal(mat.ushortAt(7), 3);
820
}
821
822
// test intAt(i)
823
{
824
let data = new Int32Array([0, -1000, 65000, 255, -2000000, -1, 2, 3]);
825
let dataPtr = cv._malloc(32);
826
827
let dataHeap = new Int32Array(cv.HEAPU32.buffer, dataPtr, 8);
828
dataHeap.set(new Int32Array(data.buffer));
829
830
let mat = new cv.Mat(8, 1, cv.CV_32SC1, dataPtr, 0);
831
832
assert.equal(mat.intAt(0), 0);
833
assert.equal(mat.intAt(1), -1000);
834
assert.equal(mat.intAt(2), 65000);
835
assert.equal(mat.intAt(3), 255);
836
assert.equal(mat.intAt(4), -2000000);
837
assert.equal(mat.intAt(5), -1);
838
assert.equal(mat.intAt(6), 2);
839
assert.equal(mat.intAt(7), 3);
840
}
841
842
// test floatAt(i)
843
{
844
const EPSILON = 0.001;
845
let data = new Float32Array([0, -10.5, 650.001, 255, -20.1, -1.2, 2, 3.5]);
846
let dataPtr = cv._malloc(32);
847
848
let dataHeap = new Float32Array(cv.HEAPU32.buffer, dataPtr, 8);
849
dataHeap.set(new Float32Array(data.buffer));
850
851
let mat = new cv.Mat(8, 1, cv.CV_32FC1, dataPtr, 0);
852
853
assert.equal(Math.abs(mat.floatAt(0)-0) < EPSILON, true);
854
assert.equal(Math.abs(mat.floatAt(1)+10.5) < EPSILON, true);
855
assert.equal(Math.abs(mat.floatAt(2)-650.001) < EPSILON, true);
856
assert.equal(Math.abs(mat.floatAt(3)-255) < EPSILON, true);
857
assert.equal(Math.abs(mat.floatAt(4)+20.1) < EPSILON, true);
858
assert.equal(Math.abs(mat.floatAt(5)+1.2) < EPSILON, true);
859
assert.equal(Math.abs(mat.floatAt(6)-2) < EPSILON, true);
860
assert.equal(Math.abs(mat.floatAt(7)-3.5) < EPSILON, true);
861
}
862
863
// test intAt(i,j)
864
{
865
let mat = cv.Mat.eye({height: 3, width: 3}, cv.CV_32SC1);
866
867
assert.equal(mat.intAt(0, 0), 1);
868
assert.equal(mat.intAt(0, 1), 0);
869
assert.equal(mat.intAt(0, 2), 0);
870
assert.equal(mat.intAt(1, 0), 0);
871
assert.equal(mat.intAt(1, 1), 1);
872
assert.equal(mat.intAt(1, 2), 0);
873
assert.equal(mat.intAt(2, 0), 0);
874
assert.equal(mat.intAt(2, 1), 0);
875
assert.equal(mat.intAt(2, 2), 1);
876
877
mat.delete();
878
}
879
});
880
881
QUnit.test('test_mat_operations', function(assert) {
882
// test minMaxLoc
883
{
884
let src = cv.Mat.ones(4, 4, cv.CV_8UC1);
885
886
src.data[2] = 0;
887
src.data[5] = 2;
888
889
let result = cv.minMaxLoc(src);
890
891
assert.equal(result.minVal, 0);
892
assert.equal(result.maxVal, 2);
893
assert.deepEqual(result.minLoc, {x: 2, y: 0});
894
assert.deepEqual(result.maxLoc, {x: 1, y: 1});
895
896
src.delete();
897
}
898
});
899
900
QUnit.test('test_mat_roi', function(assert) {
901
// test minMaxLoc
902
{
903
let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
904
let roi = mat.roi(new cv.Rect(1, 1, 1, 1));
905
906
assert.equal(roi.rows, 1);
907
assert.equal(roi.cols, 1);
908
assert.deepEqual(roi.data, new Uint8Array([mat.ucharAt(1, 1)]));
909
910
mat.delete();
911
roi.delete();
912
}
913
});
914
915
916
QUnit.test('test_mat_range', function(assert) {
917
{
918
let src = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
919
let mat = src.colRange(0, 1);
920
921
assert.equal(mat.isContinuous(), false);
922
assert.equal(mat.rows, 2);
923
assert.equal(mat.cols, 1);
924
assert.equal(mat.ucharAt(0), 0);
925
assert.equal(mat.ucharAt(1), 2);
926
927
mat.delete();
928
929
mat = src.colRange({start: 0, end: 1});
930
931
assert.equal(mat.isContinuous(), false);
932
assert.equal(mat.rows, 2);
933
assert.equal(mat.cols, 1);
934
assert.equal(mat.ucharAt(0), 0);
935
assert.equal(mat.ucharAt(1), 2);
936
937
mat.delete();
938
939
mat = src.rowRange(1, 2);
940
941
assert.equal(mat.rows, 1);
942
assert.equal(mat.cols, 2);
943
assert.deepEqual(mat.data, new Uint8Array([2, 3]));
944
945
mat.delete();
946
947
mat = src.rowRange({start: 1, end: 2});
948
949
assert.equal(mat.rows, 1);
950
assert.equal(mat.cols, 2);
951
assert.deepEqual(mat.data, new Uint8Array([2, 3]));
952
953
mat.delete();
954
955
src.delete();
956
}
957
});
958
959
QUnit.test('test_mat_diag', function(assert) {
960
// test diag
961
{
962
let mat = cv.matFromArray(3, 3, cv.CV_8UC1, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
963
let d = mat.diag();
964
let d1 = mat.diag(1);
965
let d2 = mat.diag(-1);
966
967
assert.equal(mat.isContinuous(), true);
968
assert.equal(d.isContinuous(), false);
969
assert.equal(d1.isContinuous(), false);
970
assert.equal(d2.isContinuous(), false);
971
972
assert.equal(d.ucharAt(0), 0);
973
assert.equal(d.ucharAt(1), 4);
974
assert.equal(d.ucharAt(2), 8);
975
976
assert.equal(d1.ucharAt(0), 1);
977
assert.equal(d1.ucharAt(1), 5);
978
979
assert.equal(d2.ucharAt(0), 3);
980
assert.equal(d2.ucharAt(1), 7);
981
982
mat.delete();
983
d.delete();
984
d1.delete();
985
d2.delete();
986
}
987
});
988
989