Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/js/test/test_utils.js
16344 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
// Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
43
//
44
// LICENSE AGREEMENT
45
// Copyright (c) 2015 The Regents of the University of California (Regents)
46
//
47
// Redistribution and use in source and binary forms, with or without
48
// modification, are permitted provided that the following conditions are met:
49
// 1. Redistributions of source code must retain the above copyright
50
// notice, this list of conditions and the following disclaimer.
51
// 2. Redistributions in binary form must reproduce the above copyright
52
// notice, this list of conditions and the following disclaimer in the
53
// documentation and/or other materials provided with the distribution.
54
// 3. Neither the name of the University nor the
55
// names of its contributors may be used to endorse or promote products
56
// derived from this software without specific prior written permission.
57
//
58
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
59
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61
// DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
62
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
64
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
65
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68
//
69
70
if (typeof module !== 'undefined' && module.exports) {
71
// The envrionment is Node.js
72
var cv = require('./opencv.js'); // eslint-disable-line no-var
73
}
74
QUnit.module('Utils', {});
75
QUnit.test('Test vectors', function(assert) {
76
{
77
let pointVector = new cv.PointVector();
78
for (let i=0; i<100; ++i) {
79
pointVector.push_back({x: i, y: 2*i});
80
}
81
82
assert.equal(pointVector.size(), 100);
83
84
let index = 10;
85
let item = pointVector.get(index);
86
assert.equal(item.x, index);
87
assert.equal(item.y, 2*index);
88
89
index = 0;
90
item = pointVector.get(index);
91
assert.equal(item.x, index);
92
assert.equal(item.y, 2*index);
93
94
index = 99;
95
item = pointVector.get(index);
96
assert.equal(item.x, index);
97
assert.equal(item.y, 2*index);
98
99
pointVector.delete();
100
}
101
102
{
103
let pointVector = new cv.PointVector();
104
for (let i=0; i<100; ++i) {
105
pointVector.push_back(new cv.Point(i, 2*i));
106
}
107
108
pointVector.push_back(new cv.Point());
109
110
assert.equal(pointVector.size(), 101);
111
112
let index = 10;
113
let item = pointVector.get(index);
114
assert.equal(item.x, index);
115
assert.equal(item.y, 2*index);
116
117
index = 0;
118
item = pointVector.get(index);
119
assert.equal(item.x, index);
120
assert.equal(item.y, 2*index);
121
122
index = 99;
123
item = pointVector.get(index);
124
assert.equal(item.x, index);
125
assert.equal(item.y, 2*index);
126
127
index = 100;
128
item = pointVector.get(index);
129
assert.equal(item.x, 0);
130
assert.equal(item.y, 0);
131
132
pointVector.delete();
133
}
134
});
135
QUnit.test('Test Rect', function(assert) {
136
let rectVector = new cv.RectVector();
137
let rect = {x: 1, y: 2, width: 3, height: 4};
138
rectVector.push_back(rect);
139
rectVector.push_back(new cv.Rect());
140
rectVector.push_back(new cv.Rect(rect));
141
rectVector.push_back(new cv.Rect({x: 5, y: 6}, {width: 7, height: 8}));
142
rectVector.push_back(new cv.Rect(9, 10, 11, 12));
143
144
assert.equal(rectVector.size(), 5);
145
146
let item = rectVector.get(0);
147
assert.equal(item.x, 1);
148
assert.equal(item.y, 2);
149
assert.equal(item.width, 3);
150
assert.equal(item.height, 4);
151
152
item = rectVector.get(1);
153
assert.equal(item.x, 0);
154
assert.equal(item.y, 0);
155
assert.equal(item.width, 0);
156
assert.equal(item.height, 0);
157
158
item = rectVector.get(2);
159
assert.equal(item.x, 1);
160
assert.equal(item.y, 2);
161
assert.equal(item.width, 3);
162
assert.equal(item.height, 4);
163
164
item = rectVector.get(3);
165
assert.equal(item.x, 5);
166
assert.equal(item.y, 6);
167
assert.equal(item.width, 7);
168
assert.equal(item.height, 8);
169
170
item = rectVector.get(4);
171
assert.equal(item.x, 9);
172
assert.equal(item.y, 10);
173
assert.equal(item.width, 11);
174
assert.equal(item.height, 12);
175
176
rectVector.delete();
177
});
178
QUnit.test('Test Size', function(assert) {
179
{
180
let mat = new cv.Mat();
181
mat.create({width: 5, height: 10}, cv.CV_8UC4);
182
let size = mat.size();
183
184
assert.ok(mat.type() === cv.CV_8UC4);
185
assert.ok(size.height === 10);
186
assert.ok(size.width === 5);
187
assert.ok(mat.channels() === 4);
188
189
mat.delete();
190
}
191
192
{
193
let mat = new cv.Mat();
194
mat.create(new cv.Size(5, 10), cv.CV_8UC4);
195
let size = mat.size();
196
197
assert.ok(mat.type() === cv.CV_8UC4);
198
assert.ok(size.height === 10);
199
assert.ok(size.width === 5);
200
assert.ok(mat.channels() === 4);
201
202
mat.delete();
203
}
204
});
205
206
207
QUnit.test('test_rotated_rect', function(assert) {
208
{
209
let rect = {center: {x: 100, y: 100}, size: {height: 100, width: 50}, angle: 30};
210
211
assert.equal(rect.center.x, 100);
212
assert.equal(rect.center.y, 100);
213
assert.equal(rect.angle, 30);
214
assert.equal(rect.size.height, 100);
215
assert.equal(rect.size.width, 50);
216
}
217
218
{
219
let rect = new cv.RotatedRect();
220
221
assert.equal(rect.center.x, 0);
222
assert.equal(rect.center.y, 0);
223
assert.equal(rect.angle, 0);
224
assert.equal(rect.size.height, 0);
225
assert.equal(rect.size.width, 0);
226
227
let points = cv.RotatedRect.points(rect);
228
229
assert.equal(points[0].x, 0);
230
assert.equal(points[0].y, 0);
231
assert.equal(points[1].x, 0);
232
assert.equal(points[1].y, 0);
233
assert.equal(points[2].x, 0);
234
assert.equal(points[2].y, 0);
235
assert.equal(points[3].x, 0);
236
assert.equal(points[3].y, 0);
237
}
238
239
{
240
let rect = new cv.RotatedRect({x: 100, y: 100}, {height: 100, width: 50}, 30);
241
242
assert.equal(rect.center.x, 100);
243
assert.equal(rect.center.y, 100);
244
assert.equal(rect.angle, 30);
245
assert.equal(rect.size.height, 100);
246
assert.equal(rect.size.width, 50);
247
248
let points = cv.RotatedRect.points(rect);
249
250
assert.equal(points[0].x, cv.RotatedRect.boundingRect2f(rect).x);
251
assert.equal(points[1].y, cv.RotatedRect.boundingRect2f(rect).y);
252
}
253
});
254
255