forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_math.cpp
More file actions
3865 lines (3277 loc) · 123 KB
/
test_math.cpp
File metadata and controls
3865 lines (3277 loc) · 123 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////////////////
/////////////////// tests for matrix operations and math functions ///////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
#include "test_precomp.hpp"
#include <float.h>
#include <math.h>
#include "opencv2/core/softfloat.hpp"
using namespace cv;
using namespace std;
/// !!! NOTE !!! These tests happily avoid overflow cases & out-of-range arguments
/// so that output arrays contain neigher Inf's nor Nan's.
/// Handling such cases would require special modification of check function
/// (validate_test_results) => TBD.
/// Also, need some logarithmic-scale generation of input data. Right now it is done (in some tests)
/// by generating min/max boundaries for random data in logarimithic scale, but
/// within the same test case all the input array elements are of the same order.
class Core_MathTest : public cvtest::ArrayTest
{
public:
typedef cvtest::ArrayTest Base;
Core_MathTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes,
vector<vector<int> >& types);
double get_success_error_level( int /*test_case_idx*/, int i, int j );
bool test_nd;
};
Core_MathTest::Core_MathTest()
{
optional_mask = false;
test_array[INPUT].push_back(NULL);
test_array[OUTPUT].push_back(NULL);
test_array[REF_OUTPUT].push_back(NULL);
test_nd = false;
}
double Core_MathTest::get_success_error_level( int /*test_case_idx*/, int i, int j )
{
return test_mat[i][j].depth() == CV_32F ? FLT_EPSILON*128 : DBL_EPSILON*1024;
}
void Core_MathTest::get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types)
{
RNG& rng = ts->get_rng();
int depth = cvtest::randInt(rng)%2 + CV_32F;
int cn = cvtest::randInt(rng) % 4 + 1, type = CV_MAKETYPE(depth, cn);
size_t i, j;
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
for( i = 0; i < test_array.size(); i++ )
{
size_t count = test_array[i].size();
for( j = 0; j < count; j++ )
types[i][j] = type;
}
test_nd = cvtest::randInt(rng)%3 == 0;
}
////////// pow /////////////
class Core_PowTest : public Core_MathTest
{
public:
typedef Core_MathTest Base;
Core_PowTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types );
void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
void run_func();
void prepare_to_validation( int test_case_idx );
double get_success_error_level( int test_case_idx, int i, int j );
double power;
};
Core_PowTest::Core_PowTest()
{
power = 0;
}
void Core_PowTest::get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int depth = cvtest::randInt(rng) % (CV_64F+1);
int cn = cvtest::randInt(rng) % 4 + 1;
size_t i, j;
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
depth += depth == CV_8S;
if( depth < CV_32F || cvtest::randInt(rng)%8 == 0 )
// integer power
power = (int)(cvtest::randInt(rng)%21 - 10);
else
{
i = cvtest::randInt(rng)%17;
power = i == 16 ? 1./3 : i == 15 ? 0.5 : i == 14 ? -0.5 : cvtest::randReal(rng)*10 - 5;
}
for( i = 0; i < test_array.size(); i++ )
{
size_t count = test_array[i].size();
int type = CV_MAKETYPE(depth, cn);
for( j = 0; j < count; j++ )
types[i][j] = type;
}
test_nd = cvtest::randInt(rng)%3 == 0;
}
double Core_PowTest::get_success_error_level( int test_case_idx, int i, int j )
{
int depth = test_mat[i][j].depth();
if( depth < CV_32F )
return power == cvRound(power) && power >= 0 ? 0 : 1;
else
return Base::get_success_error_level( test_case_idx, i, j );
}
void Core_PowTest::get_minmax_bounds( int /*i*/, int /*j*/, int type, Scalar& low, Scalar& high )
{
double l, u = cvtest::randInt(ts->get_rng())%1000 + 1;
if( power > 0 )
{
double mval = cvtest::getMaxVal(type);
double u1 = pow(mval,1./power)*2;
u = MIN(u,u1);
}
l = power == cvRound(power) ? -u : FLT_EPSILON;
low = Scalar::all(l);
high = Scalar::all(u);
}
void Core_PowTest::run_func()
{
if(!test_nd)
{
if( fabs(power-1./3) <= DBL_EPSILON && test_mat[INPUT][0].depth() == CV_32F )
{
Mat a = test_mat[INPUT][0], b = test_mat[OUTPUT][0];
a = a.reshape(1);
b = b.reshape(1);
for( int i = 0; i < a.rows; i++ )
{
b.at<float>(i,0) = (float)fabs(cvCbrt(a.at<float>(i,0)));
for( int j = 1; j < a.cols; j++ )
b.at<float>(i,j) = (float)fabs(cv::cubeRoot(a.at<float>(i,j)));
}
}
else
cvPow( test_array[INPUT][0], test_array[OUTPUT][0], power );
}
else
{
Mat& a = test_mat[INPUT][0];
Mat& b = test_mat[OUTPUT][0];
if(power == 0.5)
cv::sqrt(a, b);
else
cv::pow(a, power, b);
}
}
inline static int ipow( int a, int power )
{
int b = 1;
while( power > 0 )
{
if( power&1 )
b *= a, power--;
else
a *= a, power >>= 1;
}
return b;
}
inline static double ipow( double a, int power )
{
double b = 1.;
while( power > 0 )
{
if( power&1 )
b *= a, power--;
else
a *= a, power >>= 1;
}
return b;
}
void Core_PowTest::prepare_to_validation( int /*test_case_idx*/ )
{
const Mat& a = test_mat[INPUT][0];
Mat& b = test_mat[REF_OUTPUT][0];
int depth = a.depth();
int ncols = a.cols*a.channels();
int ipower = cvRound(power), apower = abs(ipower);
int i, j;
for( i = 0; i < a.rows; i++ )
{
const uchar* a_data = a.ptr(i);
uchar* b_data = b.ptr(i);
switch( depth )
{
case CV_8U:
if( ipower < 0 )
for( j = 0; j < ncols; j++ )
{
int val = ((uchar*)a_data)[j];
((uchar*)b_data)[j] = (uchar)(val == 0 ? 255 : val == 1 ? 1 :
val == 2 && ipower == -1 ? 1 : 0);
}
else
for( j = 0; j < ncols; j++ )
{
int val = ((uchar*)a_data)[j];
val = ipow( val, ipower );
((uchar*)b_data)[j] = saturate_cast<uchar>(val);
}
break;
case CV_8S:
if( ipower < 0 )
for( j = 0; j < ncols; j++ )
{
int val = ((schar*)a_data)[j];
((schar*)b_data)[j] = (schar)(val == 0 ? 127 : val == 1 ? 1 :
val ==-1 ? 1-2*(ipower&1) :
val == 2 && ipower == -1 ? 1 : 0);
}
else
for( j = 0; j < ncols; j++ )
{
int val = ((schar*)a_data)[j];
val = ipow( val, ipower );
((schar*)b_data)[j] = saturate_cast<schar>(val);
}
break;
case CV_16U:
if( ipower < 0 )
for( j = 0; j < ncols; j++ )
{
int val = ((ushort*)a_data)[j];
((ushort*)b_data)[j] = (ushort)(val == 0 ? 65535 : val == 1 ? 1 :
val ==-1 ? 1-2*(ipower&1) :
val == 2 && ipower == -1 ? 1 : 0);
}
else
for( j = 0; j < ncols; j++ )
{
int val = ((ushort*)a_data)[j];
val = ipow( val, ipower );
((ushort*)b_data)[j] = saturate_cast<ushort>(val);
}
break;
case CV_16S:
if( ipower < 0 )
for( j = 0; j < ncols; j++ )
{
int val = ((short*)a_data)[j];
((short*)b_data)[j] = (short)(val == 0 ? 32767 : val == 1 ? 1 :
val ==-1 ? 1-2*(ipower&1) :
val == 2 && ipower == -1 ? 1 : 0);
}
else
for( j = 0; j < ncols; j++ )
{
int val = ((short*)a_data)[j];
val = ipow( val, ipower );
((short*)b_data)[j] = saturate_cast<short>(val);
}
break;
case CV_32S:
if( ipower < 0 )
for( j = 0; j < ncols; j++ )
{
int val = ((int*)a_data)[j];
((int*)b_data)[j] = val == 0 ? INT_MAX : val == 1 ? 1 :
val ==-1 ? 1-2*(ipower&1) :
val == 2 && ipower == -1 ? 1 : 0;
}
else
for( j = 0; j < ncols; j++ )
{
int val = ((int*)a_data)[j];
val = ipow( val, ipower );
((int*)b_data)[j] = val;
}
break;
case CV_32F:
if( power != ipower )
for( j = 0; j < ncols; j++ )
{
double val = ((float*)a_data)[j];
val = pow( fabs(val), power );
((float*)b_data)[j] = (float)val;
}
else
for( j = 0; j < ncols; j++ )
{
double val = ((float*)a_data)[j];
if( ipower < 0 )
val = 1./val;
val = ipow( val, apower );
((float*)b_data)[j] = (float)val;
}
break;
case CV_64F:
if( power != ipower )
for( j = 0; j < ncols; j++ )
{
double val = ((double*)a_data)[j];
val = pow( fabs(val), power );
((double*)b_data)[j] = (double)val;
}
else
for( j = 0; j < ncols; j++ )
{
double val = ((double*)a_data)[j];
if( ipower < 0 )
val = 1./val;
val = ipow( val, apower );
((double*)b_data)[j] = (double)val;
}
break;
}
}
}
///////////////////////////////////////// matrix tests ////////////////////////////////////////////
class Core_MatrixTest : public cvtest::ArrayTest
{
public:
typedef cvtest::ArrayTest Base;
Core_MatrixTest( int in_count, int out_count,
bool allow_int, bool scalar_output, int max_cn );
protected:
void get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types );
double get_success_error_level( int test_case_idx, int i, int j );
bool allow_int;
bool scalar_output;
int max_cn;
};
Core_MatrixTest::Core_MatrixTest( int in_count, int out_count,
bool _allow_int, bool _scalar_output, int _max_cn )
: allow_int(_allow_int), scalar_output(_scalar_output), max_cn(_max_cn)
{
int i;
for( i = 0; i < in_count; i++ )
test_array[INPUT].push_back(NULL);
for( i = 0; i < out_count; i++ )
{
test_array[OUTPUT].push_back(NULL);
test_array[REF_OUTPUT].push_back(NULL);
}
element_wise_relative_error = false;
}
void Core_MatrixTest::get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int depth = cvtest::randInt(rng) % (allow_int ? CV_64F+1 : 2);
int cn = cvtest::randInt(rng) % max_cn + 1;
size_t i, j;
if( allow_int )
depth += depth == CV_8S;
else
depth += CV_32F;
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
for( i = 0; i < test_array.size(); i++ )
{
size_t count = test_array[i].size();
int flag = (i == OUTPUT || i == REF_OUTPUT) && scalar_output;
int type = !flag ? CV_MAKETYPE(depth, cn) : CV_64FC1;
for( j = 0; j < count; j++ )
{
types[i][j] = type;
if( flag )
sizes[i][j] = Size( 4, 1 );
}
}
}
double Core_MatrixTest::get_success_error_level( int test_case_idx, int i, int j )
{
int input_depth = test_mat[INPUT][0].depth();
double input_precision = input_depth < CV_32F ? 0 : input_depth == CV_32F ? 5e-5 : 5e-10;
double output_precision = Base::get_success_error_level( test_case_idx, i, j );
return MAX(input_precision, output_precision);
}
///////////////// Trace /////////////////////
class Core_TraceTest : public Core_MatrixTest
{
public:
Core_TraceTest();
protected:
void run_func();
void prepare_to_validation( int test_case_idx );
};
Core_TraceTest::Core_TraceTest() : Core_MatrixTest( 1, 1, true, true, 4 )
{
}
void Core_TraceTest::run_func()
{
test_mat[OUTPUT][0].at<Scalar>(0,0) = cvTrace(test_array[INPUT][0]);
}
void Core_TraceTest::prepare_to_validation( int )
{
Mat& mat = test_mat[INPUT][0];
int count = MIN( mat.rows, mat.cols );
Mat diag(count, 1, mat.type(), mat.ptr(), mat.step + mat.elemSize());
Scalar r = cvtest::mean(diag);
r *= (double)count;
test_mat[REF_OUTPUT][0].at<Scalar>(0,0) = r;
}
///////// dotproduct //////////
class Core_DotProductTest : public Core_MatrixTest
{
public:
Core_DotProductTest();
protected:
void run_func();
void prepare_to_validation( int test_case_idx );
};
Core_DotProductTest::Core_DotProductTest() : Core_MatrixTest( 2, 1, true, true, 4 )
{
}
void Core_DotProductTest::run_func()
{
test_mat[OUTPUT][0].at<Scalar>(0,0) = Scalar(cvDotProduct( test_array[INPUT][0], test_array[INPUT][1] ));
}
void Core_DotProductTest::prepare_to_validation( int )
{
test_mat[REF_OUTPUT][0].at<Scalar>(0,0) = Scalar(cvtest::crossCorr( test_mat[INPUT][0], test_mat[INPUT][1] ));
}
///////// crossproduct //////////
class Core_CrossProductTest : public Core_MatrixTest
{
public:
Core_CrossProductTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes,
vector<vector<int> >& types );
void run_func();
void prepare_to_validation( int test_case_idx );
};
Core_CrossProductTest::Core_CrossProductTest() : Core_MatrixTest( 2, 1, false, false, 1 )
{
}
void Core_CrossProductTest::get_test_array_types_and_sizes( int,
vector<vector<Size> >& sizes,
vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int depth = cvtest::randInt(rng) % 2 + CV_32F;
int cn = cvtest::randInt(rng) & 1 ? 3 : 1, type = CV_MAKETYPE(depth, cn);
CvSize sz;
types[INPUT][0] = types[INPUT][1] = types[OUTPUT][0] = types[REF_OUTPUT][0] = type;
if( cn == 3 )
sz = Size(1,1);
else if( cvtest::randInt(rng) & 1 )
sz = Size(3,1);
else
sz = Size(1,3);
sizes[INPUT][0] = sizes[INPUT][1] = sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = sz;
}
void Core_CrossProductTest::run_func()
{
cvCrossProduct( test_array[INPUT][0], test_array[INPUT][1], test_array[OUTPUT][0] );
}
void Core_CrossProductTest::prepare_to_validation( int )
{
CvScalar a(0), b(0), c(0);
if( test_mat[INPUT][0].rows > 1 )
{
a.val[0] = cvGetReal2D( test_array[INPUT][0], 0, 0 );
a.val[1] = cvGetReal2D( test_array[INPUT][0], 1, 0 );
a.val[2] = cvGetReal2D( test_array[INPUT][0], 2, 0 );
b.val[0] = cvGetReal2D( test_array[INPUT][1], 0, 0 );
b.val[1] = cvGetReal2D( test_array[INPUT][1], 1, 0 );
b.val[2] = cvGetReal2D( test_array[INPUT][1], 2, 0 );
}
else if( test_mat[INPUT][0].cols > 1 )
{
a.val[0] = cvGetReal1D( test_array[INPUT][0], 0 );
a.val[1] = cvGetReal1D( test_array[INPUT][0], 1 );
a.val[2] = cvGetReal1D( test_array[INPUT][0], 2 );
b.val[0] = cvGetReal1D( test_array[INPUT][1], 0 );
b.val[1] = cvGetReal1D( test_array[INPUT][1], 1 );
b.val[2] = cvGetReal1D( test_array[INPUT][1], 2 );
}
else
{
a = cvGet1D( test_array[INPUT][0], 0 );
b = cvGet1D( test_array[INPUT][1], 0 );
}
c.val[2] = a.val[0]*b.val[1] - a.val[1]*b.val[0];
c.val[1] = -a.val[0]*b.val[2] + a.val[2]*b.val[0];
c.val[0] = a.val[1]*b.val[2] - a.val[2]*b.val[1];
if( test_mat[REF_OUTPUT][0].rows > 1 )
{
cvSetReal2D( test_array[REF_OUTPUT][0], 0, 0, c.val[0] );
cvSetReal2D( test_array[REF_OUTPUT][0], 1, 0, c.val[1] );
cvSetReal2D( test_array[REF_OUTPUT][0], 2, 0, c.val[2] );
}
else if( test_mat[REF_OUTPUT][0].cols > 1 )
{
cvSetReal1D( test_array[REF_OUTPUT][0], 0, c.val[0] );
cvSetReal1D( test_array[REF_OUTPUT][0], 1, c.val[1] );
cvSetReal1D( test_array[REF_OUTPUT][0], 2, c.val[2] );
}
else
{
cvSet1D( test_array[REF_OUTPUT][0], 0, c );
}
}
///////////////// gemm /////////////////////
class Core_GEMMTest : public Core_MatrixTest
{
public:
typedef Core_MatrixTest Base;
Core_GEMMTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
void get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high );
int prepare_test_case( int test_case_idx );
void run_func();
void prepare_to_validation( int test_case_idx );
int tabc_flag;
double alpha, beta;
};
Core_GEMMTest::Core_GEMMTest() : Core_MatrixTest( 5, 1, false, false, 2 )
{
test_case_count = 100;
max_log_array_size = 10;
tabc_flag = 0;
alpha = beta = 0;
}
void Core_GEMMTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
Size sizeA;
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
sizeA = sizes[INPUT][0];
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
sizes[INPUT][0] = sizeA;
sizes[INPUT][2] = sizes[INPUT][3] = Size(1,1);
types[INPUT][2] = types[INPUT][3] &= ~CV_MAT_CN_MASK;
tabc_flag = cvtest::randInt(rng) & 7;
switch( tabc_flag & (CV_GEMM_A_T|CV_GEMM_B_T) )
{
case 0:
sizes[INPUT][1].height = sizes[INPUT][0].width;
sizes[OUTPUT][0].height = sizes[INPUT][0].height;
sizes[OUTPUT][0].width = sizes[INPUT][1].width;
break;
case CV_GEMM_B_T:
sizes[INPUT][1].width = sizes[INPUT][0].width;
sizes[OUTPUT][0].height = sizes[INPUT][0].height;
sizes[OUTPUT][0].width = sizes[INPUT][1].height;
break;
case CV_GEMM_A_T:
sizes[INPUT][1].height = sizes[INPUT][0].height;
sizes[OUTPUT][0].height = sizes[INPUT][0].width;
sizes[OUTPUT][0].width = sizes[INPUT][1].width;
break;
case CV_GEMM_A_T | CV_GEMM_B_T:
sizes[INPUT][1].width = sizes[INPUT][0].height;
sizes[OUTPUT][0].height = sizes[INPUT][0].width;
sizes[OUTPUT][0].width = sizes[INPUT][1].height;
break;
}
sizes[REF_OUTPUT][0] = sizes[OUTPUT][0];
if( cvtest::randInt(rng) & 1 )
sizes[INPUT][4] = Size(0,0);
else if( !(tabc_flag & CV_GEMM_C_T) )
sizes[INPUT][4] = sizes[OUTPUT][0];
else
{
sizes[INPUT][4].width = sizes[OUTPUT][0].height;
sizes[INPUT][4].height = sizes[OUTPUT][0].width;
}
}
int Core_GEMMTest::prepare_test_case( int test_case_idx )
{
int code = Base::prepare_test_case( test_case_idx );
if( code > 0 )
{
alpha = cvGetReal2D( test_array[INPUT][2], 0, 0 );
beta = cvGetReal2D( test_array[INPUT][3], 0, 0 );
}
return code;
}
void Core_GEMMTest::get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high )
{
low = Scalar::all(-10.);
high = Scalar::all(10.);
}
void Core_GEMMTest::run_func()
{
cvGEMM( test_array[INPUT][0], test_array[INPUT][1], alpha,
test_array[INPUT][4], beta, test_array[OUTPUT][0], tabc_flag );
}
void Core_GEMMTest::prepare_to_validation( int )
{
cvtest::gemm( test_mat[INPUT][0], test_mat[INPUT][1], alpha,
test_array[INPUT][4] ? test_mat[INPUT][4] : Mat(),
beta, test_mat[REF_OUTPUT][0], tabc_flag );
}
///////////////// multransposed /////////////////////
class Core_MulTransposedTest : public Core_MatrixTest
{
public:
Core_MulTransposedTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
void get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high );
void run_func();
void prepare_to_validation( int test_case_idx );
int order;
};
Core_MulTransposedTest::Core_MulTransposedTest() : Core_MatrixTest( 2, 1, false, false, 1 )
{
test_case_count = 100;
order = 0;
test_array[TEMP].push_back(NULL);
}
void Core_MulTransposedTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int bits = cvtest::randInt(rng);
int src_type = cvtest::randInt(rng) % 5;
int dst_type = cvtest::randInt(rng) % 2;
src_type = src_type == 0 ? CV_8U : src_type == 1 ? CV_16U : src_type == 2 ? CV_16S :
src_type == 3 ? CV_32F : CV_64F;
dst_type = dst_type == 0 ? CV_32F : CV_64F;
dst_type = MAX( dst_type, src_type );
Core_MatrixTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
if( bits & 1 )
sizes[INPUT][1] = Size(0,0);
else
{
sizes[INPUT][1] = sizes[INPUT][0];
if( bits & 2 )
sizes[INPUT][1].height = 1;
if( bits & 4 )
sizes[INPUT][1].width = 1;
}
sizes[TEMP][0] = sizes[INPUT][0];
types[INPUT][0] = src_type;
types[OUTPUT][0] = types[REF_OUTPUT][0] = types[INPUT][1] = types[TEMP][0] = dst_type;
order = (bits & 8) != 0;
sizes[OUTPUT][0].width = sizes[OUTPUT][0].height = order == 0 ?
sizes[INPUT][0].height : sizes[INPUT][0].width;
sizes[REF_OUTPUT][0] = sizes[OUTPUT][0];
}
void Core_MulTransposedTest::get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high )
{
low = cvScalarAll(-10.);
high = cvScalarAll(10.);
}
void Core_MulTransposedTest::run_func()
{
cvMulTransposed( test_array[INPUT][0], test_array[OUTPUT][0],
order, test_array[INPUT][1] );
}
void Core_MulTransposedTest::prepare_to_validation( int )
{
const Mat& src = test_mat[INPUT][0];
Mat delta = test_mat[INPUT][1];
Mat& temp = test_mat[TEMP][0];
if( !delta.empty() )
{
if( delta.rows < src.rows || delta.cols < src.cols )
{
cv::repeat( delta, src.rows/delta.rows, src.cols/delta.cols, temp);
delta = temp;
}
cvtest::add( src, 1, delta, -1, Scalar::all(0), temp, temp.type());
}
else
src.convertTo(temp, temp.type());
cvtest::gemm( temp, temp, 1., Mat(), 0, test_mat[REF_OUTPUT][0], order == 0 ? GEMM_2_T : GEMM_1_T );
}
///////////////// Transform /////////////////////
class Core_TransformTest : public Core_MatrixTest
{
public:
typedef Core_MatrixTest Base;
Core_TransformTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
double get_success_error_level( int test_case_idx, int i, int j );
int prepare_test_case( int test_case_idx );
void run_func();
void prepare_to_validation( int test_case_idx );
double scale;
bool diagMtx;
};
Core_TransformTest::Core_TransformTest() : Core_MatrixTest( 3, 1, true, false, 4 )
{
scale = 1;
diagMtx = false;
}
void Core_TransformTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int bits = cvtest::randInt(rng);
int depth, dst_cn, mat_cols, mattype;
Base::get_test_array_types_and_sizes( test_case_idx, sizes, types );
mat_cols = CV_MAT_CN(types[INPUT][0]);
depth = CV_MAT_DEPTH(types[INPUT][0]);
dst_cn = cvtest::randInt(rng) % 4 + 1;
types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth, dst_cn);
mattype = depth < CV_32S ? CV_32F : depth == CV_64F ? CV_64F : bits & 1 ? CV_32F : CV_64F;
types[INPUT][1] = mattype;
types[INPUT][2] = CV_MAKETYPE(mattype, dst_cn);
scale = 1./((cvtest::randInt(rng)%4)*50+1);
if( bits & 2 )
{
sizes[INPUT][2] = Size(0,0);
mat_cols += (bits & 4) != 0;
}
else if( bits & 4 )
sizes[INPUT][2] = Size(1,1);
else
{
if( bits & 8 )
sizes[INPUT][2] = Size(dst_cn,1);
else
sizes[INPUT][2] = Size(1,dst_cn);
types[INPUT][2] &= ~CV_MAT_CN_MASK;
}
diagMtx = (bits & 16) != 0;
sizes[INPUT][1] = Size(mat_cols,dst_cn);
}
int Core_TransformTest::prepare_test_case( int test_case_idx )
{
int code = Base::prepare_test_case( test_case_idx );
if( code > 0 )
{
Mat& m = test_mat[INPUT][1];
cvtest::add(m, scale, m, 0, Scalar::all(0), m, m.type() );
if(diagMtx)
{
Mat mask = Mat::eye(m.rows, m.cols, CV_8U)*255;
mask = ~mask;
m.setTo(Scalar::all(0), mask);
}
}
return code;
}
double Core_TransformTest::get_success_error_level( int test_case_idx, int i, int j )
{
int depth = test_mat[INPUT][0].depth();
return depth <= CV_8S ? 1 : depth <= CV_32S ? 9 : Base::get_success_error_level( test_case_idx, i, j );
}
void Core_TransformTest::run_func()
{
CvMat _m = test_mat[INPUT][1], _shift = test_mat[INPUT][2];
cvTransform( test_array[INPUT][0], test_array[OUTPUT][0], &_m, _shift.data.ptr ? &_shift : 0);
}
void Core_TransformTest::prepare_to_validation( int )
{
Mat transmat = test_mat[INPUT][1];
Mat shift = test_mat[INPUT][2];
cvtest::transform( test_mat[INPUT][0], test_mat[REF_OUTPUT][0], transmat, shift );
}
class Core_TransformLargeTest : public Core_TransformTest
{
public:
typedef Core_MatrixTest Base;
protected:
void get_test_array_types_and_sizes(int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types);
};
void Core_TransformLargeTest::get_test_array_types_and_sizes(int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types)
{
RNG& rng = ts->get_rng();
int bits = cvtest::randInt(rng);
int depth, dst_cn, mat_cols, mattype;
Base::get_test_array_types_and_sizes(test_case_idx, sizes, types);
for (unsigned int j = 0; j < sizes.size(); j++)
{
for (unsigned int i = 0; i < sizes[j].size(); i++)
{
sizes[j][i].width *= 4;
}
}
mat_cols = CV_MAT_CN(types[INPUT][0]);
depth = CV_MAT_DEPTH(types[INPUT][0]);
dst_cn = cvtest::randInt(rng) % 4 + 1;
types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth, dst_cn);
mattype = depth < CV_32S ? CV_32F : depth == CV_64F ? CV_64F : bits & 1 ? CV_32F : CV_64F;
types[INPUT][1] = mattype;
types[INPUT][2] = CV_MAKETYPE(mattype, dst_cn);
scale = 1. / ((cvtest::randInt(rng) % 4) * 50 + 1);
if (bits & 2)
{
sizes[INPUT][2] = Size(0, 0);
mat_cols += (bits & 4) != 0;
}
else if (bits & 4)
sizes[INPUT][2] = Size(1, 1);
else
{
if (bits & 8)
sizes[INPUT][2] = Size(dst_cn, 1);
else
sizes[INPUT][2] = Size(1, dst_cn);
types[INPUT][2] &= ~CV_MAT_CN_MASK;
}
diagMtx = (bits & 16) != 0;
sizes[INPUT][1] = Size(mat_cols, dst_cn);
}
///////////////// PerspectiveTransform /////////////////////
class Core_PerspectiveTransformTest : public Core_MatrixTest
{
public:
Core_PerspectiveTransformTest();
protected:
void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
double get_success_error_level( int test_case_idx, int i, int j );
void run_func();
void prepare_to_validation( int test_case_idx );
};
Core_PerspectiveTransformTest::Core_PerspectiveTransformTest() : Core_MatrixTest( 2, 1, false, false, 2 )
{
}
void Core_PerspectiveTransformTest::get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
RNG& rng = ts->get_rng();
int bits = cvtest::randInt(rng);
int depth, cn, mattype;
Core_MatrixTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
cn = CV_MAT_CN(types[INPUT][0]) + 1;
depth = CV_MAT_DEPTH(types[INPUT][0]);
types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth, cn);
mattype = depth == CV_64F ? CV_64F : bits & 1 ? CV_32F : CV_64F;
types[INPUT][1] = mattype;
sizes[INPUT][1] = Size(cn + 1, cn + 1);
}
double Core_PerspectiveTransformTest::get_success_error_level( int test_case_idx, int i, int j )
{
int depth = test_mat[INPUT][0].depth();