Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/test/test_operations.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
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) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
#include "opencv2/ts/ocl_test.hpp" // T-API like tests
45
46
namespace opencv_test {
47
namespace {
48
49
class CV_OperationsTest : public cvtest::BaseTest
50
{
51
public:
52
CV_OperationsTest();
53
~CV_OperationsTest();
54
protected:
55
void run(int);
56
57
struct test_excep
58
{
59
test_excep(const string& _s=string("")) : s(_s) { }
60
string s;
61
};
62
63
bool SomeMatFunctions();
64
bool TestMat();
65
template<typename _Tp> void TestType(Size sz, _Tp value);
66
bool TestTemplateMat();
67
bool TestMatND();
68
bool TestSparseMat();
69
bool TestVec();
70
bool TestMatxMultiplication();
71
bool TestMatxElementwiseDivison();
72
bool TestSubMatAccess();
73
bool TestExp();
74
bool TestSVD();
75
bool operations1();
76
77
void checkDiff(const Mat& m1, const Mat& m2, const string& s)
78
{
79
if (cvtest::norm(m1, m2, NORM_INF) != 0) throw test_excep(s);
80
}
81
void checkDiffF(const Mat& m1, const Mat& m2, const string& s)
82
{
83
if (cvtest::norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s);
84
}
85
};
86
87
CV_OperationsTest::CV_OperationsTest()
88
{
89
}
90
91
CV_OperationsTest::~CV_OperationsTest() {}
92
93
#define STR(a) STR2(a)
94
#define STR2(a) #a
95
96
#define CHECK_DIFF(a, b) checkDiff(a, b, "(" #a ") != (" #b ") at l." STR(__LINE__))
97
#define CHECK_DIFF_FLT(a, b) checkDiffF(a, b, "(" #a ") !=(eps) (" #b ") at l." STR(__LINE__))
98
99
#if defined _MSC_VER && _MSC_VER < 1400
100
#define MSVC_OLD 1
101
#else
102
#define MSVC_OLD 0
103
#endif
104
105
template<typename _Tp> void CV_OperationsTest::TestType(Size sz, _Tp value)
106
{
107
cv::Mat_<_Tp> m(sz);
108
CV_Assert(m.cols == sz.width && m.rows == sz.height && m.depth() == cv::traits::Depth<_Tp>::value &&
109
m.channels() == DataType<_Tp>::channels &&
110
m.elemSize() == sizeof(_Tp) && m.step == m.elemSize()*m.cols);
111
for( int y = 0; y < sz.height; y++ )
112
for( int x = 0; x < sz.width; x++ )
113
{
114
m(y,x) = value;
115
}
116
117
double s = sum(Mat(m).reshape(1))[0];
118
CV_Assert( s == (double)sz.width*sz.height );
119
}
120
121
bool CV_OperationsTest::TestMat()
122
{
123
try
124
{
125
Mat one_3x1(3, 1, CV_32F, Scalar(1.0));
126
Mat shi_3x1(3, 1, CV_32F, Scalar(1.2));
127
Mat shi_2x1(2, 1, CV_32F, Scalar(-1));
128
Scalar shift = Scalar::all(15);
129
130
float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };
131
Mat rot_2x3(2, 3, CV_32F, data);
132
133
Mat res = one_3x1 + shi_3x1 + shi_3x1 + shi_3x1;
134
res = Mat(Mat(2 * rot_2x3) * res - shi_2x1) + shift;
135
136
Mat tmp, res2;
137
cv::add(one_3x1, shi_3x1, tmp);
138
cv::add(tmp, shi_3x1, tmp);
139
cv::add(tmp, shi_3x1, tmp);
140
cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);
141
cv::add(res2, Mat(2, 1, CV_32F, shift), res2);
142
143
CHECK_DIFF(res, res2);
144
145
Mat mat4x4(4, 4, CV_32F);
146
cv::randu(mat4x4, Scalar(0), Scalar(10));
147
148
Mat roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));
149
Mat roi2 = mat4x4(Range(1, 3), Range(1, 3));
150
151
CHECK_DIFF(roi1, roi2);
152
CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));
153
154
Mat intMat10(3, 3, CV_32S, Scalar(10));
155
Mat intMat11(3, 3, CV_32S, Scalar(11));
156
Mat resMat(3, 3, CV_8U, Scalar(255));
157
158
CHECK_DIFF(resMat, intMat10 == intMat10);
159
CHECK_DIFF(resMat, intMat10 < intMat11);
160
CHECK_DIFF(resMat, intMat11 > intMat10);
161
CHECK_DIFF(resMat, intMat10 <= intMat11);
162
CHECK_DIFF(resMat, intMat11 >= intMat10);
163
CHECK_DIFF(resMat, intMat11 != intMat10);
164
165
CHECK_DIFF(resMat, intMat10 == 10.0);
166
CHECK_DIFF(resMat, 10.0 == intMat10);
167
CHECK_DIFF(resMat, intMat10 < 11.0);
168
CHECK_DIFF(resMat, 11.0 > intMat10);
169
CHECK_DIFF(resMat, 10.0 < intMat11);
170
CHECK_DIFF(resMat, 11.0 >= intMat10);
171
CHECK_DIFF(resMat, 10.0 <= intMat11);
172
CHECK_DIFF(resMat, 10.0 != intMat11);
173
CHECK_DIFF(resMat, intMat11 != 10.0);
174
175
Mat eye = Mat::eye(3, 3, CV_16S);
176
Mat maskMat4(3, 3, CV_16S, Scalar(4));
177
Mat maskMat1(3, 3, CV_16S, Scalar(1));
178
Mat maskMat5(3, 3, CV_16S, Scalar(5));
179
Mat maskMat0(3, 3, CV_16S, Scalar(0));
180
181
CHECK_DIFF(maskMat0, maskMat4 & maskMat1);
182
CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);
183
CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));
184
185
Mat m;
186
m = maskMat4.clone(); m &= maskMat1; CHECK_DIFF(maskMat0, m);
187
m = maskMat4.clone(); m &= maskMat1 | maskMat1; CHECK_DIFF(maskMat0, m);
188
m = maskMat4.clone(); m &= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat0, m);
189
190
m = maskMat4.clone(); m &= Scalar(1); CHECK_DIFF(maskMat0, m);
191
m = maskMat4.clone(); m |= maskMat1; CHECK_DIFF(maskMat5, m);
192
m = maskMat5.clone(); m ^= maskMat1; CHECK_DIFF(maskMat4, m);
193
m = maskMat4.clone(); m |= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat5, m);
194
m = maskMat5.clone(); m ^= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat4, m);
195
196
m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);
197
m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);
198
199
200
201
CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));
202
CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);
203
CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));
204
CHECK_DIFF(maskMat0, (maskMat1 | maskMat1) & Scalar(4));
205
CHECK_DIFF(maskMat0, Scalar(4) & (maskMat1 | maskMat1));
206
207
CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));
208
CHECK_DIFF(maskMat0, (maskMat4 | maskMat1) ^ maskMat5);
209
CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ (maskMat4 + maskMat1));
210
CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));
211
CHECK_DIFF(maskMat1, Scalar(5) ^ maskMat4);
212
CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 + maskMat1));
213
CHECK_DIFF(maskMat5, Scalar(5) | (maskMat4 + maskMat1));
214
CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ Scalar(5));
215
216
CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));
217
CHECK_DIFF(maskMat5, (maskMat4 ^ maskMat1) | maskMat5);
218
CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));
219
CHECK_DIFF(maskMat5, (maskMat4 | maskMat4) | Scalar(1));
220
CHECK_DIFF(maskMat5, Scalar(1) | (maskMat4 | maskMat4));
221
CHECK_DIFF(maskMat5, Scalar(1) | maskMat4);
222
CHECK_DIFF(maskMat5, (maskMat5 | maskMat5) | (maskMat4 ^ maskMat1));
223
224
CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));
225
CHECK_DIFF(maskMat1, min(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));
226
CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));
227
CHECK_DIFF(maskMat5, max(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));
228
229
CHECK_DIFF(maskMat1, min(maskMat1, maskMat5 | maskMat5));
230
CHECK_DIFF(maskMat1, min(maskMat1 | maskMat1, maskMat5));
231
CHECK_DIFF(maskMat5, max(maskMat1 | maskMat1, maskMat5));
232
CHECK_DIFF(maskMat5, max(maskMat1, maskMat5 | maskMat5));
233
234
CHECK_DIFF(~maskMat1, maskMat1 ^ -1);
235
CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ -1);
236
237
CHECK_DIFF(maskMat1, maskMat4/4.0);
238
239
/////////////////////////////
240
241
CHECK_DIFF(1.0 - (maskMat5 | maskMat5), -maskMat4);
242
CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 1.0, maskMat5);
243
CHECK_DIFF(1.0 + (maskMat4 | maskMat4) * 1.0, maskMat5);
244
CHECK_DIFF((maskMat5 | maskMat5) * 1.0 - 1.0, maskMat4);
245
CHECK_DIFF(5.0 - (maskMat4 | maskMat4) * 1.0, maskMat1);
246
CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 0.5 + 0.5, maskMat5);
247
CHECK_DIFF(0.5 + ((maskMat4 | maskMat4) * 1.0 + 0.5), maskMat5);
248
CHECK_DIFF(((maskMat4 | maskMat4) * 1.0 + 2.0) - 1.0, maskMat5);
249
CHECK_DIFF(5.0 - ((maskMat1 | maskMat1) * 1.0 + 3.0), maskMat1);
250
CHECK_DIFF( ( (maskMat1 | maskMat1) * 2.0 + 2.0) * 1.25, maskMat5);
251
CHECK_DIFF( 1.25 * ( (maskMat1 | maskMat1) * 2.0 + 2.0), maskMat5);
252
CHECK_DIFF( -( (maskMat1 | maskMat1) * (-2.0) + 1.0), maskMat1);
253
CHECK_DIFF( maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0, maskMat5);
254
CHECK_DIFF( 1.0 + (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);
255
CHECK_DIFF( (maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0) - 1.0, maskMat4);
256
CHECK_DIFF(5.0 - (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat1);
257
CHECK_DIFF((maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0)*1.25, maskMat5);
258
CHECK_DIFF(1.25 * (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);
259
CHECK_DIFF(-(maskMat1 * 2.0 + maskMat4 * (-1) + 1.0), maskMat1);
260
CHECK_DIFF((maskMat1 * 1.0 + maskMat4), maskMat5);
261
CHECK_DIFF((maskMat4 + maskMat1 * 1.0), maskMat5);
262
CHECK_DIFF((maskMat1 * 3.0 + 1.0) + maskMat1, maskMat5);
263
CHECK_DIFF(maskMat1 + (maskMat1 * 3.0 + 1.0), maskMat5);
264
CHECK_DIFF(maskMat1*4.0 + (maskMat1 | maskMat1), maskMat5);
265
CHECK_DIFF((maskMat1 | maskMat1) + maskMat1*4.0, maskMat5);
266
CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1 | maskMat1), maskMat5);
267
CHECK_DIFF((maskMat1 | maskMat1) + (maskMat1*3.0 + 1.0), maskMat5);
268
CHECK_DIFF(maskMat1*4.0 + maskMat4*2.0, maskMat1 * 12);
269
CHECK_DIFF((maskMat1*3.0 + 1.0) + maskMat4*2.0, maskMat1 * 12);
270
CHECK_DIFF(maskMat4*2.0 + (maskMat1*3.0 + 1.0), maskMat1 * 12);
271
CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1*2.0 + 2.0), maskMat1 * 8);
272
273
CHECK_DIFF(maskMat5*1.0 - maskMat4, maskMat1);
274
CHECK_DIFF(maskMat5 - maskMat1 * 4.0, maskMat1);
275
CHECK_DIFF((maskMat4 * 1.0 + 4.0)- maskMat4, maskMat4);
276
CHECK_DIFF(maskMat5 - (maskMat1 * 2.0 + 2.0), maskMat1);
277
CHECK_DIFF(maskMat5*1.0 - (maskMat4 | maskMat4), maskMat1);
278
CHECK_DIFF((maskMat5 | maskMat5) - maskMat1 * 4.0, maskMat1);
279
CHECK_DIFF((maskMat4 * 1.0 + 4.0)- (maskMat4 | maskMat4), maskMat4);
280
CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 * 2.0 + 2.0), maskMat1);
281
CHECK_DIFF(maskMat1*5.0 - maskMat4 * 1.0, maskMat1);
282
CHECK_DIFF((maskMat1*5.0 + 3.0)- maskMat4 * 1.0, maskMat4);
283
CHECK_DIFF(maskMat4 * 2.0 - (maskMat1*4.0 + 3.0), maskMat1);
284
CHECK_DIFF((maskMat1 * 2.0 + 3.0) - (maskMat1*3.0 + 1.0), maskMat1);
285
286
CHECK_DIFF((maskMat5 - maskMat4)* 4.0, maskMat4);
287
CHECK_DIFF(4.0 * (maskMat5 - maskMat4), maskMat4);
288
289
CHECK_DIFF(-((maskMat4 | maskMat4) - (maskMat5 | maskMat5)), maskMat1);
290
291
CHECK_DIFF(4.0 * (maskMat1 | maskMat1), maskMat4);
292
CHECK_DIFF((maskMat4 | maskMat4)/4.0, maskMat1);
293
294
#if !MSVC_OLD
295
CHECK_DIFF(2.0 * (maskMat1 * 2.0) , maskMat4);
296
#endif
297
CHECK_DIFF((maskMat4 / 2.0) / 2.0 , maskMat1);
298
CHECK_DIFF(-(maskMat4 - maskMat5) , maskMat1);
299
CHECK_DIFF(-((maskMat4 - maskMat5) * 1.0), maskMat1);
300
301
302
/////////////////////////////
303
CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
304
305
///// Element-wise multiplication
306
307
CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);
308
CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);
309
CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
310
CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
311
CHECK_DIFF(maskMat4.mul(maskMat4) * 0.25, maskMat4);
312
CHECK_DIFF(0.25 * maskMat4.mul(maskMat4), maskMat4);
313
314
////// Element-wise division
315
316
CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
317
CHECK_DIFF((maskMat4 & maskMat4) / (maskMat1 * 4), maskMat1);
318
319
CHECK_DIFF((maskMat4 & maskMat4) / maskMat4, maskMat1);
320
CHECK_DIFF(maskMat4 / (maskMat4 & maskMat4), maskMat1);
321
CHECK_DIFF((maskMat1 * 4) / maskMat4, maskMat1);
322
323
CHECK_DIFF(maskMat4 / (maskMat1 * 4), maskMat1);
324
CHECK_DIFF((maskMat4 * 0.5 )/ (maskMat1 * 2), maskMat1);
325
326
CHECK_DIFF(maskMat4 / maskMat4.mul(maskMat1), maskMat1);
327
CHECK_DIFF((maskMat4 & maskMat4) / maskMat4.mul(maskMat1), maskMat1);
328
329
CHECK_DIFF(4.0 / maskMat4, maskMat1);
330
CHECK_DIFF(4.0 / (maskMat4 | maskMat4), maskMat1);
331
CHECK_DIFF(4.0 / (maskMat1 * 4.0), maskMat1);
332
CHECK_DIFF(4.0 / (maskMat4 / maskMat1), maskMat1);
333
334
m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);
335
m = maskMat4.clone(); m/=maskMat4; CHECK_DIFF(m, maskMat1);
336
m = maskMat4.clone(); m/=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);
337
m = maskMat4.clone(); m/=(maskMat4 / maskMat1); CHECK_DIFF(m, maskMat1);
338
339
/////////////////////////////
340
float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};
341
Mat mt(3, 3, CV_32F, matrix_data);
342
Mat mi = mt.inv();
343
Mat d1 = Mat::eye(3, 3, CV_32F);
344
Mat d2 = d1 * 2;
345
MatExpr mt_tr = mt.t();
346
MatExpr mi_tr = mi.t();
347
Mat mi2 = mi * 2;
348
349
350
CHECK_DIFF_FLT( mi2 * mt, d2 );
351
CHECK_DIFF_FLT( mi * mt, d1 );
352
CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );
353
354
m = mi.clone(); m*=mt; CHECK_DIFF_FLT(m, d1);
355
m = mi.clone(); m*= (2 * mt - mt) ; CHECK_DIFF_FLT(m, d1);
356
357
m = maskMat4.clone(); m+=(maskMat1 * 1.0); CHECK_DIFF(m, maskMat5);
358
m = maskMat5.clone(); m-=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);
359
360
m = maskMat1.clone(); m+=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat5);
361
m = maskMat5.clone(); m-=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat1);
362
#if !MSVC_OLD
363
m = mi.clone(); m+=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi + d1 * 4);
364
m = mi.clone(); m-=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi - d1 * 4);
365
m = mi.clone(); m*=(mt * 1.0); CHECK_DIFF_FLT(m, d1);
366
m = mi.clone(); m*=(mt * 1.0 + Mat::eye(m.size(), m.type())); CHECK_DIFF_FLT(m, d1 + mi);
367
m = mi.clone(); m*=mt_tr.t(); CHECK_DIFF_FLT(m, d1);
368
369
CHECK_DIFF_FLT( (mi * 2) * mt, d2);
370
CHECK_DIFF_FLT( mi * (2 * mt), d2);
371
CHECK_DIFF_FLT( mt.t() * mi_tr, d1 );
372
CHECK_DIFF_FLT( mt_tr * mi.t(), d1 );
373
CHECK_DIFF_FLT( (mi * 0.4) * (mt * 5), d2);
374
375
CHECK_DIFF_FLT( mt.t() * (mi_tr * 2), d2 );
376
CHECK_DIFF_FLT( (mt_tr * 2) * mi.t(), d2 );
377
378
CHECK_DIFF_FLT(mt.t() * mi.t(), d1);
379
CHECK_DIFF_FLT( (mi * mt) * 2.0, d2);
380
CHECK_DIFF_FLT( 2.0 * (mi * mt), d2);
381
CHECK_DIFF_FLT( -(mi * mt), -d1);
382
383
CHECK_DIFF_FLT( (mi * mt) / 2.0, d1 / 2);
384
385
Mat mt_mul_2_plus_1;
386
gemm(mt, d1, 2, Mat::ones(3, 3, CV_32F), 1, mt_mul_2_plus_1);
387
388
CHECK_DIFF( (mt * 2.0 + 1.0) * mi, mt_mul_2_plus_1 * mi); // (A*alpha + beta)*B
389
CHECK_DIFF( mi * (mt * 2.0 + 1.0), mi * mt_mul_2_plus_1); // A*(B*alpha + beta)
390
CHECK_DIFF( (mt * 2.0 + 1.0) * (mi * 2), mt_mul_2_plus_1 * mi2); // (A*alpha + beta)*(B*gamma)
391
CHECK_DIFF( (mi *2)* (mt * 2.0 + 1.0), mi2 * mt_mul_2_plus_1); // (A*gamma)*(B*alpha + beta)
392
CHECK_DIFF_FLT( (mt * 2.0 + 1.0) * mi.t(), mt_mul_2_plus_1 * mi_tr); // (A*alpha + beta)*B^t
393
CHECK_DIFF_FLT( mi.t() * (mt * 2.0 + 1.0), mi_tr * mt_mul_2_plus_1); // A^t*(B*alpha + beta)
394
395
CHECK_DIFF_FLT( (mi * mt + d2)*5, d1 * 3 * 5);
396
CHECK_DIFF_FLT( mi * mt + d2, d1 * 3);
397
CHECK_DIFF_FLT( -(mi * mt) + d2, d1);
398
CHECK_DIFF_FLT( (mi * mt) + d1, d2);
399
CHECK_DIFF_FLT( d1 + (mi * mt), d2);
400
CHECK_DIFF_FLT( (mi * mt) - d2, -d1);
401
CHECK_DIFF_FLT( d2 - (mi * mt), d1);
402
403
CHECK_DIFF_FLT( (mi * mt) + d2 * 0.5, d2);
404
CHECK_DIFF_FLT( d2 * 0.5 + (mi * mt), d2);
405
CHECK_DIFF_FLT( (mi * mt) - d1 * 2, -d1);
406
CHECK_DIFF_FLT( d1 * 2 - (mi * mt), d1);
407
408
CHECK_DIFF_FLT( (mi * mt) + mi.t(), mi_tr + d1);
409
CHECK_DIFF_FLT( mi.t() + (mi * mt), mi_tr + d1);
410
CHECK_DIFF_FLT( (mi * mt) - mi.t(), d1 - mi_tr);
411
CHECK_DIFF_FLT( mi.t() - (mi * mt), mi_tr - d1);
412
413
CHECK_DIFF_FLT( 2.0 *(mi * mt + d2), d1 * 6);
414
CHECK_DIFF_FLT( -(mi * mt + d2), d1 * -3);
415
416
CHECK_DIFF_FLT(mt.inv() * mt, d1);
417
418
CHECK_DIFF_FLT(mt.inv() * (2*mt - mt), d1);
419
#endif
420
}
421
catch (const test_excep& e)
422
{
423
ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
424
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
425
return false;
426
}
427
return true;
428
}
429
430
bool CV_OperationsTest::SomeMatFunctions()
431
{
432
try
433
{
434
Mat rgba( 10, 10, CV_8UC4, Scalar(1,2,3,4) );
435
Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
436
Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );
437
Mat out[] = { bgr, alpha };
438
// rgba[0] -> bgr[2], rgba[1] -> bgr[1],
439
// rgba[2] -> bgr[0], rgba[3] -> alpha[0]
440
int from_to[] = { 0,2, 1,1, 2,0, 3,3 };
441
mixChannels( &rgba, 1, out, 2, from_to, 4 );
442
443
Mat bgr_exp( rgba.size(), CV_8UC3, Scalar(3,2,1));
444
Mat alpha_exp( rgba.size(), CV_8UC1, Scalar(4));
445
446
CHECK_DIFF(bgr_exp, bgr);
447
CHECK_DIFF(alpha_exp, alpha);
448
}
449
catch (const test_excep& e)
450
{
451
ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
452
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
453
return false;
454
}
455
return true;
456
457
}
458
459
460
bool CV_OperationsTest::TestSubMatAccess()
461
{
462
try
463
{
464
Mat_<float> T_bs(4,4);
465
Vec3f cdir(1.f, 1.f, 0.f);
466
Vec3f ydir(1.f, 0.f, 1.f);
467
Vec3f fpt(0.1f, 0.7f, 0.2f);
468
T_bs.setTo(0);
469
T_bs(Range(0,3),Range(2,3)) = 1.0*Mat(cdir); // weird OpenCV stuff, need to do multiply
470
T_bs(Range(0,3),Range(1,2)) = 1.0*Mat(ydir);
471
T_bs(Range(0,3),Range(0,1)) = 1.0*Mat(cdir.cross(ydir));
472
T_bs(Range(0,3),Range(3,4)) = 1.0*Mat(fpt);
473
T_bs(3,3) = 1.0;
474
//std::cout << "[Nav Grok] S frame =" << std::endl << T_bs << std::endl;
475
476
// set up display coords, really just the S frame
477
std::vector<float>coords;
478
479
for (int i=0; i<16; i++)
480
{
481
coords.push_back(T_bs(i));
482
//std::cout << T_bs1(i) << std::endl;
483
}
484
CV_Assert( cvtest::norm(coords, T_bs.reshape(1,1), NORM_INF) == 0 );
485
}
486
catch (const test_excep& e)
487
{
488
ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
489
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
490
return false;
491
}
492
return true;
493
}
494
495
bool CV_OperationsTest::TestTemplateMat()
496
{
497
try
498
{
499
Mat_<float> one_3x1(3, 1, 1.0f);
500
Mat_<float> shi_3x1(3, 1, 1.2f);
501
Mat_<float> shi_2x1(2, 1, -2);
502
Scalar shift = Scalar::all(15);
503
504
float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };
505
Mat_<float> rot_2x3(2, 3, data);
506
507
Mat_<float> res = Mat(Mat(2 * rot_2x3) * Mat(one_3x1 + shi_3x1 + shi_3x1 + shi_3x1) - shi_2x1) + shift;
508
Mat_<float> resS = rot_2x3 * one_3x1;
509
510
Mat_<float> tmp, res2, resS2;
511
cv::add(one_3x1, shi_3x1, tmp);
512
cv::add(tmp, shi_3x1, tmp);
513
cv::add(tmp, shi_3x1, tmp);
514
cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);
515
cv::add(res2, Mat(2, 1, CV_32F, shift), res2);
516
517
cv::gemm(rot_2x3, one_3x1, 1, shi_2x1, 0, resS2, 0);
518
CHECK_DIFF(res, res2);
519
CHECK_DIFF(resS, resS2);
520
521
522
Mat_<float> mat4x4(4, 4);
523
cv::randu(mat4x4, Scalar(0), Scalar(10));
524
525
Mat_<float> roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));
526
Mat_<float> roi2 = mat4x4(Range(1, 3), Range(1, 3));
527
528
CHECK_DIFF(roi1, roi2);
529
CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));
530
531
Mat_<int> intMat10(3, 3, 10);
532
Mat_<int> intMat11(3, 3, 11);
533
Mat_<uchar> resMat(3, 3, 255);
534
535
CHECK_DIFF(resMat, intMat10 == intMat10);
536
CHECK_DIFF(resMat, intMat10 < intMat11);
537
CHECK_DIFF(resMat, intMat11 > intMat10);
538
CHECK_DIFF(resMat, intMat10 <= intMat11);
539
CHECK_DIFF(resMat, intMat11 >= intMat10);
540
541
CHECK_DIFF(resMat, intMat10 == 10.0);
542
CHECK_DIFF(resMat, intMat10 < 11.0);
543
CHECK_DIFF(resMat, intMat11 > 10.0);
544
CHECK_DIFF(resMat, intMat10 <= 11.0);
545
CHECK_DIFF(resMat, intMat11 >= 10.0);
546
547
Mat_<uchar> maskMat4(3, 3, 4);
548
Mat_<uchar> maskMat1(3, 3, 1);
549
Mat_<uchar> maskMat5(3, 3, 5);
550
Mat_<uchar> maskMat0(3, 3, (uchar)0);
551
552
CHECK_DIFF(maskMat0, maskMat4 & maskMat1);
553
CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);
554
CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));
555
556
Mat_<uchar> m;
557
m = maskMat4.clone(); m&=maskMat1; CHECK_DIFF(maskMat0, m);
558
m = maskMat4.clone(); m&=Scalar(1); CHECK_DIFF(maskMat0, m);
559
560
m = maskMat4.clone(); m|=maskMat1; CHECK_DIFF(maskMat5, m);
561
m = maskMat4.clone(); m^=maskMat1; CHECK_DIFF(maskMat5, m);
562
563
CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));
564
CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);
565
CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));
566
567
CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));
568
CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));
569
570
CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));
571
CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));
572
573
CHECK_DIFF(~maskMat1, maskMat1 ^ 0xFF);
574
CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ 0xFF);
575
576
CHECK_DIFF(maskMat1 + maskMat4, maskMat5);
577
CHECK_DIFF(maskMat1 + Scalar(4), maskMat5);
578
CHECK_DIFF(Scalar(4) + maskMat1, maskMat5);
579
CHECK_DIFF(Scalar(4) + (maskMat1 & maskMat1), maskMat5);
580
581
CHECK_DIFF(maskMat1 + 4.0, maskMat5);
582
CHECK_DIFF((maskMat1 & 0xFF) + 4.0, maskMat5);
583
CHECK_DIFF(4.0 + maskMat1, maskMat5);
584
585
m = maskMat4.clone(); m+=Scalar(1); CHECK_DIFF(m, maskMat5);
586
m = maskMat4.clone(); m+=maskMat1; CHECK_DIFF(m, maskMat5);
587
m = maskMat4.clone(); m+=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat5);
588
589
CHECK_DIFF(maskMat5 - maskMat1, maskMat4);
590
CHECK_DIFF(maskMat5 - Scalar(1), maskMat4);
591
CHECK_DIFF((maskMat5 | maskMat5) - Scalar(1), maskMat4);
592
CHECK_DIFF(maskMat5 - 1, maskMat4);
593
CHECK_DIFF((maskMat5 | maskMat5) - 1, maskMat4);
594
CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 | maskMat1), maskMat4);
595
596
CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));
597
CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));
598
599
m = maskMat5.clone(); m-=Scalar(1); CHECK_DIFF(m, maskMat4);
600
m = maskMat5.clone(); m-=maskMat1; CHECK_DIFF(m, maskMat4);
601
m = maskMat5.clone(); m-=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat4);
602
603
m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);
604
m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);
605
606
CHECK_DIFF(maskMat1, maskMat4/4.0);
607
608
Mat_<float> negf(3, 3, -3.0);
609
Mat_<float> posf = -negf;
610
Mat_<float> posf2 = posf * 2;
611
Mat_<int> negi(3, 3, -3);
612
613
CHECK_DIFF(abs(negf), -negf);
614
CHECK_DIFF(abs(posf - posf2), -negf);
615
CHECK_DIFF(abs(negi), -(negi & negi));
616
617
CHECK_DIFF(5.0 - maskMat4, maskMat1);
618
619
620
CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);
621
CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);
622
CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
623
624
625
////// Element-wise division
626
627
CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
628
CHECK_DIFF(4.0 / maskMat4, maskMat1);
629
m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);
630
631
////////////////////////////////
632
633
typedef Mat_<int> TestMat_t;
634
635
const TestMat_t cnegi = negi.clone();
636
637
TestMat_t::iterator beg = negi.begin();
638
TestMat_t::iterator end = negi.end();
639
640
TestMat_t::const_iterator cbeg = cnegi.begin();
641
TestMat_t::const_iterator cend = cnegi.end();
642
643
int sum = 0;
644
for(; beg!=end; ++beg)
645
sum+=*beg;
646
647
for(; cbeg!=cend; ++cbeg)
648
sum-=*cbeg;
649
650
if (sum != 0) throw test_excep();
651
652
CHECK_DIFF(negi.col(1), negi.col(2));
653
CHECK_DIFF(negi.row(1), negi.row(2));
654
CHECK_DIFF(negi.col(1), negi.diag());
655
656
if (Mat_<Point2f>(1, 1).elemSize1() != sizeof(float)) throw test_excep();
657
if (Mat_<Point2f>(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();
658
if (Mat_<Point2f>(1, 1).depth() != CV_32F) throw test_excep();
659
if (Mat_<float>(1, 1).depth() != CV_32F) throw test_excep();
660
if (Mat_<int>(1, 1).depth() != CV_32S) throw test_excep();
661
if (Mat_<double>(1, 1).depth() != CV_64F) throw test_excep();
662
if (Mat_<Point3d>(1, 1).depth() != CV_64F) throw test_excep();
663
if (Mat_<signed char>(1, 1).depth() != CV_8S) throw test_excep();
664
if (Mat_<unsigned short>(1, 1).depth() != CV_16U) throw test_excep();
665
if (Mat_<unsigned short>(1, 1).channels() != 1) throw test_excep();
666
if (Mat_<Point2f>(1, 1).channels() != 2) throw test_excep();
667
if (Mat_<Point3f>(1, 1).channels() != 3) throw test_excep();
668
if (Mat_<Point3d>(1, 1).channels() != 3) throw test_excep();
669
670
Mat_<uchar> eye = Mat_<uchar>::zeros(2, 2); CHECK_DIFF(Mat_<uchar>::zeros(Size(2, 2)), eye);
671
eye.at<uchar>(Point(0,0)) = 1; eye.at<uchar>(1, 1) = 1;
672
673
CHECK_DIFF(Mat_<uchar>::eye(2, 2), eye);
674
CHECK_DIFF(eye, Mat_<uchar>::eye(Size(2,2)));
675
676
Mat_<uchar> ones(2, 2, (uchar)1);
677
CHECK_DIFF(ones, Mat_<uchar>::ones(Size(2,2)));
678
CHECK_DIFF(Mat_<uchar>::ones(2, 2), ones);
679
680
Mat_<Point2f> pntMat(2, 2, Point2f(1, 0));
681
if(pntMat.stepT() != 2) throw test_excep();
682
683
uchar uchar_data[] = {1, 0, 0, 1};
684
685
Mat_<uchar> matFromData(1, 4, uchar_data);
686
const Mat_<uchar> mat2 = matFromData.clone();
687
CHECK_DIFF(matFromData, eye.reshape(1, 1));
688
if (matFromData(Point(0,0)) != uchar_data[0])throw test_excep();
689
if (mat2(Point(0,0)) != uchar_data[0]) throw test_excep();
690
691
if (matFromData(0,0) != uchar_data[0])throw test_excep();
692
if (mat2(0,0) != uchar_data[0]) throw test_excep();
693
694
Mat_<uchar> rect(eye, Rect(0, 0, 1, 1));
695
if (rect.cols != 1 || rect.rows != 1 || rect(0,0) != uchar_data[0]) throw test_excep();
696
697
//cv::Mat_<_Tp>::adjustROI(int,int,int,int)
698
//cv::Mat_<_Tp>::cross(const Mat_&) const
699
//cv::Mat_<_Tp>::Mat_(const vector<_Tp>&,bool)
700
//cv::Mat_<_Tp>::Mat_(int,int,_Tp*,size_t)
701
//cv::Mat_<_Tp>::Mat_(int,int,const _Tp&)
702
//cv::Mat_<_Tp>::Mat_(Size,const _Tp&)
703
//cv::Mat_<_Tp>::mul(const Mat_<_Tp>&,double) const
704
//cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_DivRS_<Mat> >,Mat_<_Tp> >&,double) const
705
//cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_Scale_<Mat> >,Mat_<_Tp> >&,double) const
706
//cv::Mat_<_Tp>::operator Mat_<T2>() const
707
//cv::Mat_<_Tp>::operator MatExpr_<Mat_<_Tp>,Mat_<_Tp> >() const
708
//cv::Mat_<_Tp>::operator()(const Range&,const Range&) const
709
//cv::Mat_<_Tp>::operator()(const Rect&) const
710
711
//cv::Mat_<_Tp>::operator=(const MatExpr_Base&)
712
//cv::Mat_<_Tp>::operator[](int) const
713
714
715
///////////////////////////////
716
717
float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};
718
Mat_<float> mt(3, 3, matrix_data);
719
Mat_<float> mi = mt.inv();
720
Mat_<float> d1 = Mat_<float>::eye(3, 3);
721
Mat_<float> d2 = d1 * 2;
722
Mat_<float> mt_tr = mt.t();
723
Mat_<float> mi_tr = mi.t();
724
Mat_<float> mi2 = mi * 2;
725
726
CHECK_DIFF_FLT( mi2 * mt, d2 );
727
CHECK_DIFF_FLT( mi * mt, d1 );
728
CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );
729
730
Mat_<float> mf;
731
mf = mi.clone(); mf*=mt; CHECK_DIFF_FLT(mf, d1);
732
733
////// typedefs //////
734
735
if (Mat1b(1, 1).elemSize() != sizeof(uchar)) throw test_excep();
736
if (Mat2b(1, 1).elemSize() != 2 * sizeof(uchar)) throw test_excep();
737
if (Mat3b(1, 1).elemSize() != 3 * sizeof(uchar)) throw test_excep();
738
if (Mat1f(1, 1).elemSize() != sizeof(float)) throw test_excep();
739
if (Mat2f(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();
740
if (Mat3f(1, 1).elemSize() != 3 * sizeof(float)) throw test_excep();
741
if (Mat1f(1, 1).depth() != CV_32F) throw test_excep();
742
if (Mat3f(1, 1).depth() != CV_32F) throw test_excep();
743
if (Mat3f(1, 1).type() != CV_32FC3) throw test_excep();
744
if (Mat1i(1, 1).depth() != CV_32S) throw test_excep();
745
if (Mat1d(1, 1).depth() != CV_64F) throw test_excep();
746
if (Mat1b(1, 1).depth() != CV_8U) throw test_excep();
747
if (Mat3b(1, 1).type() != CV_8UC3) throw test_excep();
748
if (Mat1w(1, 1).depth() != CV_16U) throw test_excep();
749
if (Mat1s(1, 1).depth() != CV_16S) throw test_excep();
750
if (Mat1f(1, 1).channels() != 1) throw test_excep();
751
if (Mat1b(1, 1).channels() != 1) throw test_excep();
752
if (Mat1i(1, 1).channels() != 1) throw test_excep();
753
if (Mat1w(1, 1).channels() != 1) throw test_excep();
754
if (Mat1s(1, 1).channels() != 1) throw test_excep();
755
if (Mat2f(1, 1).channels() != 2) throw test_excep();
756
if (Mat2b(1, 1).channels() != 2) throw test_excep();
757
if (Mat2i(1, 1).channels() != 2) throw test_excep();
758
if (Mat2w(1, 1).channels() != 2) throw test_excep();
759
if (Mat2s(1, 1).channels() != 2) throw test_excep();
760
if (Mat3f(1, 1).channels() != 3) throw test_excep();
761
if (Mat3b(1, 1).channels() != 3) throw test_excep();
762
if (Mat3i(1, 1).channels() != 3) throw test_excep();
763
if (Mat3w(1, 1).channels() != 3) throw test_excep();
764
if (Mat3s(1, 1).channels() != 3) throw test_excep();
765
766
vector<Mat_<float> > mvf, mvf2;
767
Mat_<Vec2f> mf2;
768
mvf.push_back(Mat_<float>::ones(4, 3));
769
mvf.push_back(Mat_<float>::zeros(4, 3));
770
merge(mvf, mf2);
771
split(mf2, mvf2);
772
CV_Assert( cvtest::norm(mvf2[0], mvf[0], CV_C) == 0 &&
773
cvtest::norm(mvf2[1], mvf[1], CV_C) == 0 );
774
775
{
776
Mat a(2,2,CV_32F,1.f);
777
Mat b(1,2,CV_32F,1.f);
778
Mat c = (a*b.t()).t();
779
CV_Assert( cvtest::norm(c, CV_L1) == 4. );
780
}
781
782
bool badarg_catched = false;
783
try
784
{
785
Mat m1 = Mat::zeros(1, 10, CV_8UC1);
786
Mat m2 = Mat::zeros(10, 10, CV_8UC3);
787
m1.copyTo(m2.row(1));
788
}
789
catch(const Exception&)
790
{
791
badarg_catched = true;
792
}
793
CV_Assert( badarg_catched );
794
795
Size size(2, 5);
796
TestType<float>(size, 1.f);
797
cv::Vec3f val1(1.f);
798
TestType<cv::Vec3f>(size, val1);
799
cv::Matx31f val2(1.f);
800
TestType<cv::Matx31f>(size, val2);
801
cv::Matx41f val3(1.f);
802
TestType<cv::Matx41f>(size, val3);
803
cv::Matx32f val4(1.f);
804
TestType<cv::Matx32f>(size, val4);
805
}
806
catch (const test_excep& e)
807
{
808
ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
809
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
810
return false;
811
}
812
return true;
813
}
814
815
bool CV_OperationsTest::TestMatND()
816
{
817
int sizes[] = { 3, 3, 3};
818
cv::MatND nd(3, sizes, CV_32F);
819
820
return true;
821
}
822
823
bool CV_OperationsTest::TestSparseMat()
824
{
825
try
826
{
827
int sizes[] = { 10, 10, 10};
828
int dims = sizeof(sizes)/sizeof(sizes[0]);
829
SparseMat mat(dims, sizes, CV_32FC2);
830
831
if (mat.dims() != dims) throw test_excep();
832
if (mat.channels() != 2) throw test_excep();
833
if (mat.depth() != CV_32F) throw test_excep();
834
835
SparseMat mat2 = mat.clone();
836
}
837
catch (const test_excep&)
838
{
839
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
840
return false;
841
}
842
return true;
843
}
844
845
846
bool CV_OperationsTest::TestMatxMultiplication()
847
{
848
try
849
{
850
Matx33f mat(1, 1, 1, 0, 1, 1, 0, 0, 1); // Identity matrix
851
Point2f pt(3, 4);
852
Point3f res = mat * pt; // Correctly assumes homogeneous coordinates
853
854
Vec3f res2 = mat*Vec3f(res.x, res.y, res.z);
855
856
if(res.x != 8.0) throw test_excep();
857
if(res.y != 5.0) throw test_excep();
858
if(res.z != 1.0) throw test_excep();
859
860
if(res2[0] != 14.0) throw test_excep();
861
if(res2[1] != 6.0) throw test_excep();
862
if(res2[2] != 1.0) throw test_excep();
863
864
Matx44f mat44f(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);
865
Matx44d mat44d(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);
866
Scalar s(4, 3, 2, 1);
867
Scalar sf = mat44f*s;
868
Scalar sd = mat44d*s;
869
870
if(sf[0] != 10.0) throw test_excep();
871
if(sf[1] != 6.0) throw test_excep();
872
if(sf[2] != 3.0) throw test_excep();
873
if(sf[3] != 1.0) throw test_excep();
874
875
if(sd[0] != 10.0) throw test_excep();
876
if(sd[1] != 6.0) throw test_excep();
877
if(sd[2] != 3.0) throw test_excep();
878
if(sd[3] != 1.0) throw test_excep();
879
}
880
catch(const test_excep&)
881
{
882
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
883
return false;
884
}
885
return true;
886
}
887
888
bool CV_OperationsTest::TestMatxElementwiseDivison()
889
{
890
try
891
{
892
Matx22f mat(2, 4, 6, 8);
893
Matx22f mat2(2, 2, 2, 2);
894
895
Matx22f res = mat.div(mat2);
896
897
if(res(0, 0) != 1.0) throw test_excep();
898
if(res(0, 1) != 2.0) throw test_excep();
899
if(res(1, 0) != 3.0) throw test_excep();
900
if(res(1, 1) != 4.0) throw test_excep();
901
}
902
catch(const test_excep&)
903
{
904
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
905
return false;
906
}
907
return true;
908
}
909
910
911
bool CV_OperationsTest::TestVec()
912
{
913
try
914
{
915
cv::Mat hsvImage_f(5, 5, CV_32FC3), hsvImage_b(5, 5, CV_8UC3);
916
int i = 0,j = 0;
917
cv::Vec3f a;
918
919
//these compile
920
cv::Vec3b b = a;
921
hsvImage_f.at<cv::Vec3f>(i,j) = cv::Vec3f((float)i,0,1);
922
hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3b(cv::Vec3f((float)i,0,1));
923
924
//these don't
925
b = cv::Vec3f(1,0,0);
926
cv::Vec3b c;
927
c = cv::Vec3f(0,0,1);
928
hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f((float)i,0,1);
929
hsvImage_b.at<cv::Vec3b>(i,j) = a;
930
hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f(1,2,3);
931
}
932
catch(const test_excep&)
933
{
934
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
935
return false;
936
}
937
return true;
938
}
939
940
bool CV_OperationsTest::operations1()
941
{
942
try
943
{
944
Point3d p1(1, 1, 1), p2(2, 2, 2), p4(4, 4, 4);
945
p1*=2;
946
if (!(p1 == p2)) throw test_excep();
947
if (!(p2 * 2 == p4)) throw test_excep();
948
if (!(p2 * 2.f == p4)) throw test_excep();
949
if (!(p2 * 2.f == p4)) throw test_excep();
950
951
Point2d pi1(1, 1), pi2(2, 2), pi4(4, 4);
952
pi1*=2;
953
if (!(pi1 == pi2)) throw test_excep();
954
if (!(pi2 * 2 == pi4)) throw test_excep();
955
if (!(pi2 * 2.f == pi4)) throw test_excep();
956
if (!(pi2 * 2.f == pi4)) throw test_excep();
957
958
Vec2d v12(1, 1), v22(2, 2);
959
v12*=2.0;
960
if (!(v12 == v22)) throw test_excep();
961
962
Vec3d v13(1, 1, 1), v23(2, 2, 2);
963
v13*=2.0;
964
if (!(v13 == v23)) throw test_excep();
965
966
Vec4d v14(1, 1, 1, 1), v24(2, 2, 2, 2);
967
v14*=2.0;
968
if (!(v14 == v24)) throw test_excep();
969
970
Size sz(10, 20);
971
if (sz.area() != 200) throw test_excep();
972
if (sz.width != 10 || sz.height != 20) throw test_excep();
973
if (cvSize(sz).width != 10 || cvSize(sz).height != 20) throw test_excep();
974
975
Vec<double, 5> v5d(1, 1, 1, 1, 1);
976
Vec<double, 6> v6d(1, 1, 1, 1, 1, 1);
977
Vec<double, 7> v7d(1, 1, 1, 1, 1, 1, 1);
978
Vec<double, 8> v8d(1, 1, 1, 1, 1, 1, 1, 1);
979
Vec<double, 9> v9d(1, 1, 1, 1, 1, 1, 1, 1, 1);
980
Vec<double,10> v10d(1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
981
982
Vec<double,10> v10dzero;
983
for (int ii = 0; ii < 10; ++ii) {
984
if (v10dzero[ii] != 0.0)
985
throw test_excep();
986
}
987
988
Mat A(1, 32, CV_32F), B;
989
for( int i = 0; i < A.cols; i++ )
990
A.at<float>(i) = (float)(i <= 12 ? i : 24 - i);
991
cv::transpose(A, B);
992
993
int minidx[2] = {0, 0}, maxidx[2] = {0, 0};
994
double minval = 0, maxval = 0;
995
cv::minMaxIdx(A, &minval, &maxval, minidx, maxidx);
996
997
if( !(minidx[0] == 0 && minidx[1] == 31 && maxidx[0] == 0 && maxidx[1] == 12 &&
998
minval == -7 && maxval == 12))
999
throw test_excep();
1000
1001
cv::minMaxIdx(B, &minval, &maxval, minidx, maxidx);
1002
1003
if( !(minidx[0] == 31 && minidx[1] == 0 && maxidx[0] == 12 && maxidx[1] == 0 &&
1004
minval == -7 && maxval == 12))
1005
throw test_excep();
1006
1007
Matx33f b(1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f);
1008
Mat c;
1009
cv::add(Mat::zeros(3, 3, CV_32F), b, c);
1010
CV_Assert( cvtest::norm(b, c, CV_C) == 0 );
1011
1012
cv::add(Mat::zeros(3, 3, CV_64F), b, c, noArray(), c.type());
1013
CV_Assert( cvtest::norm(b, c, CV_C) == 0 );
1014
1015
cv::add(Mat::zeros(6, 1, CV_64F), 1, c, noArray(), c.type());
1016
CV_Assert( cvtest::norm(Matx61f(1.f, 1.f, 1.f, 1.f, 1.f, 1.f), c, CV_C) == 0 );
1017
1018
vector<Point2f> pt2d(3);
1019
vector<Point3d> pt3d(2);
1020
1021
CV_Assert( Mat(pt2d).checkVector(2) == 3 && Mat(pt2d).checkVector(3) < 0 &&
1022
Mat(pt3d).checkVector(2) < 0 && Mat(pt3d).checkVector(3) == 2 );
1023
1024
Matx44f m44(0.8147f, 0.6324f, 0.9575f, 0.9572f,
1025
0.9058f, 0.0975f, 0.9649f, 0.4854f,
1026
0.1270f, 0.2785f, 0.1576f, 0.8003f,
1027
0.9134f, 0.5469f, 0.9706f, 0.1419f);
1028
double d = cv::determinant(m44);
1029
CV_Assert( fabs(d - (-0.0262)) <= 0.001 );
1030
1031
Cv32suf z;
1032
z.i = 0x80000000;
1033
CV_Assert( cvFloor(z.f) == 0 && cvCeil(z.f) == 0 && cvRound(z.f) == 0 );
1034
}
1035
catch(const test_excep&)
1036
{
1037
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
1038
return false;
1039
}
1040
return true;
1041
}
1042
1043
1044
bool CV_OperationsTest::TestExp()
1045
{
1046
Mat1f tt = Mat1f::ones(4,2);
1047
Mat1f outs;
1048
exp(-tt, outs);
1049
Mat1f tt2 = Mat1f::ones(4,1), outs2;
1050
exp(-tt2, outs2);
1051
return true;
1052
}
1053
1054
1055
bool CV_OperationsTest::TestSVD()
1056
{
1057
try
1058
{
1059
Mat A = (Mat_<double>(3,4) << 1, 2, -1, 4, 2, 4, 3, 5, -1, -2, 6, 7);
1060
Mat x;
1061
SVD::solveZ(A,x);
1062
if( cvtest::norm(A*x, CV_C) > FLT_EPSILON )
1063
throw test_excep();
1064
1065
SVD svd(A, SVD::FULL_UV);
1066
if( cvtest::norm(A*svd.vt.row(3).t(), CV_C) > FLT_EPSILON )
1067
throw test_excep();
1068
1069
Mat Dp(3,3,CV_32FC1);
1070
Mat Dc(3,3,CV_32FC1);
1071
Mat Q(3,3,CV_32FC1);
1072
Mat U,Vt,R,T,W;
1073
1074
Dp.at<float>(0,0)=0.86483884f; Dp.at<float>(0,1)= -0.3077251f; Dp.at<float>(0,2)=-0.55711365f;
1075
Dp.at<float>(1,0)=0.49294353f; Dp.at<float>(1,1)=-0.24209651f; Dp.at<float>(1,2)=-0.25084701f;
1076
Dp.at<float>(2,0)=0; Dp.at<float>(2,1)=0; Dp.at<float>(2,2)=0;
1077
1078
Dc.at<float>(0,0)=0.75632739f; Dc.at<float>(0,1)= -0.38859656f; Dc.at<float>(0,2)=-0.36773083f;
1079
Dc.at<float>(1,0)=0.9699229f; Dc.at<float>(1,1)=-0.49858192f; Dc.at<float>(1,2)=-0.47134098f;
1080
Dc.at<float>(2,0)=0.10566688f; Dc.at<float>(2,1)=-0.060333252f; Dc.at<float>(2,2)=-0.045333147f;
1081
1082
Q=Dp*Dc.t();
1083
SVD decomp;
1084
decomp=SVD(Q);
1085
U=decomp.u;
1086
Vt=decomp.vt;
1087
W=decomp.w;
1088
Mat I = Mat::eye(3, 3, CV_32F);
1089
1090
if( cvtest::norm(U*U.t(), I, CV_C) > FLT_EPSILON ||
1091
cvtest::norm(Vt*Vt.t(), I, CV_C) > FLT_EPSILON ||
1092
W.at<float>(2) < 0 || W.at<float>(1) < W.at<float>(2) ||
1093
W.at<float>(0) < W.at<float>(1) ||
1094
cvtest::norm(U*Mat::diag(W)*Vt, Q, CV_C) > FLT_EPSILON )
1095
throw test_excep();
1096
}
1097
catch(const test_excep&)
1098
{
1099
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
1100
return false;
1101
}
1102
return true;
1103
}
1104
1105
void CV_OperationsTest::run( int /* start_from */)
1106
{
1107
if (!TestMat())
1108
return;
1109
1110
if (!SomeMatFunctions())
1111
return;
1112
1113
if (!TestTemplateMat())
1114
return;
1115
1116
if (!TestMatND())
1117
return;
1118
1119
if (!TestSparseMat())
1120
return;
1121
1122
if (!TestVec())
1123
return;
1124
1125
if (!TestMatxMultiplication())
1126
return;
1127
1128
if (!TestMatxElementwiseDivison())
1129
return;
1130
1131
if (!TestSubMatAccess())
1132
return;
1133
1134
if (!TestExp())
1135
return;
1136
1137
if (!TestSVD())
1138
return;
1139
1140
if (!operations1())
1141
return;
1142
1143
ts->set_failed_test_info(cvtest::TS::OK);
1144
}
1145
1146
TEST(Core_Array, expressions) { CV_OperationsTest test; test.safe_run(); }
1147
1148
class CV_SparseMatTest : public cvtest::BaseTest
1149
{
1150
public:
1151
CV_SparseMatTest() {}
1152
~CV_SparseMatTest() {}
1153
protected:
1154
void run(int)
1155
{
1156
try
1157
{
1158
RNG& rng = theRNG();
1159
const int MAX_DIM=3;
1160
int sizes[MAX_DIM], idx[MAX_DIM];
1161
for( int iter = 0; iter < 100; iter++ )
1162
{
1163
ts->printf(cvtest::TS::LOG, ".");
1164
ts->update_context(this, iter, true);
1165
int k, dims = rng.uniform(1, MAX_DIM+1), p = 1;
1166
for( k = 0; k < dims; k++ )
1167
{
1168
sizes[k] = rng.uniform(1, 30);
1169
p *= sizes[k];
1170
}
1171
int j, nz = rng.uniform(0, (p+2)/2), nz0 = 0;
1172
SparseMat_<int> v(dims,sizes);
1173
1174
CV_Assert( (int)v.nzcount() == 0 );
1175
1176
SparseMatIterator_<int> it = v.begin();
1177
SparseMatIterator_<int> it_end = v.end();
1178
1179
for( k = 0; it != it_end; ++it, ++k )
1180
;
1181
CV_Assert( k == 0 );
1182
1183
int sum0 = 0, sum = 0;
1184
for( j = 0; j < nz; j++ )
1185
{
1186
int val = rng.uniform(1, 100);
1187
for( k = 0; k < dims; k++ )
1188
idx[k] = rng.uniform(0, sizes[k]);
1189
if( dims == 1 )
1190
{
1191
CV_Assert( v.ref(idx[0]) == v(idx[0]) );
1192
}
1193
else if( dims == 2 )
1194
{
1195
CV_Assert( v.ref(idx[0], idx[1]) == v(idx[0], idx[1]) );
1196
}
1197
else if( dims == 3 )
1198
{
1199
CV_Assert( v.ref(idx[0], idx[1], idx[2]) == v(idx[0], idx[1], idx[2]) );
1200
}
1201
CV_Assert( v.ref(idx) == v(idx) );
1202
v.ref(idx) += val;
1203
if( v(idx) == val )
1204
nz0++;
1205
sum0 += val;
1206
}
1207
1208
CV_Assert( (int)v.nzcount() == nz0 );
1209
1210
it = v.begin();
1211
it_end = v.end();
1212
1213
for( k = 0; it != it_end; ++it, ++k )
1214
sum += *it;
1215
CV_Assert( k == nz0 && sum == sum0 );
1216
1217
v.clear();
1218
CV_Assert( (int)v.nzcount() == 0 );
1219
1220
it = v.begin();
1221
it_end = v.end();
1222
1223
for( k = 0; it != it_end; ++it, ++k )
1224
;
1225
CV_Assert( k == 0 );
1226
}
1227
}
1228
catch(...)
1229
{
1230
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
1231
}
1232
}
1233
};
1234
1235
TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }
1236
1237
TEST(MatTestRoi, adjustRoiOverflow)
1238
{
1239
Mat m(15, 10, CV_32S);
1240
Mat roi(m, cv::Range(2, 10), cv::Range(3,6));
1241
int rowsInROI = roi.rows;
1242
roi.adjustROI(1, 0, 0, 0);
1243
1244
ASSERT_EQ(roi.rows, rowsInROI + 1);
1245
1246
roi.adjustROI(-m.rows, -m.rows, 0, 0);
1247
1248
ASSERT_EQ(roi.rows, m.rows);
1249
}
1250
1251
1252
CV_ENUM(SortRowCol, SORT_EVERY_COLUMN, SORT_EVERY_ROW)
1253
CV_ENUM(SortOrder, SORT_ASCENDING, SORT_DESCENDING)
1254
1255
PARAM_TEST_CASE(sortIdx, MatDepth, SortRowCol, SortOrder, Size, bool)
1256
{
1257
int type;
1258
Size size;
1259
int flags;
1260
bool use_roi;
1261
1262
Mat src, src_roi;
1263
Mat dst, dst_roi;
1264
1265
virtual void SetUp()
1266
{
1267
int depth = GET_PARAM(0);
1268
int rowFlags = GET_PARAM(1);
1269
int orderFlags = GET_PARAM(2);
1270
size = GET_PARAM(3);
1271
use_roi = GET_PARAM(4);
1272
1273
type = CV_MAKE_TYPE(depth, 1);
1274
1275
flags = rowFlags | orderFlags;
1276
}
1277
1278
void generateTestData()
1279
{
1280
Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
1281
randomSubMat(src, src_roi, size, srcBorder, type, -100, 100);
1282
1283
Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
1284
randomSubMat(dst, dst_roi, size, dstBorder, CV_32S, 5, 16);
1285
}
1286
1287
template<typename T>
1288
void check_(const cv::Mat& values_, const cv::Mat_<int>& idx_)
1289
{
1290
cv::Mat_<T>& values = (cv::Mat_<T>&)values_;
1291
cv::Mat_<int>& idx = (cv::Mat_<int>&)idx_;
1292
size_t N = values.total();
1293
std::vector<bool> processed(N, false);
1294
int prevIdx = idx(0);
1295
T prevValue = values(prevIdx);
1296
processed[prevIdx] = true;
1297
for (size_t i = 1; i < N; i++)
1298
{
1299
int nextIdx = idx((int)i);
1300
T value = values(nextIdx);
1301
ASSERT_EQ(false, processed[nextIdx]) << "Indexes must be unique. i=" << i << " idx=" << nextIdx << std::endl << idx;
1302
processed[nextIdx] = true;
1303
if ((flags & SORT_DESCENDING) == SORT_DESCENDING)
1304
ASSERT_GE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;
1305
else
1306
ASSERT_LE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;
1307
prevValue = value;
1308
prevIdx = nextIdx;
1309
}
1310
}
1311
1312
void validate()
1313
{
1314
ASSERT_EQ(CV_32SC1, dst_roi.type());
1315
ASSERT_EQ(size, dst_roi.size());
1316
bool isColumn = (flags & SORT_EVERY_COLUMN) == SORT_EVERY_COLUMN;
1317
size_t N = isColumn ? src_roi.cols : src_roi.rows;
1318
Mat values_row((int)N, 1, type), idx_row((int)N, 1, CV_32S);
1319
for (size_t i = 0; i < N; i++)
1320
{
1321
SCOPED_TRACE(cv::format("row/col=%d", (int)i));
1322
if (isColumn)
1323
{
1324
src_roi.col((int)i).copyTo(values_row);
1325
dst_roi.col((int)i).copyTo(idx_row);
1326
}
1327
else
1328
{
1329
src_roi.row((int)i).copyTo(values_row);
1330
dst_roi.row((int)i).copyTo(idx_row);
1331
}
1332
switch(type)
1333
{
1334
case CV_8U: check_<uchar>(values_row, idx_row); break;
1335
case CV_8S: check_<char>(values_row, idx_row); break;
1336
case CV_16S: check_<short>(values_row, idx_row); break;
1337
case CV_32S: check_<int>(values_row, idx_row); break;
1338
case CV_32F: check_<float>(values_row, idx_row); break;
1339
case CV_64F: check_<double>(values_row, idx_row); break;
1340
default: ASSERT_FALSE(true) << "Unsupported type: " << type;
1341
}
1342
}
1343
}
1344
};
1345
1346
TEST_P(sortIdx, simple)
1347
{
1348
for (int j = 0; j < 5; j++)
1349
{
1350
generateTestData();
1351
1352
cv::sortIdx(src_roi, dst_roi, flags);
1353
validate();
1354
}
1355
}
1356
1357
INSTANTIATE_TEST_CASE_P(Core, sortIdx, Combine(
1358
Values(CV_8U, CV_8S, CV_16S, CV_32S, CV_32F, CV_64F), // depth
1359
Values(SORT_EVERY_COLUMN, SORT_EVERY_ROW),
1360
Values(SORT_ASCENDING, SORT_DESCENDING),
1361
Values(Size(3, 3), Size(16, 8)),
1362
::testing::Bool()
1363
));
1364
1365
1366
TEST(Core_sortIdx, regression_8941)
1367
{
1368
cv::Mat src = (cv::Mat_<int>(3, 3) <<
1369
1, 2, 3,
1370
0, 9, 5,
1371
8, 1, 6
1372
);
1373
cv::Mat expected = (cv::Mat_<int>(3, 1) <<
1374
1,
1375
0,
1376
2
1377
);
1378
1379
cv::Mat result;
1380
cv::sortIdx(src.col(0), result, CV_SORT_EVERY_COLUMN | CV_SORT_ASCENDING);
1381
#if 0
1382
std::cout << src.col(0) << std::endl;
1383
std::cout << result << std::endl;
1384
#endif
1385
ASSERT_EQ(expected.size(), result.size());
1386
EXPECT_EQ(0, cvtest::norm(expected, result, NORM_INF)) <<
1387
"result=" << std::endl << result << std::endl <<
1388
"expected=" << std::endl << expected;
1389
}
1390
1391
}} // namespace
1392
1393