Merge pull request #1704 from SpecLad:merge-2.4
[profile/ivi/opencv.git] / modules / ocl / test / test_arithm.cpp
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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Niko Li, newlife20080214@gmail.com
20 //    Jia Haipeng, jiahaipeng95@gmail.com
21 //    Shengen Yan, yanshengen@gmail.com
22 //    Jiang Liyuan,jlyuan001.good@163.com
23 //    Rock Li, Rock.Li@amd.com
24 //    Zailong Wu, bullet@yeah.net
25 //    Yao Wang, bitwangyaoyao@gmail.com
26 //
27 // Redistribution and use in source and binary forms, with or without modification,
28 // are permitted provided that the following conditions are met:
29 //
30 //   * Redistribution's of source code must retain the above copyright notice,
31 //     this list of conditions and the following disclaimer.
32 //
33 //   * Redistribution's in binary form must reproduce the above copyright notice,
34 //     this list of conditions and the following disclaimer in the documentation
35 //     and/or other materials provided with the distribution.
36 //
37 //   * The name of the copyright holders may not be used to endorse or promote products
38 //     derived from this software without specific prior written permission.
39 //
40 // This software is provided by the copyright holders and contributors "as is" and
41 // any express or implied warranties, including, but not limited to, the implied
42 // warranties of merchantability and fitness for a particular purpose are disclaimed.
43 // In no event shall the Intel Corporation or contributors be liable for any direct,
44 // indirect, incidental, special, exemplary, or consequential damages
45 // (including, but not limited to, procurement of substitute goods or services;
46 // loss of use, data, or profits; or business interruption) however caused
47 // and on any theory of liability, whether in contract, strict liability,
48 // or tort (including negligence or otherwise) arising in any way out of
49 // the use of this software, even if advised of the possibility of such damage.
50 //
51 //M*/
52
53 #include "test_precomp.hpp"
54 #include <iomanip>
55
56 #ifdef HAVE_OPENCL
57
58 using namespace cv;
59 using namespace cv::ocl;
60 using namespace cvtest;
61 using namespace testing;
62 using namespace std;
63
64 static bool relativeError(double actual, double expected, double eps)
65 {
66     return std::abs(actual - expected) / actual < eps;
67 }
68
69 //////////////////////////////// LUT /////////////////////////////////////////////////
70
71 PARAM_TEST_CASE(Lut, MatDepth, MatDepth, bool, bool)
72 {
73     int lut_depth;
74     int cn;
75     bool use_roi, same_cn;
76
77     // src mat
78     cv::Mat src;
79     cv::Mat lut;
80     cv::Mat dst;
81
82     // src mat with roi
83     cv::Mat src_roi;
84     cv::Mat lut_roi;
85     cv::Mat dst_roi;
86
87     // ocl dst mat for testing
88     cv::ocl::oclMat gsrc_whole;
89     cv::ocl::oclMat glut_whole;
90     cv::ocl::oclMat gdst_whole;
91
92     // ocl mat with roi
93     cv::ocl::oclMat gsrc_roi;
94     cv::ocl::oclMat glut_roi;
95     cv::ocl::oclMat gdst_roi;
96
97     virtual void SetUp()
98     {
99         lut_depth = GET_PARAM(0);
100         cn = GET_PARAM(1);
101         same_cn = GET_PARAM(2);
102         use_roi = GET_PARAM(3);
103     }
104
105     void random_roi()
106     {
107         const int src_type = CV_MAKE_TYPE(CV_8U, cn);
108         const int lut_type = CV_MAKE_TYPE(lut_depth, same_cn ? cn : 1);
109         const int dst_type = CV_MAKE_TYPE(lut_depth, cn);
110
111         Size roiSize = randomSize(1, MAX_VALUE);
112         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
113         randomSubMat(src, src_roi, roiSize, srcBorder, src_type, 0, 256);
114
115         Size lutRoiSize = Size(256, 1);
116         Border lutBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
117         randomSubMat(lut, lut_roi, lutRoiSize, lutBorder, lut_type, 5, 16);
118
119         Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
120         randomSubMat(dst, dst_roi, roiSize, dstBorder, dst_type, 5, 16);
121
122         generateOclMat(gsrc_whole, gsrc_roi, src, roiSize, srcBorder);
123         generateOclMat(glut_whole, glut_roi, lut, lutRoiSize, lutBorder);
124         generateOclMat(gdst_whole, gdst_roi, dst, roiSize, dstBorder);
125     }
126
127     void Near(double threshold = 0.)
128     {
129         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), threshold);
130         EXPECT_MAT_NEAR(dst_roi, Mat(gdst_roi), threshold);
131     }
132 };
133
134 OCL_TEST_P(Lut, Mat)
135 {
136     for (int j = 0; j < LOOP_TIMES; j++)
137     {
138         random_roi();
139
140         cv::LUT(src_roi, lut_roi, dst_roi);
141         cv::ocl::LUT(gsrc_roi, glut_roi, gdst_roi);
142
143         Near();
144     }
145 }
146
147 ///////////////////////// ArithmTestBase ///////////////////////////
148
149 PARAM_TEST_CASE(ArithmTestBase, MatDepth, Channels, bool)
150 {
151     int depth;
152     int cn;
153     bool use_roi;
154     cv::Scalar val;
155
156     // src mat
157     cv::Mat src1;
158     cv::Mat src2;
159     cv::Mat mask;
160     cv::Mat dst1;
161     cv::Mat dst2;
162
163     // src mat with roi
164     cv::Mat src1_roi;
165     cv::Mat src2_roi;
166     cv::Mat mask_roi;
167     cv::Mat dst1_roi;
168     cv::Mat dst2_roi;
169
170     // ocl dst mat for testing
171     cv::ocl::oclMat gsrc1_whole;
172     cv::ocl::oclMat gsrc2_whole;
173     cv::ocl::oclMat gdst1_whole;
174     cv::ocl::oclMat gdst2_whole;
175     cv::ocl::oclMat gmask_whole;
176
177     // ocl mat with roi
178     cv::ocl::oclMat gsrc1_roi;
179     cv::ocl::oclMat gsrc2_roi;
180     cv::ocl::oclMat gdst1_roi;
181     cv::ocl::oclMat gdst2_roi;
182     cv::ocl::oclMat gmask_roi;
183
184     virtual void SetUp()
185     {
186         depth = GET_PARAM(0);
187         cn = GET_PARAM(1);
188         use_roi = GET_PARAM(2);
189     }
190
191     void random_roi()
192     {
193         const int type = CV_MAKE_TYPE(depth, cn);
194
195         Size roiSize = randomSize(1, MAX_VALUE);
196         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
197         randomSubMat(src1, src1_roi, roiSize, srcBorder, type, 2, 11);
198
199         Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
200         randomSubMat(src2, src2_roi, roiSize, src2Border, type, -1540, 1740);
201
202         Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
203         randomSubMat(dst1, dst1_roi, roiSize, dst1Border, type, 5, 16);
204
205         Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
206         randomSubMat(dst2, dst2_roi, roiSize, dst2Border, type, 5, 16);
207
208         Border maskBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
209         randomSubMat(mask, mask_roi, roiSize, maskBorder, CV_8UC1, 0, 2);
210         cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
211
212
213         generateOclMat(gsrc1_whole, gsrc1_roi, src1, roiSize, srcBorder);
214         generateOclMat(gsrc2_whole, gsrc2_roi, src2, roiSize, src2Border);
215         generateOclMat(gdst1_whole, gdst1_roi, dst1, roiSize, dst1Border);
216         generateOclMat(gdst2_whole, gdst2_roi, dst2, roiSize, dst2Border);
217         generateOclMat(gmask_whole, gmask_roi, mask, roiSize, maskBorder);
218
219         val = cv::Scalar(rng.uniform(-100.0, 100.0), rng.uniform(-100.0, 100.0),
220                          rng.uniform(-100.0, 100.0), rng.uniform(-100.0, 100.0));
221     }
222
223     void Near(double threshold = 0.)
224     {
225         EXPECT_MAT_NEAR(dst1, Mat(gdst1_whole), threshold);
226         EXPECT_MAT_NEAR(dst1_roi, Mat(gdst1_roi), threshold);
227     }
228
229     void Near1(double threshold = 0.)
230     {
231         EXPECT_MAT_NEAR(dst2, Mat(gdst2_whole), threshold);
232         EXPECT_MAT_NEAR(dst2_roi, Mat(gdst2_roi), threshold);
233     }
234 };
235
236 //////////////////////////////// Exp /////////////////////////////////////////////////
237
238 typedef ArithmTestBase Exp;
239
240 OCL_TEST_P(Exp, Mat)
241 {
242     for (int j = 0; j < LOOP_TIMES; j++)
243     {
244         random_roi();
245
246         cv::exp(src1_roi, dst1_roi);
247         cv::ocl::exp(gsrc1_roi, gdst1_roi);
248
249         Near(2);
250     }
251 }
252
253 //////////////////////////////// Log /////////////////////////////////////////////////
254
255 typedef ArithmTestBase Log;
256
257 OCL_TEST_P(Log, Mat)
258 {
259     for (int j = 0; j < LOOP_TIMES; j++)
260     {
261         random_roi();
262
263         cv::log(src1_roi, dst1_roi);
264         cv::ocl::log(gsrc1_roi, gdst1_roi);
265         Near(1);
266     }
267 }
268
269 //////////////////////////////// Add /////////////////////////////////////////////////
270
271 typedef ArithmTestBase Add;
272
273 OCL_TEST_P(Add, Mat)
274 {
275     for (int j = 0; j < LOOP_TIMES; j++)
276     {
277         random_roi();
278
279         cv::add(src1_roi, src2_roi, dst1_roi);
280         cv::ocl::add(gsrc1_roi, gsrc2_roi, gdst1_roi);
281         Near(0);
282     }
283 }
284
285 OCL_TEST_P(Add, Mat_Mask)
286 {
287     for (int j = 0; j < LOOP_TIMES; j++)
288     {
289         random_roi();
290
291         cv::add(src1_roi, src2_roi, dst1_roi, mask_roi);
292         cv::ocl::add(gsrc1_roi, gsrc2_roi, gdst1_roi, gmask_roi);
293         Near(0);
294     }
295 }
296
297 OCL_TEST_P(Add, Scalar)
298 {
299     for (int j = 0; j < LOOP_TIMES; j++)
300     {
301         random_roi();
302
303         cv::add(src1_roi, val, dst1_roi);
304         cv::ocl::add(gsrc1_roi, val, gdst1_roi);
305         Near(1e-5);
306     }
307 }
308
309 OCL_TEST_P(Add, Scalar_Mask)
310 {
311     for (int j = 0; j < LOOP_TIMES; j++)
312     {
313         random_roi();
314
315         cv::add(src1_roi, val, dst1_roi, mask_roi);
316         cv::ocl::add(gsrc1_roi, val, gdst1_roi, gmask_roi);
317         Near(1e-5);
318     }
319 }
320
321 //////////////////////////////// Sub /////////////////////////////////////////////////
322
323 typedef ArithmTestBase Sub;
324
325 OCL_TEST_P(Sub, Mat)
326 {
327     for (int j = 0; j < LOOP_TIMES; j++)
328     {
329         random_roi();
330
331         cv::subtract(src1_roi, src2_roi, dst1_roi);
332         cv::ocl::subtract(gsrc1_roi, gsrc2_roi, gdst1_roi);
333
334         Near(0);
335     }
336 }
337
338 OCL_TEST_P(Sub, Mat_Mask)
339 {
340     for (int j = 0; j < LOOP_TIMES; j++)
341     {
342         random_roi();
343
344         cv::subtract(src1_roi, src2_roi, dst1_roi, mask_roi);
345         cv::ocl::subtract(gsrc1_roi, gsrc2_roi, gdst1_roi, gmask_roi);
346         Near(0);
347     }
348 }
349
350 OCL_TEST_P(Sub, Scalar)
351 {
352     for (int j = 0; j < LOOP_TIMES; j++)
353     {
354         random_roi();
355
356         cv::subtract(src1_roi, val, dst1_roi);
357         cv::ocl::subtract(gsrc1_roi, val, gdst1_roi);
358
359         Near(1e-5);
360     }
361 }
362
363 OCL_TEST_P(Sub, Scalar_Mask)
364 {
365     for (int j = 0; j < LOOP_TIMES; j++)
366     {
367         random_roi();
368
369         cv::subtract(src1_roi, val, dst1_roi, mask_roi);
370         cv::ocl::subtract(gsrc1_roi, val, gdst1_roi, gmask_roi);
371         Near(1e-5);
372     }
373 }
374
375 //////////////////////////////// Mul /////////////////////////////////////////////////
376
377 typedef ArithmTestBase Mul;
378
379 OCL_TEST_P(Mul, Mat)
380 {
381     for (int j = 0; j < LOOP_TIMES; j++)
382     {
383         random_roi();
384
385         cv::multiply(src1_roi, src2_roi, dst1_roi);
386         cv::ocl::multiply(gsrc1_roi, gsrc2_roi, gdst1_roi);
387         Near(0);
388     }
389 }
390
391 OCL_TEST_P(Mul, Scalar)
392 {
393     for (int j = 0; j < LOOP_TIMES; j++)
394     {
395         random_roi();
396
397         cv::multiply(Scalar::all(val[0]), src1_roi, dst1_roi);
398         cv::ocl::multiply(val[0], gsrc1_roi, gdst1_roi);
399
400         Near(gdst1_roi.depth() >= CV_32F ? 1e-3 : 1);
401     }
402 }
403
404 OCL_TEST_P(Mul, Mat_Scalar)
405 {
406     for (int j = 0; j < LOOP_TIMES; j++)
407     {
408         random_roi();
409
410         cv::multiply(src1_roi, src2_roi, dst1_roi, val[0]);
411         cv::ocl::multiply(gsrc1_roi, gsrc2_roi, gdst1_roi, val[0]);
412
413         Near(gdst1_roi.depth() >= CV_32F ? 1e-3 : 1);
414     }
415 }
416
417 //////////////////////////////// Div /////////////////////////////////////////////////
418
419 typedef ArithmTestBase Div;
420
421 OCL_TEST_P(Div, Mat)
422 {
423     for (int j = 0; j < LOOP_TIMES; j++)
424     {
425         random_roi();
426
427         cv::divide(src1_roi, src2_roi, dst1_roi);
428         cv::ocl::divide(gsrc1_roi, gsrc2_roi, gdst1_roi);
429         Near(1);
430     }
431 }
432
433 OCL_TEST_P(Div, Scalar)
434 {
435     for (int j = 0; j < LOOP_TIMES; j++)
436     {
437         random_roi();
438
439         cv::divide(val[0], src1_roi, dst1_roi);
440         cv::ocl::divide(val[0], gsrc1_roi, gdst1_roi);
441
442         Near(gdst1_roi.depth() >= CV_32F ? 1e-3 : 1);
443     }
444 }
445
446 OCL_TEST_P(Div, Mat_Scalar)
447 {
448     for (int j = 0; j < LOOP_TIMES; j++)
449     {
450         random_roi();
451
452         cv::divide(src1_roi, src2_roi, dst1_roi, val[0]);
453         cv::ocl::divide(gsrc1_roi, gsrc2_roi, gdst1_roi, val[0]);
454
455         Near(gdst1_roi.depth() >= CV_32F ? 4e-3 : 1);
456     }
457 }
458
459 //////////////////////////////// Absdiff /////////////////////////////////////////////////
460
461 typedef ArithmTestBase Min;
462
463 OCL_TEST_P(Min, Mat)
464 {
465     for (int j = 0; j < LOOP_TIMES; j++)
466     {
467         random_roi();
468
469         dst1_roi = cv::min(src1_roi, src2_roi);
470         cv::ocl::min(gsrc1_roi, gsrc2_roi, gdst1_roi);
471         Near(0);
472     }
473 }
474
475 typedef ArithmTestBase Max;
476
477 OCL_TEST_P(Max, Mat)
478 {
479     for (int j = 0; j < LOOP_TIMES; j++)
480     {
481         random_roi();
482
483         dst1_roi = cv::min(src1_roi, src2_roi);
484         cv::ocl::min(gsrc1_roi, gsrc2_roi, gdst1_roi);
485         Near(0);
486     }
487 }
488
489 //////////////////////////////// Abs /////////////////////////////////////////////////////
490
491 typedef ArithmTestBase Abs;
492
493 OCL_TEST_P(Abs, Abs)
494 {
495     for (int j = 0; j < LOOP_TIMES; j++)
496     {
497         random_roi();
498
499         dst1_roi = cv::abs(src1_roi);
500         cv::ocl::abs(gsrc1_roi, gdst1_roi);
501         Near(0);
502     }
503 }
504
505 //////////////////////////////// Absdiff /////////////////////////////////////////////////
506
507 typedef ArithmTestBase Absdiff;
508
509 OCL_TEST_P(Absdiff, Mat)
510 {
511     for (int j = 0; j < LOOP_TIMES; j++)
512     {
513         random_roi();
514
515         cv::absdiff(src1_roi, src2_roi, dst1_roi);
516         cv::ocl::absdiff(gsrc1_roi, gsrc2_roi, gdst1_roi);
517         Near(0);
518     }
519 }
520
521 OCL_TEST_P(Absdiff, Scalar)
522 {
523     for (int j = 0; j < LOOP_TIMES; j++)
524     {
525         random_roi();
526
527         cv::absdiff(src1_roi, val, dst1_roi);
528         cv::ocl::absdiff(gsrc1_roi, val, gdst1_roi);
529         Near(1e-5);
530     }
531 }
532
533 //////////////////////////////// CartToPolar /////////////////////////////////////////////////
534
535 typedef ArithmTestBase CartToPolar;
536
537 OCL_TEST_P(CartToPolar, angleInDegree)
538 {
539     for (int j = 0; j < LOOP_TIMES; j++)
540     {
541         random_roi();
542
543         cv::cartToPolar(src1_roi, src2_roi, dst1_roi, dst2_roi, true);
544         cv::ocl::cartToPolar(gsrc1_roi, gsrc2_roi, gdst1_roi, gdst2_roi, true);
545         Near(.5);
546         Near1(.5);
547     }
548 }
549
550 OCL_TEST_P(CartToPolar, angleInRadians)
551 {
552     for (int j = 0; j < LOOP_TIMES; j++)
553     {
554         random_roi();
555
556         cv::cartToPolar(src1_roi, src2_roi, dst1_roi, dst2_roi);
557         cv::ocl::cartToPolar(gsrc1_roi, gsrc2_roi, gdst1_roi, gdst2_roi);
558         Near(.5);
559         Near1(.5);
560     }
561 }
562
563 //////////////////////////////// PolarToCart /////////////////////////////////////////////////
564
565 typedef ArithmTestBase PolarToCart;
566
567 OCL_TEST_P(PolarToCart, angleInDegree)
568 {
569     for (int j = 0; j < LOOP_TIMES; j++)
570     {
571         random_roi();
572
573         cv::polarToCart(src1_roi, src2_roi, dst1_roi, dst2_roi, true);
574         cv::ocl::polarToCart(gsrc1_roi, gsrc2_roi, gdst1_roi, gdst2_roi, true);
575
576         Near(.5);
577         Near1(.5);
578     }
579 }
580
581 OCL_TEST_P(PolarToCart, angleInRadians)
582 {
583     for (int j = 0; j < LOOP_TIMES; j++)
584     {
585         random_roi();
586
587         cv::polarToCart(src1_roi, src2_roi, dst1_roi, dst2_roi);
588         cv::ocl::polarToCart(gsrc1_roi, gsrc2_roi, gdst1_roi, gdst2_roi);
589
590         Near(.5);
591         Near1(.5);
592     }
593 }
594
595 //////////////////////////////// Magnitude /////////////////////////////////////////////////
596
597 typedef ArithmTestBase Magnitude;
598
599 OCL_TEST_P(Magnitude, Mat)
600 {
601     for (int j = 0; j < LOOP_TIMES; j++)
602     {
603         random_roi();
604
605         cv::magnitude(src1_roi, src2_roi, dst1_roi);
606         cv::ocl::magnitude(gsrc1_roi, gsrc2_roi, gdst1_roi);
607         Near(depth == CV_64F ? 1e-5 : 1e-2);
608     }
609 }
610
611 //////////////////////////////// Transpose /////////////////////////////////////////////////
612
613 typedef ArithmTestBase Transpose;
614
615 OCL_TEST_P(Transpose, Mat)
616 {
617     for (int j = 0; j < LOOP_TIMES; j++)
618     {
619         random_roi();
620
621         cv::transpose(src1_roi, dst1_roi);
622         cv::ocl::transpose(gsrc1_roi, gdst1_roi);
623
624         Near(1e-5);
625     }
626 }
627
628 OCL_TEST_P(Transpose, SquareInplace)
629 {
630     const int type = CV_MAKE_TYPE(depth, cn);
631
632     for (int j = 0; j < LOOP_TIMES; j++)
633     {
634         Size roiSize = randomSize(1, MAX_VALUE);
635         roiSize.height = roiSize.width; // make it square
636
637         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
638         randomSubMat(src1, src1_roi, roiSize, srcBorder, type, 5, 16);
639
640         generateOclMat(gsrc1_whole, gsrc1_roi, src1, roiSize, srcBorder);
641
642         cv::transpose(src1_roi, src1_roi);
643         cv::ocl::transpose(gsrc1_roi, gsrc1_roi);
644
645         EXPECT_MAT_NEAR(src1, Mat(gsrc1_whole), 0.0);
646         EXPECT_MAT_NEAR(src1_roi, Mat(gsrc1_roi), 0.0);
647     }
648 }
649
650 //////////////////////////////// Flip /////////////////////////////////////////////////
651
652 typedef ArithmTestBase Flip;
653
654 OCL_TEST_P(Flip, X)
655 {
656     for (int j = 0; j < LOOP_TIMES; j++)
657     {
658         random_roi();
659
660         cv::flip(src1_roi, dst1_roi, 0);
661         cv::ocl::flip(gsrc1_roi, gdst1_roi, 0);
662         Near(1e-5);
663     }
664 }
665
666 OCL_TEST_P(Flip, Y)
667 {
668     for (int j = 0; j < LOOP_TIMES; j++)
669     {
670         random_roi();
671
672         cv::flip(src1_roi, dst1_roi, 1);
673         cv::ocl::flip(gsrc1_roi, gdst1_roi, 1);
674         Near(1e-5);
675     }
676 }
677
678 OCL_TEST_P(Flip, BOTH)
679 {
680     for (int j = 0; j < LOOP_TIMES; j++)
681     {
682         random_roi();
683
684         cv::flip(src1_roi, dst1_roi, -1);
685         cv::ocl::flip(gsrc1_roi, gdst1_roi, -1);
686         Near(1e-5);
687     }
688 }
689
690 //////////////////////////////// MinMax /////////////////////////////////////////////////
691
692 typedef ArithmTestBase MinMax;
693
694 OCL_TEST_P(MinMax, MAT)
695 {
696     for (int j = 0; j < LOOP_TIMES; j++)
697     {
698         random_roi();
699
700         double minVal, maxVal;
701
702         if (src1.depth() != CV_8S)
703             cv::minMaxIdx(src1_roi, &minVal, &maxVal, NULL, NULL);
704         else
705         {
706             minVal = std::numeric_limits<double>::max();
707             maxVal = -std::numeric_limits<double>::max();
708             for (int i = 0; i < src1_roi.rows; ++i)
709                 for (int j = 0; j < src1_roi.cols; ++j)
710                 {
711                     signed char val = src1_roi.at<signed char>(i, j);
712                     if (val < minVal) minVal = val;
713                     if (val > maxVal) maxVal = val;
714                 }
715         }
716
717         double minVal_, maxVal_;
718         cv::ocl::minMax(gsrc1_roi, &minVal_, &maxVal_);
719
720         EXPECT_DOUBLE_EQ(minVal_, minVal);
721         EXPECT_DOUBLE_EQ(maxVal_, maxVal);
722     }
723 }
724
725 OCL_TEST_P(MinMax, MASK)
726 {
727     for (int j = 0; j < LOOP_TIMES; j++)
728     {
729         random_roi();
730
731         double minVal, maxVal;
732         cv::Point minLoc, maxLoc;
733
734         if (src1.depth() != CV_8S)
735             cv::minMaxLoc(src1_roi, &minVal, &maxVal, &minLoc, &maxLoc, mask_roi);
736         else
737         {
738             minVal = std::numeric_limits<double>::max();
739             maxVal = -std::numeric_limits<double>::max();
740             for (int i = 0; i < src1_roi.rows; ++i)
741                 for (int j = 0; j < src1_roi.cols; ++j)
742                 {
743                     signed char val = src1_roi.at<signed char>(i, j);
744                     unsigned char m = mask_roi.at<unsigned char>(i, j);
745                     if (val < minVal && m) minVal = val;
746                     if (val > maxVal && m) maxVal = val;
747                 }
748         }
749
750         double minVal_, maxVal_;
751         cv::ocl::minMax(gsrc1_roi, &minVal_, &maxVal_, gmask_roi);
752
753         EXPECT_DOUBLE_EQ(minVal, minVal_);
754         EXPECT_DOUBLE_EQ(maxVal, maxVal_);
755     }
756 }
757
758 //////////////////////////////// MinMaxLoc /////////////////////////////////////////////////
759
760 typedef ArithmTestBase MinMaxLoc;
761
762 OCL_TEST_P(MinMaxLoc, MAT)
763 {
764     for (int j = 0; j < LOOP_TIMES; j++)
765     {
766         random_roi();
767
768         double minVal, maxVal;
769         cv::Point minLoc, maxLoc;
770         int depth = src1.depth();
771
772         if (depth != CV_8S)
773             cv::minMaxLoc(src1_roi, &minVal, &maxVal, &minLoc, &maxLoc);
774         else
775         {
776             minVal = std::numeric_limits<double>::max();
777             maxVal = -std::numeric_limits<double>::max();
778             for (int i = 0; i < src1_roi.rows; ++i)
779                 for (int j = 0; j < src1_roi.cols; ++j)
780                 {
781                     signed char val = src1_roi.at<signed char>(i, j);
782                     if (val < minVal)
783                     {
784                         minVal = val;
785                         minLoc.x = j;
786                         minLoc.y = i;
787                     }
788                     if (val > maxVal)
789                     {
790                         maxVal = val;
791                         maxLoc.x = j;
792                         maxLoc.y = i;
793                     }
794                 }
795         }
796
797         double minVal_, maxVal_;
798         cv::Point minLoc_, maxLoc_;
799         cv::ocl::minMaxLoc(gsrc1_roi, &minVal_, &maxVal_, &minLoc_, &maxLoc_, cv::ocl::oclMat());
800
801         double error0 = 0., error1 = 0., minlocVal = 0., minlocVal_ = 0., maxlocVal = 0., maxlocVal_ = 0.;
802         if (depth == 0)
803         {
804             minlocVal = src1_roi.at<unsigned char>(minLoc);
805             minlocVal_ = src1_roi.at<unsigned char>(minLoc_);
806             maxlocVal = src1_roi.at<unsigned char>(maxLoc);
807             maxlocVal_ = src1_roi.at<unsigned char>(maxLoc_);
808             error0 = ::abs(src1_roi.at<unsigned char>(minLoc_) - src1_roi.at<unsigned char>(minLoc));
809             error1 = ::abs(src1_roi.at<unsigned char>(maxLoc_) - src1_roi.at<unsigned char>(maxLoc));
810         }
811         if (depth == 1)
812         {
813             minlocVal = src1_roi.at<signed char>(minLoc);
814             minlocVal_ = src1_roi.at<signed char>(minLoc_);
815             maxlocVal = src1_roi.at<signed char>(maxLoc);
816             maxlocVal_ = src1_roi.at<signed char>(maxLoc_);
817             error0 = ::abs(src1_roi.at<signed char>(minLoc_) - src1_roi.at<signed char>(minLoc));
818             error1 = ::abs(src1_roi.at<signed char>(maxLoc_) - src1_roi.at<signed char>(maxLoc));
819         }
820         if (depth == 2)
821         {
822             minlocVal = src1_roi.at<unsigned short>(minLoc);
823             minlocVal_ = src1_roi.at<unsigned short>(minLoc_);
824             maxlocVal = src1_roi.at<unsigned short>(maxLoc);
825             maxlocVal_ = src1_roi.at<unsigned short>(maxLoc_);
826             error0 = ::abs(src1_roi.at<unsigned short>(minLoc_) - src1_roi.at<unsigned short>(minLoc));
827             error1 = ::abs(src1_roi.at<unsigned short>(maxLoc_) - src1_roi.at<unsigned short>(maxLoc));
828         }
829         if (depth == 3)
830         {
831             minlocVal = src1_roi.at<signed short>(minLoc);
832             minlocVal_ = src1_roi.at<signed short>(minLoc_);
833             maxlocVal = src1_roi.at<signed short>(maxLoc);
834             maxlocVal_ = src1_roi.at<signed short>(maxLoc_);
835             error0 = ::abs(src1_roi.at<signed short>(minLoc_) - src1_roi.at<signed short>(minLoc));
836             error1 = ::abs(src1_roi.at<signed short>(maxLoc_) - src1_roi.at<signed short>(maxLoc));
837         }
838         if (depth == 4)
839         {
840             minlocVal = src1_roi.at<int>(minLoc);
841             minlocVal_ = src1_roi.at<int>(minLoc_);
842             maxlocVal = src1_roi.at<int>(maxLoc);
843             maxlocVal_ = src1_roi.at<int>(maxLoc_);
844             error0 = ::abs(src1_roi.at<int>(minLoc_) - src1_roi.at<int>(minLoc));
845             error1 = ::abs(src1_roi.at<int>(maxLoc_) - src1_roi.at<int>(maxLoc));
846         }
847         if (depth == 5)
848         {
849             minlocVal = src1_roi.at<float>(minLoc);
850             minlocVal_ = src1_roi.at<float>(minLoc_);
851             maxlocVal = src1_roi.at<float>(maxLoc);
852             maxlocVal_ = src1_roi.at<float>(maxLoc_);
853             error0 = ::abs(src1_roi.at<float>(minLoc_) - src1_roi.at<float>(minLoc));
854             error1 = ::abs(src1_roi.at<float>(maxLoc_) - src1_roi.at<float>(maxLoc));
855         }
856         if (depth == 6)
857         {
858             minlocVal = src1_roi.at<double>(minLoc);
859             minlocVal_ = src1_roi.at<double>(minLoc_);
860             maxlocVal = src1_roi.at<double>(maxLoc);
861             maxlocVal_ = src1_roi.at<double>(maxLoc_);
862             error0 = ::abs(src1_roi.at<double>(minLoc_) - src1_roi.at<double>(minLoc));
863             error1 = ::abs(src1_roi.at<double>(maxLoc_) - src1_roi.at<double>(maxLoc));
864         }
865
866         EXPECT_DOUBLE_EQ(minVal_, minVal);
867         EXPECT_DOUBLE_EQ(maxVal_, maxVal);
868         EXPECT_DOUBLE_EQ(minlocVal_, minlocVal);
869         EXPECT_DOUBLE_EQ(maxlocVal_, maxlocVal);
870
871         EXPECT_DOUBLE_EQ(error0, 0.0);
872         EXPECT_DOUBLE_EQ(error1, 0.0);
873     }
874 }
875
876 OCL_TEST_P(MinMaxLoc, MASK)
877 {
878     for (int j = 0; j < LOOP_TIMES; j++)
879     {
880         random_roi();
881         double minVal, maxVal;
882         cv::Point minLoc, maxLoc;
883         int depth = src1.depth();
884         if (depth != CV_8S)
885             cv::minMaxLoc(src1_roi, &minVal, &maxVal, &minLoc, &maxLoc, mask_roi);
886         else
887         {
888             minVal = std::numeric_limits<double>::max();
889             maxVal = -std::numeric_limits<double>::max();
890             for (int i = 0; i < src1_roi.rows; ++i)
891                 for (int j = 0; j < src1_roi.cols; ++j)
892                 {
893                     signed char val = src1_roi.at<signed char>(i, j);
894                     unsigned char m = mask_roi.at<unsigned char>(i , j);
895                     if (val < minVal && m)
896                     {
897                         minVal = val;
898                         minLoc.x = j;
899                         minLoc.y = i;
900                     }
901                     if (val > maxVal && m)
902                     {
903                         maxVal = val;
904                         maxLoc.x = j;
905                         maxLoc.y = i;
906                     }
907                 }
908         }
909
910         double minVal_, maxVal_;
911         cv::Point minLoc_, maxLoc_;
912         cv::ocl::minMaxLoc(gsrc1_roi, &minVal_, &maxVal_, &minLoc_, &maxLoc_, gmask_roi);
913
914         double error0 = 0., error1 = 0., minlocVal = 0., minlocVal_ = 0., maxlocVal = 0., maxlocVal_ = 0.;
915         if (minLoc_.x == -1 || minLoc_.y == -1 || maxLoc_.x == -1 || maxLoc_.y == -1) continue;
916         if (depth == 0)
917         {
918             minlocVal = src1_roi.at<unsigned char>(minLoc);
919             minlocVal_ = src1_roi.at<unsigned char>(minLoc_);
920             maxlocVal = src1_roi.at<unsigned char>(maxLoc);
921             maxlocVal_ = src1_roi.at<unsigned char>(maxLoc_);
922             error0 = ::abs(src1_roi.at<unsigned char>(minLoc_) - src1_roi.at<unsigned char>(minLoc));
923             error1 = ::abs(src1_roi.at<unsigned char>(maxLoc_) - src1_roi.at<unsigned char>(maxLoc));
924         }
925         if (depth == 1)
926         {
927             minlocVal = src1_roi.at<signed char>(minLoc);
928             minlocVal_ = src1_roi.at<signed char>(minLoc_);
929             maxlocVal = src1_roi.at<signed char>(maxLoc);
930             maxlocVal_ = src1_roi.at<signed char>(maxLoc_);
931             error0 = ::abs(src1_roi.at<signed char>(minLoc_) - src1_roi.at<signed char>(minLoc));
932             error1 = ::abs(src1_roi.at<signed char>(maxLoc_) - src1_roi.at<signed char>(maxLoc));
933         }
934         if (depth == 2)
935         {
936             minlocVal = src1_roi.at<unsigned short>(minLoc);
937             minlocVal_ = src1_roi.at<unsigned short>(minLoc_);
938             maxlocVal = src1_roi.at<unsigned short>(maxLoc);
939             maxlocVal_ = src1_roi.at<unsigned short>(maxLoc_);
940             error0 = ::abs(src1_roi.at<unsigned short>(minLoc_) - src1_roi.at<unsigned short>(minLoc));
941             error1 = ::abs(src1_roi.at<unsigned short>(maxLoc_) - src1_roi.at<unsigned short>(maxLoc));
942         }
943         if (depth == 3)
944         {
945             minlocVal = src1_roi.at<signed short>(minLoc);
946             minlocVal_ = src1_roi.at<signed short>(minLoc_);
947             maxlocVal = src1_roi.at<signed short>(maxLoc);
948             maxlocVal_ = src1_roi.at<signed short>(maxLoc_);
949             error0 = ::abs(src1_roi.at<signed short>(minLoc_) - src1_roi.at<signed short>(minLoc));
950             error1 = ::abs(src1_roi.at<signed short>(maxLoc_) - src1_roi.at<signed short>(maxLoc));
951         }
952         if (depth == 4)
953         {
954             minlocVal = src1_roi.at<int>(minLoc);
955             minlocVal_ = src1_roi.at<int>(minLoc_);
956             maxlocVal = src1_roi.at<int>(maxLoc);
957             maxlocVal_ = src1_roi.at<int>(maxLoc_);
958             error0 = ::abs(src1_roi.at<int>(minLoc_) - src1_roi.at<int>(minLoc));
959             error1 = ::abs(src1_roi.at<int>(maxLoc_) - src1_roi.at<int>(maxLoc));
960         }
961         if (depth == 5)
962         {
963             minlocVal = src1_roi.at<float>(minLoc);
964             minlocVal_ = src1_roi.at<float>(minLoc_);
965             maxlocVal = src1_roi.at<float>(maxLoc);
966             maxlocVal_ = src1_roi.at<float>(maxLoc_);
967             error0 = ::abs(src1_roi.at<float>(minLoc_) - src1_roi.at<float>(minLoc));
968             error1 = ::abs(src1_roi.at<float>(maxLoc_) - src1_roi.at<float>(maxLoc));
969         }
970         if (depth == 6)
971         {
972             minlocVal = src1_roi.at<double>(minLoc);
973             minlocVal_ = src1_roi.at<double>(minLoc_);
974             maxlocVal = src1_roi.at<double>(maxLoc);
975             maxlocVal_ = src1_roi.at<double>(maxLoc_);
976             error0 = ::abs(src1_roi.at<double>(minLoc_) - src1_roi.at<double>(minLoc));
977             error1 = ::abs(src1_roi.at<double>(maxLoc_) - src1_roi.at<double>(maxLoc));
978         }
979
980         EXPECT_DOUBLE_EQ(minVal_, minVal);
981         EXPECT_DOUBLE_EQ(maxVal_, maxVal);
982         EXPECT_DOUBLE_EQ(minlocVal_, minlocVal);
983         EXPECT_DOUBLE_EQ(maxlocVal_, maxlocVal);
984
985         EXPECT_DOUBLE_EQ(error0, 0.0);
986         EXPECT_DOUBLE_EQ(error1, 0.0);
987     }
988 }
989
990 //////////////////////////////// Sum /////////////////////////////////////////////////
991
992 typedef ArithmTestBase Sum;
993
994 OCL_TEST_P(Sum, MAT)
995 {
996     for (int j = 0; j < LOOP_TIMES; j++)
997     {
998         random_roi();
999
1000         Scalar cpures = cv::sum(src1_roi);
1001         Scalar gpures = cv::ocl::sum(gsrc1_roi);
1002
1003         // check results
1004         EXPECT_NEAR(cpures[0], gpures[0], 0.1);
1005         EXPECT_NEAR(cpures[1], gpures[1], 0.1);
1006         EXPECT_NEAR(cpures[2], gpures[2], 0.1);
1007         EXPECT_NEAR(cpures[3], gpures[3], 0.1);
1008     }
1009 }
1010
1011 typedef ArithmTestBase SqrSum;
1012
1013 template <typename T, typename WT>
1014 static Scalar sqrSum(const Mat & src)
1015 {
1016     Scalar sum = Scalar::all(0);
1017     int cn = src.channels();
1018     WT data[4] = { 0, 0, 0, 0 };
1019
1020     int cols = src.cols * cn;
1021     for (int y = 0; y < src.rows; ++y)
1022     {
1023         const T * const sdata = src.ptr<T>(y);
1024         for (int x = 0; x < cols; )
1025             for (int i = 0; i < cn; ++i, ++x)
1026             {
1027                 WT t = static_cast<WT>(sdata[x]);
1028                 data[i] += t * t;
1029             }
1030     }
1031
1032     for (int i = 0; i < cn; ++i)
1033         sum[i] = static_cast<double>(data[i]);
1034
1035     return sum;
1036 }
1037
1038 typedef Scalar (*sumFunc)(const Mat &);
1039
1040 OCL_TEST_P(SqrSum, MAT)
1041 {
1042     for (int j = 0; j < LOOP_TIMES; j++)
1043     {
1044         random_roi();
1045
1046         static sumFunc funcs[] = { sqrSum<uchar, int>,
1047                                  sqrSum<char, int>,
1048                                  sqrSum<ushort, int>,
1049                                  sqrSum<short, int>,
1050                                  sqrSum<int, int>,
1051                                  sqrSum<float, double>,
1052                                  sqrSum<double, double>,
1053                                  0 };
1054
1055         sumFunc func = funcs[src1_roi.depth()];
1056         CV_Assert(func != 0);
1057
1058         Scalar cpures = func(src1_roi);
1059         Scalar gpures = cv::ocl::sqrSum(gsrc1_roi);
1060
1061         // check results
1062         EXPECT_NEAR(cpures[0], gpures[0], 1.0);
1063         EXPECT_NEAR(cpures[1], gpures[1], 1.0);
1064         EXPECT_NEAR(cpures[2], gpures[2], 1.0);
1065         EXPECT_NEAR(cpures[3], gpures[3], 1.0);
1066     }
1067 }
1068
1069 typedef ArithmTestBase AbsSum;
1070
1071 template <typename T, typename WT>
1072 static Scalar absSum(const Mat & src)
1073 {
1074     Scalar sum = Scalar::all(0);
1075     int cn = src.channels();
1076     WT data[4] = { 0, 0, 0, 0 };
1077
1078     int cols = src.cols * cn;
1079     for (int y = 0; y < src.rows; ++y)
1080     {
1081         const T * const sdata = src.ptr<T>(y);
1082         for (int x = 0; x < cols; )
1083             for (int i = 0; i < cn; ++i, ++x)
1084             {
1085                 WT t = static_cast<WT>(sdata[x]);
1086                 data[i] += t >= 0 ? t : -t;
1087             }
1088     }
1089
1090     for (int i = 0; i < cn; ++i)
1091         sum[i] = static_cast<double>(data[i]);
1092
1093     return sum;
1094 }
1095
1096 OCL_TEST_P(AbsSum, MAT)
1097 {
1098     for (int j = 0; j < LOOP_TIMES; j++)
1099     {
1100         random_roi();
1101
1102         static sumFunc funcs[] = { absSum<uchar, int>,
1103                                  absSum<char, int>,
1104                                  absSum<ushort, int>,
1105                                  absSum<short, int>,
1106                                  absSum<int, int>,
1107                                  absSum<float, double>,
1108                                  absSum<double, double>,
1109                                  0 };
1110
1111         sumFunc func = funcs[src1_roi.depth()];
1112         CV_Assert(func != 0);
1113
1114         Scalar cpures = func(src1_roi);
1115         Scalar gpures = cv::ocl::absSum(gsrc1_roi);
1116
1117         // check results
1118         EXPECT_NEAR(cpures[0], gpures[0], 0.1);
1119         EXPECT_NEAR(cpures[1], gpures[1], 0.1);
1120         EXPECT_NEAR(cpures[2], gpures[2], 0.1);
1121         EXPECT_NEAR(cpures[3], gpures[3], 0.1);
1122     }
1123 }
1124
1125 //////////////////////////////// CountNonZero /////////////////////////////////////////////////
1126
1127 typedef ArithmTestBase CountNonZero;
1128
1129 OCL_TEST_P(CountNonZero, MAT)
1130 {
1131     for (int j = 0; j < LOOP_TIMES; j++)
1132     {
1133         random_roi();
1134         int cpures = cv::countNonZero(src1_roi);
1135         int gpures = cv::ocl::countNonZero(gsrc1_roi);
1136
1137         EXPECT_DOUBLE_EQ((double)cpures, (double)gpures);
1138     }
1139 }
1140
1141 //////////////////////////////// Phase /////////////////////////////////////////////////
1142
1143 typedef ArithmTestBase Phase;
1144
1145 OCL_TEST_P(Phase, angleInDegrees)
1146 {
1147     for (int j = 0; j < LOOP_TIMES; j++)
1148     {
1149         random_roi();
1150         cv::phase(src1_roi, src2_roi, dst1_roi, true);
1151         cv::ocl::phase(gsrc1_roi, gsrc2_roi, gdst1_roi, true);
1152
1153         Near(1e-2);
1154     }
1155 }
1156
1157 OCL_TEST_P(Phase, angleInRadians)
1158 {
1159     for (int j = 0; j < LOOP_TIMES; j++)
1160     {
1161         random_roi();
1162         cv::phase(src1_roi, src2_roi, dst1_roi);
1163         cv::ocl::phase(gsrc1_roi, gsrc2_roi, gdst1_roi);
1164
1165         Near(1e-2);
1166     }
1167 }
1168
1169 //////////////////////////////// Bitwise_and /////////////////////////////////////////////////
1170
1171 typedef ArithmTestBase Bitwise_and;
1172
1173 OCL_TEST_P(Bitwise_and, Mat)
1174 {
1175     for (int j = 0; j < LOOP_TIMES; j++)
1176     {
1177         random_roi();
1178
1179         cv::bitwise_and(src1_roi, src2_roi, dst1_roi);
1180         cv::ocl::bitwise_and(gsrc1_roi, gsrc2_roi, gdst1_roi);
1181         Near(0);
1182     }
1183 }
1184
1185 OCL_TEST_P(Bitwise_and, Mat_Mask)
1186 {
1187     for (int j = 0; j < LOOP_TIMES; j++)
1188     {
1189         random_roi();
1190
1191         cv::bitwise_and(src1_roi, src2_roi, dst1_roi, mask_roi);
1192         cv::ocl::bitwise_and(gsrc1_roi, gsrc2_roi, gdst1_roi, gmask_roi);
1193         Near(0);
1194     }
1195 }
1196
1197 OCL_TEST_P(Bitwise_and, Scalar)
1198 {
1199     for (int j = 0; j < LOOP_TIMES; j++)
1200     {
1201         random_roi();
1202
1203         cv::bitwise_and(src1_roi, val, dst1_roi);
1204         cv::ocl::bitwise_and(gsrc1_roi, val, gdst1_roi);
1205         Near(1e-5);
1206     }
1207 }
1208
1209 OCL_TEST_P(Bitwise_and, Scalar_Mask)
1210 {
1211     for (int j = 0; j < LOOP_TIMES; j++)
1212     {
1213         random_roi();
1214
1215         cv::bitwise_and(src1_roi, val, dst1_roi, mask_roi);
1216         cv::ocl::bitwise_and(gsrc1_roi, val, gdst1_roi, gmask_roi);
1217         Near(1e-5);
1218     }
1219 }
1220
1221 //////////////////////////////// Bitwise_or /////////////////////////////////////////////////
1222
1223 typedef ArithmTestBase Bitwise_or;
1224
1225 OCL_TEST_P(Bitwise_or, Mat)
1226 {
1227     for (int j = 0; j < LOOP_TIMES; j++)
1228     {
1229         random_roi();
1230
1231         cv::bitwise_or(src1_roi, src2_roi, dst1_roi);
1232         cv::ocl::bitwise_or(gsrc1_roi, gsrc2_roi, gdst1_roi);
1233         Near(0);
1234     }
1235 }
1236
1237 OCL_TEST_P(Bitwise_or, Mat_Mask)
1238 {
1239     for (int j = 0; j < LOOP_TIMES; j++)
1240     {
1241         random_roi();
1242
1243         cv::bitwise_or(src1_roi, src2_roi, dst1_roi, mask_roi);
1244         cv::ocl::bitwise_or(gsrc1_roi, gsrc2_roi, gdst1_roi, gmask_roi);
1245         Near(0);
1246     }
1247 }
1248
1249 OCL_TEST_P(Bitwise_or, Scalar)
1250 {
1251     for (int j = 0; j < LOOP_TIMES; j++)
1252     {
1253         random_roi();
1254
1255         cv::bitwise_or(src1_roi, val, dst1_roi);
1256         cv::ocl::bitwise_or(gsrc1_roi, val, gdst1_roi);
1257         Near(1e-5);
1258     }
1259 }
1260
1261 OCL_TEST_P(Bitwise_or, Scalar_Mask)
1262 {
1263     for (int j = 0; j < LOOP_TIMES; j++)
1264     {
1265         random_roi();
1266
1267         cv::bitwise_or(src1_roi, val, dst1_roi, mask_roi);
1268         cv::ocl::bitwise_or(gsrc1_roi, val, gdst1_roi, gmask_roi);
1269         Near(1e-5);
1270     }
1271 }
1272
1273 //////////////////////////////// Bitwise_xor /////////////////////////////////////////////////
1274
1275 typedef ArithmTestBase Bitwise_xor;
1276
1277 OCL_TEST_P(Bitwise_xor, Mat)
1278 {
1279     for (int j = 0; j < LOOP_TIMES; j++)
1280     {
1281         random_roi();
1282
1283         cv::bitwise_xor(src1_roi, src2_roi, dst1_roi);
1284         cv::ocl::bitwise_xor(gsrc1_roi, gsrc2_roi, gdst1_roi);
1285         Near(0);
1286     }
1287 }
1288
1289 OCL_TEST_P(Bitwise_xor, Mat_Mask)
1290 {
1291     for (int j = 0; j < LOOP_TIMES; j++)
1292     {
1293         random_roi();
1294
1295         cv::bitwise_xor(src1_roi, src2_roi, dst1_roi, mask_roi);
1296         cv::ocl::bitwise_xor(gsrc1_roi, gsrc2_roi, gdst1_roi, gmask_roi);
1297         Near(0);
1298     }
1299 }
1300
1301 OCL_TEST_P(Bitwise_xor, Scalar)
1302 {
1303     for (int j = 0; j < LOOP_TIMES; j++)
1304     {
1305         random_roi();
1306
1307         cv::bitwise_xor(src1_roi, val, dst1_roi);
1308         cv::ocl::bitwise_xor(gsrc1_roi, val, gdst1_roi);
1309         Near(1e-5);
1310     }
1311 }
1312
1313 OCL_TEST_P(Bitwise_xor, Scalar_Mask)
1314 {
1315     for (int j = 0; j < LOOP_TIMES; j++)
1316     {
1317         random_roi();
1318
1319         cv::bitwise_xor(src1_roi, val, dst1_roi, mask_roi);
1320         cv::ocl::bitwise_xor(gsrc1_roi, val, gdst1_roi, gmask_roi);
1321         Near(1e-5);
1322     }
1323 }
1324
1325 //////////////////////////////// Bitwise_not /////////////////////////////////////////////////
1326
1327 typedef ArithmTestBase Bitwise_not;
1328
1329 OCL_TEST_P(Bitwise_not, Mat)
1330 {
1331     for (int j = 0; j < LOOP_TIMES; j++)
1332     {
1333         random_roi();
1334
1335         cv::bitwise_not(src1_roi, dst1_roi);
1336         cv::ocl::bitwise_not(gsrc1_roi, gdst1_roi);
1337         Near(0);
1338     }
1339 }
1340
1341 //////////////////////////////// Compare /////////////////////////////////////////////////
1342
1343 typedef ArithmTestBase Compare;
1344
1345 OCL_TEST_P(Compare, Mat)
1346 {
1347     int cmp_codes[] = { CMP_EQ, CMP_GT, CMP_GE, CMP_LT, CMP_LE, CMP_NE };
1348     int cmp_num = sizeof(cmp_codes) / sizeof(int);
1349
1350     for (int i = 0; i < cmp_num; ++i)
1351         for (int j = 0; j < LOOP_TIMES; j++)
1352         {
1353             random_roi();
1354
1355             cv::compare(src1_roi, src2_roi, dst1_roi, cmp_codes[i]);
1356             cv::ocl::compare(gsrc1_roi, gsrc2_roi, gdst1_roi, cmp_codes[i]);
1357
1358             Near(0);
1359         }
1360 }
1361
1362 //////////////////////////////// Pow /////////////////////////////////////////////////
1363
1364 typedef ArithmTestBase Pow;
1365
1366 OCL_TEST_P(Pow, Mat)
1367 {
1368     for (int j = 0; j < LOOP_TIMES; j++)
1369     {
1370         random_roi();
1371         double p = 4.5;
1372         cv::pow(src1_roi, p, dst1_roi);
1373         cv::ocl::pow(gsrc1_roi, p, gdst1_roi);
1374         Near(1);
1375     }
1376 }
1377
1378 //////////////////////////////// AddWeighted /////////////////////////////////////////////////
1379
1380 typedef ArithmTestBase AddWeighted;
1381
1382 OCL_TEST_P(AddWeighted, Mat)
1383 {
1384     for (int j = 0; j < LOOP_TIMES; j++)
1385     {
1386         random_roi();
1387
1388         const double alpha = 2.0, beta = 1.0, gama = 3.0;
1389
1390         cv::addWeighted(src1_roi, alpha, src2_roi, beta, gama, dst1_roi);
1391         cv::ocl::addWeighted(gsrc1_roi, alpha, gsrc2_roi, beta, gama, gdst1_roi);
1392
1393         Near(3e-4);
1394     }
1395 }
1396
1397 //////////////////////////////// setIdentity /////////////////////////////////////////////////
1398
1399 typedef ArithmTestBase SetIdentity;
1400
1401 OCL_TEST_P(SetIdentity, Mat)
1402 {
1403     for (int j = 0; j < LOOP_TIMES; j++)
1404     {
1405         random_roi();
1406
1407         cv::setIdentity(dst1_roi, val);
1408         cv::ocl::setIdentity(gdst1_roi, val);
1409
1410         Near(0);
1411     }
1412 }
1413
1414 //////////////////////////////// meanStdDev /////////////////////////////////////////////////
1415
1416 typedef ArithmTestBase MeanStdDev;
1417
1418 OCL_TEST_P(MeanStdDev, Mat)
1419 {
1420     for (int j = 0; j < LOOP_TIMES; j++)
1421     {
1422         random_roi();
1423
1424         Scalar cpu_mean, cpu_stddev;
1425         Scalar gpu_mean, gpu_stddev;
1426
1427         cv::meanStdDev(src1_roi, cpu_mean, cpu_stddev);
1428         cv::ocl::meanStdDev(gsrc1_roi, gpu_mean, gpu_stddev);
1429
1430         for (int i = 0; i < 4; ++i)
1431         {
1432             EXPECT_NEAR(cpu_mean[i], gpu_mean[i], 0.1);
1433             EXPECT_NEAR(cpu_stddev[i], gpu_stddev[i], 0.1);
1434         }
1435     }
1436 }
1437
1438 //////////////////////////////// Norm /////////////////////////////////////////////////
1439
1440 typedef ArithmTestBase Norm;
1441
1442 OCL_TEST_P(Norm, NORM_INF)
1443 {
1444     for (int relative = 0; relative < 2; ++relative)
1445         for (int j = 0; j < LOOP_TIMES; j++)
1446         {
1447             random_roi();
1448
1449             int type = NORM_INF;
1450             if (relative == 1)
1451                 type |= NORM_RELATIVE;
1452
1453             const double cpuRes = cv::norm(src1_roi, src2_roi, type);
1454             const double gpuRes = cv::ocl::norm(gsrc1_roi, gsrc2_roi, type);
1455
1456             EXPECT_NEAR(cpuRes, gpuRes, 0.1);
1457         }
1458 }
1459
1460 OCL_TEST_P(Norm, NORM_L1)
1461 {
1462     for (int relative = 0; relative < 2; ++relative)
1463         for (int j = 0; j < LOOP_TIMES; j++)
1464         {
1465             random_roi();
1466
1467             int type = NORM_L1;
1468             if (relative == 1)
1469                 type |= NORM_RELATIVE;
1470
1471             const double cpuRes = cv::norm(src1_roi, src2_roi, type);
1472             const double gpuRes = cv::ocl::norm(gsrc1_roi, gsrc2_roi, type);
1473
1474             EXPECT_PRED3(relativeError, cpuRes, gpuRes, 1e-6);
1475         }
1476 }
1477
1478 OCL_TEST_P(Norm, NORM_L2)
1479 {
1480     for (int relative = 0; relative < 2; ++relative)
1481         for (int j = 0; j < LOOP_TIMES; j++)
1482         {
1483             random_roi();
1484
1485             int type = NORM_L2;
1486             if (relative == 1)
1487                 type |= NORM_RELATIVE;
1488
1489             const double cpuRes = cv::norm(src1_roi, src2_roi, type);
1490             const double gpuRes = cv::ocl::norm(gsrc1_roi, gsrc2_roi, type);
1491
1492             EXPECT_PRED3(relativeError, cpuRes, gpuRes, 1e-6);
1493         }
1494 }
1495
1496 //////////////////////////////////////// Instantiation /////////////////////////////////////////
1497
1498 INSTANTIATE_TEST_CASE_P(Arithm, Lut, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool(), Bool()));
1499 INSTANTIATE_TEST_CASE_P(Arithm, Exp, Combine(testing::Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1500 INSTANTIATE_TEST_CASE_P(Arithm, Log, Combine(testing::Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1501 INSTANTIATE_TEST_CASE_P(Arithm, Add, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1502 INSTANTIATE_TEST_CASE_P(Arithm, Sub, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1503 INSTANTIATE_TEST_CASE_P(Arithm, Mul, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1504 INSTANTIATE_TEST_CASE_P(Arithm, Div, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1505 INSTANTIATE_TEST_CASE_P(Arithm, Min, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1506 INSTANTIATE_TEST_CASE_P(Arithm, Max, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1507 INSTANTIATE_TEST_CASE_P(Arithm, Abs, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1508 INSTANTIATE_TEST_CASE_P(Arithm, Absdiff, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1509 INSTANTIATE_TEST_CASE_P(Arithm, CartToPolar, Combine(Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1510 INSTANTIATE_TEST_CASE_P(Arithm, PolarToCart, Combine(Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1511 INSTANTIATE_TEST_CASE_P(Arithm, Magnitude, Combine(Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1512 INSTANTIATE_TEST_CASE_P(Arithm, Transpose, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1513 INSTANTIATE_TEST_CASE_P(Arithm, Flip, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1514 INSTANTIATE_TEST_CASE_P(Arithm, MinMax, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(Channels(1)), Bool()));
1515 INSTANTIATE_TEST_CASE_P(Arithm, MinMaxLoc, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(Channels(1)), Bool()));
1516 INSTANTIATE_TEST_CASE_P(Arithm, Sum, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1517 INSTANTIATE_TEST_CASE_P(Arithm, SqrSum, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1518 INSTANTIATE_TEST_CASE_P(Arithm, AbsSum, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1519 INSTANTIATE_TEST_CASE_P(Arithm, CountNonZero, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(Channels(1)), Bool()));
1520 INSTANTIATE_TEST_CASE_P(Arithm, Phase, Combine(Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1521 INSTANTIATE_TEST_CASE_P(Arithm, Bitwise_and, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1522 INSTANTIATE_TEST_CASE_P(Arithm, Bitwise_or, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1523 INSTANTIATE_TEST_CASE_P(Arithm, Bitwise_xor, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1524 INSTANTIATE_TEST_CASE_P(Arithm, Bitwise_not, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1525 INSTANTIATE_TEST_CASE_P(Arithm, Compare, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(Channels(1)), Bool()));
1526 INSTANTIATE_TEST_CASE_P(Arithm, Pow, Combine(Values(CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1527 INSTANTIATE_TEST_CASE_P(Arithm, AddWeighted, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1528 INSTANTIATE_TEST_CASE_P(Arithm, SetIdentity, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1529 INSTANTIATE_TEST_CASE_P(Arithm, MeanStdDev, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1530 INSTANTIATE_TEST_CASE_P(Arithm, Norm, Combine(Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(1, 2, 3, 4), Bool()));
1531
1532 #endif // HAVE_OPENCL