Merge pull request #1704 from SpecLad:merge-2.4
[profile/ivi/opencv.git] / modules / ocl / perf / perf_imgproc.cpp
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) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other materials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 #include "perf_precomp.hpp"
47
48 using namespace perf;
49 using std::tr1::tuple;
50 using std::tr1::get;
51
52 ///////////// equalizeHist ////////////////////////
53
54 typedef TestBaseWithParam<Size> equalizeHistFixture;
55
56 PERF_TEST_P(equalizeHistFixture, equalizeHist, OCL_TYPICAL_MAT_SIZES)
57 {
58     const Size srcSize = GetParam();
59
60     Mat src(srcSize, CV_8UC1), dst(srcSize, CV_8UC1);
61     declare.in(src, WARMUP_RNG).out(dst);
62
63     if (RUN_OCL_IMPL)
64     {
65         ocl::oclMat oclSrc(src), oclDst(srcSize, src.type());
66
67         OCL_TEST_CYCLE() cv::ocl::equalizeHist(oclSrc, oclDst);
68
69         oclDst.download(dst);
70
71         SANITY_CHECK(dst, 1 + DBL_EPSILON);
72     }
73     else if (RUN_PLAIN_IMPL)
74     {
75         TEST_CYCLE() cv::equalizeHist(src, dst);
76
77         SANITY_CHECK(dst, 1 + DBL_EPSILON);
78     }
79     else
80         OCL_PERF_ELSE
81 }
82
83 /////////// CopyMakeBorder //////////////////////
84
85 typedef Size_MatType CopyMakeBorderFixture;
86
87 PERF_TEST_P(CopyMakeBorderFixture, CopyMakeBorder,
88             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
89                                OCL_PERF_ENUM(CV_8UC1, CV_8UC4)))
90 {
91     const Size_MatType_t params = GetParam();
92     const Size srcSize = get<0>(params);
93     const int type = get<1>(params), borderType = BORDER_CONSTANT;
94
95     Mat src(srcSize, type), dst;
96     const Size dstSize = srcSize + Size(12, 12);
97     dst.create(dstSize, type);
98     declare.in(src, WARMUP_RNG).out(dst);
99
100     if (RUN_OCL_IMPL)
101     {
102         ocl::oclMat oclSrc(src), oclDst(dstSize, type);
103
104         OCL_TEST_CYCLE() cv::ocl::copyMakeBorder(oclSrc, oclDst, 7, 5, 5, 7, borderType, cv::Scalar(1.0));
105
106         oclDst.download(dst);
107
108         SANITY_CHECK(dst);
109     }
110     else if (RUN_PLAIN_IMPL)
111     {
112         TEST_CYCLE() cv::copyMakeBorder(src, dst, 7, 5, 5, 7, borderType, cv::Scalar(1.0));
113
114         SANITY_CHECK(dst);
115     }
116     else
117         OCL_PERF_ELSE
118 }
119
120 ///////////// cornerMinEigenVal ////////////////////////
121
122 typedef Size_MatType cornerMinEigenValFixture;
123
124 PERF_TEST_P(cornerMinEigenValFixture, cornerMinEigenVal,
125             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
126                                OCL_PERF_ENUM(CV_8UC1, CV_32FC1)))
127 {
128     const Size_MatType_t params = GetParam();
129     const Size srcSize = get<0>(params);
130     const int type = get<1>(params), borderType = BORDER_REFLECT;
131     const int blockSize = 7, apertureSize = 1 + 2 * 3;
132
133     Mat src(srcSize, type), dst(srcSize, CV_32FC1);
134     declare.in(src, WARMUP_RNG).out(dst)
135             .time(srcSize == OCL_SIZE_4000 ? 20 : srcSize == OCL_SIZE_2000 ? 5 : 3);
136
137     const int depth = CV_MAT_DEPTH(type);
138     const ERROR_TYPE errorType = depth == CV_8U ? ERROR_ABSOLUTE : ERROR_RELATIVE;
139
140     if (RUN_OCL_IMPL)
141     {
142         ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
143
144         OCL_TEST_CYCLE() cv::ocl::cornerMinEigenVal(oclSrc, oclDst, blockSize, apertureSize, borderType);
145
146         oclDst.download(dst);
147
148         SANITY_CHECK(dst, 1e-6, errorType);
149     }
150     else if (RUN_PLAIN_IMPL)
151     {
152         TEST_CYCLE() cv::cornerMinEigenVal(src, dst, blockSize, apertureSize, borderType);
153
154         SANITY_CHECK(dst, 1e-6, errorType);
155     }
156     else
157         OCL_PERF_ELSE
158 }
159
160 ///////////// cornerHarris ////////////////////////
161
162 typedef Size_MatType cornerHarrisFixture;
163
164 PERF_TEST_P(cornerHarrisFixture, cornerHarris,
165             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
166                                OCL_PERF_ENUM(CV_8UC1, CV_32FC1)))
167 {
168     const Size_MatType_t params = GetParam();
169     const Size srcSize = get<0>(params);
170     const int type = get<1>(params), borderType = BORDER_REFLECT;
171
172     Mat src(srcSize, type), dst(srcSize, CV_32FC1);
173     randu(src, 0, 1);
174     declare.in(src).out(dst)
175             .time(srcSize == OCL_SIZE_4000 ? 20 : srcSize == OCL_SIZE_2000 ? 5 : 3);
176
177     if (RUN_OCL_IMPL)
178     {
179         ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
180
181         OCL_TEST_CYCLE() cv::ocl::cornerHarris(oclSrc, oclDst, 5, 7, 0.1, borderType);
182
183         oclDst.download(dst);
184
185         SANITY_CHECK(dst, 3e-5);
186     }
187     else if (RUN_PLAIN_IMPL)
188     {
189         TEST_CYCLE() cv::cornerHarris(src, dst, 5, 7, 0.1, borderType);
190
191         SANITY_CHECK(dst, 3e-5);
192     }
193     else
194         OCL_PERF_ELSE
195 }
196
197 ///////////// integral ////////////////////////
198
199 typedef TestBaseWithParam<Size> integralFixture;
200
201 PERF_TEST_P(integralFixture, integral, OCL_TYPICAL_MAT_SIZES)
202 {
203     const Size srcSize = GetParam();
204
205     Mat src(srcSize, CV_8UC1), dst;
206     declare.in(src, WARMUP_RNG);
207
208     if (RUN_OCL_IMPL)
209     {
210         ocl::oclMat oclSrc(src), oclDst;
211
212         OCL_TEST_CYCLE() cv::ocl::integral(oclSrc, oclDst);
213
214         oclDst.download(dst);
215
216         SANITY_CHECK(dst);
217     }
218     else if (RUN_PLAIN_IMPL)
219     {
220         TEST_CYCLE() cv::integral(src, dst);
221
222         SANITY_CHECK(dst);
223     }
224     else
225         OCL_PERF_ELSE
226 }
227
228 ///////////// WarpAffine ////////////////////////
229
230 typedef Size_MatType WarpAffineFixture;
231
232 PERF_TEST_P(WarpAffineFixture, WarpAffine,
233             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
234                                OCL_PERF_ENUM(CV_8UC1, CV_8UC4)))
235 {
236     static const double coeffs[2][3] =
237     {
238         { cos(CV_PI / 6), -sin(CV_PI / 6), 100.0 },
239         { sin(CV_PI / 6), cos(CV_PI / 6), -100.0 }
240     };
241     Mat M(2, 3, CV_64F, (void *)coeffs);
242     const int interpolation = INTER_NEAREST;
243
244     const Size_MatType_t params = GetParam();
245     const Size srcSize = get<0>(params);
246     const int type = get<1>(params);
247
248     Mat src(srcSize, type), dst(srcSize, type);
249     declare.in(src, WARMUP_RNG).out(dst);
250
251     if (RUN_OCL_IMPL)
252     {
253         ocl::oclMat oclSrc(src), oclDst(srcSize, type);
254
255         OCL_TEST_CYCLE() cv::ocl::warpAffine(oclSrc, oclDst, M, srcSize, interpolation);
256
257         oclDst.download(dst);
258
259         SANITY_CHECK(dst);
260     }
261     else if (RUN_PLAIN_IMPL)
262     {
263         TEST_CYCLE() cv::warpAffine(src, dst, M, srcSize, interpolation);
264
265         SANITY_CHECK(dst);
266     }
267     else
268         OCL_PERF_ELSE
269 }
270
271 ///////////// WarpPerspective ////////////////////////
272
273 typedef Size_MatType WarpPerspectiveFixture;
274
275 PERF_TEST_P(WarpPerspectiveFixture, WarpPerspective,
276             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
277                                OCL_PERF_ENUM(CV_8UC1, CV_8UC4)))
278 {
279     static const double coeffs[3][3] =
280     {
281         {cos(CV_PI / 6), -sin(CV_PI / 6), 100.0},
282         {sin(CV_PI / 6), cos(CV_PI / 6), -100.0},
283         {0.0, 0.0, 1.0}
284     };
285     Mat M(3, 3, CV_64F, (void *)coeffs);
286     const int interpolation = INTER_LINEAR;
287
288     const Size_MatType_t params = GetParam();
289     const Size srcSize = get<0>(params);
290     const int type = get<1>(params);
291
292     Mat src(srcSize, type), dst(srcSize, type);
293     declare.in(src, WARMUP_RNG).out(dst)
294             .time(srcSize == OCL_SIZE_4000 ? 18 : srcSize == OCL_SIZE_2000 ? 5 : 2);
295
296     if (RUN_OCL_IMPL)
297     {
298         ocl::oclMat oclSrc(src), oclDst(srcSize, type);
299
300         OCL_TEST_CYCLE() cv::ocl::warpPerspective(oclSrc, oclDst, M, srcSize, interpolation);
301
302         oclDst.download(dst);
303
304         SANITY_CHECK(dst);
305     }
306     else if (RUN_PLAIN_IMPL)
307     {
308         TEST_CYCLE() cv::warpPerspective(src, dst, M, srcSize, interpolation);
309
310         SANITY_CHECK(dst);
311     }
312     else
313         OCL_PERF_ELSE
314 }
315
316 ///////////// resize ////////////////////////
317
318 CV_ENUM(resizeInterType, INTER_NEAREST, INTER_LINEAR)
319
320 typedef tuple<Size, MatType, resizeInterType, double> resizeParams;
321 typedef TestBaseWithParam<resizeParams> resizeFixture;
322
323 PERF_TEST_P(resizeFixture, resize,
324             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
325                                OCL_PERF_ENUM(CV_8UC1, CV_8UC4),
326                                resizeInterType::all(),
327                                ::testing::Values(0.5, 2.0)))
328 {
329     const resizeParams params = GetParam();
330     const Size srcSize = get<0>(params);
331     const int type = get<1>(params), interType = get<2>(params);
332     double scale = get<3>(params);
333
334     Mat src(srcSize, type), dst;
335     const Size dstSize(cvRound(srcSize.width * scale), cvRound(srcSize.height * scale));
336     dst.create(dstSize, type);
337     declare.in(src, WARMUP_RNG).out(dst);
338     if (interType == INTER_LINEAR && type == CV_8UC4 && OCL_SIZE_4000 == srcSize)
339         declare.time(11);
340
341     if (RUN_OCL_IMPL)
342     {
343         ocl::oclMat oclSrc(src), oclDst(dstSize, type);
344
345         OCL_TEST_CYCLE() cv::ocl::resize(oclSrc, oclDst, Size(), scale, scale, interType);
346
347         oclDst.download(dst);
348
349         SANITY_CHECK(dst, 1 + DBL_EPSILON);
350     }
351     else if (RUN_PLAIN_IMPL)
352     {
353         TEST_CYCLE() cv::resize(src, dst, Size(), scale, scale, interType);
354
355         SANITY_CHECK(dst, 1 + DBL_EPSILON);
356     }
357     else
358         OCL_PERF_ELSE
359 }
360
361 ///////////// threshold////////////////////////
362
363 CV_ENUM(ThreshType, THRESH_BINARY, THRESH_TRUNC)
364
365 typedef tuple<Size, ThreshType> ThreshParams;
366 typedef TestBaseWithParam<ThreshParams> ThreshFixture;
367
368 PERF_TEST_P(ThreshFixture, threshold,
369             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
370                                ThreshType::all()))
371 {
372     const ThreshParams params = GetParam();
373     const Size srcSize = get<0>(params);
374     const int threshType = get<1>(params);
375
376     Mat src(srcSize, CV_8U), dst(srcSize, CV_8U);
377     randu(src, 0, 100);
378     declare.in(src).out(dst);
379
380     if (RUN_OCL_IMPL)
381     {
382         ocl::oclMat oclSrc(src), oclDst(srcSize, CV_8U);
383
384         OCL_TEST_CYCLE() cv::ocl::threshold(oclSrc, oclDst, 50.0, 0.0, threshType);
385
386         oclDst.download(dst);
387
388         SANITY_CHECK(dst);
389     }
390     else if (RUN_PLAIN_IMPL)
391     {
392         TEST_CYCLE() cv::threshold(src, dst, 50.0, 0.0, threshType);
393
394         SANITY_CHECK(dst);
395     }
396     else
397         OCL_PERF_ELSE
398 }
399
400 ///////////// meanShiftFiltering////////////////////////
401
402 typedef struct _COOR
403 {
404     short x;
405     short y;
406 } COOR;
407
408 static COOR do_meanShift(int x0, int y0, uchar *sptr, uchar *dptr, int sstep, cv::Size size, int sp, int sr, int maxIter, float eps, int *tab)
409 {
410
411     int isr2 = sr * sr;
412     int c0, c1, c2, c3;
413     int iter;
414     uchar *ptr = NULL;
415     uchar *pstart = NULL;
416     int revx = 0, revy = 0;
417     c0 = sptr[0];
418     c1 = sptr[1];
419     c2 = sptr[2];
420     c3 = sptr[3];
421     // iterate meanshift procedure
422     for(iter = 0; iter < maxIter; iter++ )
423     {
424         int count = 0;
425         int s0 = 0, s1 = 0, s2 = 0, sx = 0, sy = 0;
426
427         //mean shift: process pixels in window (p-sigmaSp)x(p+sigmaSp)
428         int minx = x0 - sp;
429         int miny = y0 - sp;
430         int maxx = x0 + sp;
431         int maxy = y0 + sp;
432
433         //deal with the image boundary
434         if(minx < 0) minx = 0;
435         if(miny < 0) miny = 0;
436         if(maxx >= size.width) maxx = size.width - 1;
437         if(maxy >= size.height) maxy = size.height - 1;
438         if(iter == 0)
439         {
440             pstart = sptr;
441         }
442         else
443         {
444             pstart = pstart + revy * sstep + (revx << 2); //point to the new position
445         }
446         ptr = pstart;
447         ptr = ptr + (miny - y0) * sstep + ((minx - x0) << 2); //point to the start in the row
448
449         for( int y = miny; y <= maxy; y++, ptr += sstep - ((maxx - minx + 1) << 2))
450         {
451             int rowCount = 0;
452             int x = minx;
453 #if CV_ENABLE_UNROLLED
454             for( ; x + 4 <= maxx; x += 4, ptr += 16)
455             {
456                 int t0, t1, t2;
457                 t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
458                 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
459                 {
460                     s0 += t0;
461                     s1 += t1;
462                     s2 += t2;
463                     sx += x;
464                     rowCount++;
465                 }
466                 t0 = ptr[4], t1 = ptr[5], t2 = ptr[6];
467                 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
468                 {
469                     s0 += t0;
470                     s1 += t1;
471                     s2 += t2;
472                     sx += x + 1;
473                     rowCount++;
474                 }
475                 t0 = ptr[8], t1 = ptr[9], t2 = ptr[10];
476                 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
477                 {
478                     s0 += t0;
479                     s1 += t1;
480                     s2 += t2;
481                     sx += x + 2;
482                     rowCount++;
483                 }
484                 t0 = ptr[12], t1 = ptr[13], t2 = ptr[14];
485                 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
486                 {
487                     s0 += t0;
488                     s1 += t1;
489                     s2 += t2;
490                     sx += x + 3;
491                     rowCount++;
492                 }
493             }
494 #endif
495             for(; x <= maxx; x++, ptr += 4)
496             {
497                 int t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
498                 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
499                 {
500                     s0 += t0;
501                     s1 += t1;
502                     s2 += t2;
503                     sx += x;
504                     rowCount++;
505                 }
506             }
507             if(rowCount == 0)
508                 continue;
509             count += rowCount;
510             sy += y * rowCount;
511         }
512
513         if( count == 0 )
514             break;
515
516         int x1 = sx / count;
517         int y1 = sy / count;
518         s0 = s0 / count;
519         s1 = s1 / count;
520         s2 = s2 / count;
521
522         bool stopFlag = (x0 == x1 && y0 == y1) || (abs(x1 - x0) + abs(y1 - y0) +
523             tab[s0 - c0 + 255] + tab[s1 - c1 + 255] + tab[s2 - c2 + 255] <= eps);
524
525         //revise the pointer corresponding to the new (y0,x0)
526         revx = x1 - x0;
527         revy = y1 - y0;
528
529         x0 = x1;
530         y0 = y1;
531         c0 = s0;
532         c1 = s1;
533         c2 = s2;
534
535         if( stopFlag )
536             break;
537     } //for iter
538
539     dptr[0] = (uchar)c0;
540     dptr[1] = (uchar)c1;
541     dptr[2] = (uchar)c2;
542     dptr[3] = (uchar)c3;
543
544     COOR coor;
545     coor.x = static_cast<short>(x0);
546     coor.y = static_cast<short>(y0);
547     return coor;
548 }
549
550 static void meanShiftFiltering_(const Mat &src_roi, Mat &dst_roi, int sp, int sr, cv::TermCriteria crit)
551 {
552     if( src_roi.empty() )
553         CV_Error( Error::StsBadArg, "The input image is empty" );
554
555     if( src_roi.depth() != CV_8U || src_roi.channels() != 4 )
556         CV_Error( Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
557
558     dst_roi.create(src_roi.size(), src_roi.type());
559
560     CV_Assert( (src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) );
561     CV_Assert( !(dst_roi.step & 0x3) );
562
563     if( !(crit.type & cv::TermCriteria::MAX_ITER) )
564         crit.maxCount = 5;
565     int maxIter = std::min(std::max(crit.maxCount, 1), 100);
566     float eps;
567     if( !(crit.type & cv::TermCriteria::EPS) )
568         eps = 1.f;
569     eps = (float)std::max(crit.epsilon, 0.0);
570
571     int tab[512];
572     for(int i = 0; i < 512; i++)
573         tab[i] = (i - 255) * (i - 255);
574     uchar *sptr = src_roi.data;
575     uchar *dptr = dst_roi.data;
576     int sstep = (int)src_roi.step;
577     int dstep = (int)dst_roi.step;
578     cv::Size size = src_roi.size();
579
580     for(int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
581         dptr += dstep - (size.width << 2))
582     {
583         for(int j = 0; j < size.width; j++, sptr += 4, dptr += 4)
584         {
585             do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
586         }
587     }
588 }
589
590 typedef TestBaseWithParam<Size> meanShiftFilteringFixture;
591
592 PERF_TEST_P(meanShiftFilteringFixture, meanShiftFiltering,
593             OCL_TYPICAL_MAT_SIZES)
594 {
595     const Size srcSize = GetParam();
596     const int sp = 5, sr = 6;
597     cv::TermCriteria crit(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 5, 1);
598
599     Mat src(srcSize, CV_8UC4), dst(srcSize, CV_8UC4);
600     declare.in(src, WARMUP_RNG).out(dst)
601             .time(srcSize == OCL_SIZE_4000 ?
602                       56 : srcSize == OCL_SIZE_2000 ? 15 : 3.8);
603
604     if (RUN_PLAIN_IMPL)
605     {
606         TEST_CYCLE() meanShiftFiltering_(src, dst, sp, sr, crit);
607
608         SANITY_CHECK(dst);
609     }
610     else if (RUN_OCL_IMPL)
611     {
612         ocl::oclMat oclSrc(src), oclDst(srcSize, CV_8UC4);
613
614         OCL_TEST_CYCLE() ocl::meanShiftFiltering(oclSrc, oclDst, sp, sr, crit);
615
616         oclDst.download(dst);
617
618         SANITY_CHECK(dst);
619     }
620     else
621         OCL_PERF_ELSE
622 }
623
624 static void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi, int sp, int sr, cv::TermCriteria crit)
625 {
626     if (src_roi.empty())
627     {
628         CV_Error(Error::StsBadArg, "The input image is empty");
629     }
630     if (src_roi.depth() != CV_8U || src_roi.channels() != 4)
631     {
632         CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
633     }
634
635     dst_roi.create(src_roi.size(), src_roi.type());
636     dstCoor_roi.create(src_roi.size(), CV_16SC2);
637
638     CV_Assert((src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) &&
639               (src_roi.cols == dstCoor_roi.cols) && (src_roi.rows == dstCoor_roi.rows));
640     CV_Assert(!(dstCoor_roi.step & 0x3));
641
642     if (!(crit.type & cv::TermCriteria::MAX_ITER))
643     {
644         crit.maxCount = 5;
645     }
646
647     int maxIter = std::min(std::max(crit.maxCount, 1), 100);
648     float eps;
649
650     if (!(crit.type & cv::TermCriteria::EPS))
651     {
652         eps = 1.f;
653     }
654
655     eps = (float)std::max(crit.epsilon, 0.0);
656
657     int tab[512];
658
659     for (int i = 0; i < 512; i++)
660     {
661         tab[i] = (i - 255) * (i - 255);
662     }
663
664     uchar *sptr = src_roi.data;
665     uchar *dptr = dst_roi.data;
666     short *dCoorptr = (short *)dstCoor_roi.data;
667     int sstep = (int)src_roi.step;
668     int dstep = (int)dst_roi.step;
669     int dCoorstep = (int)dstCoor_roi.step >> 1;
670     cv::Size size = src_roi.size();
671
672     for (int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
673             dptr += dstep - (size.width << 2), dCoorptr += dCoorstep - (size.width << 1))
674     {
675         for (int j = 0; j < size.width; j++, sptr += 4, dptr += 4, dCoorptr += 2)
676         {
677             *((COOR *)dCoorptr) = do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
678         }
679     }
680
681 }
682
683 typedef TestBaseWithParam<Size> meanShiftProcFixture;
684
685 PERF_TEST_P(meanShiftProcFixture, meanShiftProc,
686             OCL_TYPICAL_MAT_SIZES)
687 {
688     const Size srcSize = GetParam();
689     TermCriteria crit(TermCriteria::COUNT + TermCriteria::EPS, 5, 1);
690
691     Mat src(srcSize, CV_8UC4), dst1(srcSize, CV_8UC4),
692             dst2(srcSize, CV_16SC2);
693     declare.in(src, WARMUP_RNG).out(dst1, dst2)
694             .time(srcSize == OCL_SIZE_4000 ?
695                       56 : srcSize == OCL_SIZE_2000 ? 15 : 3.8);;
696
697     if (RUN_PLAIN_IMPL)
698     {
699         TEST_CYCLE() meanShiftProc_(src, dst1, dst2, 5, 6, crit);
700
701         SANITY_CHECK(dst1);
702         SANITY_CHECK(dst2);
703     }
704     else if (RUN_OCL_IMPL)
705     {
706         ocl::oclMat oclSrc(src), oclDst1(srcSize, CV_8UC4),
707                 oclDst2(srcSize, CV_16SC2);
708
709         OCL_TEST_CYCLE() ocl::meanShiftProc(oclSrc, oclDst1, oclDst2, 5, 6, crit);
710
711         oclDst1.download(dst1);
712         oclDst2.download(dst2);
713
714         SANITY_CHECK(dst1);
715         SANITY_CHECK(dst2);
716     }
717     else
718         OCL_PERF_ELSE
719 }
720
721 ///////////// remap////////////////////////
722
723 CV_ENUM(RemapInterType, INTER_NEAREST, INTER_LINEAR)
724
725 typedef tuple<Size, MatType, RemapInterType> remapParams;
726 typedef TestBaseWithParam<remapParams> remapFixture;
727
728 PERF_TEST_P(remapFixture, remap,
729             ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
730                                OCL_PERF_ENUM(CV_8UC1, CV_8UC4),
731                                RemapInterType::all()))
732 {
733     const remapParams params = GetParam();
734     const Size srcSize = get<0>(params);
735     const int type = get<1>(params), interpolation = get<2>(params);
736
737     Mat src(srcSize, type), dst(srcSize, type);
738     declare.in(src, WARMUP_RNG).out(dst);
739
740     if (srcSize == OCL_SIZE_4000 && interpolation == INTER_LINEAR)
741         declare.time(9);
742
743     Mat xmap, ymap;
744     xmap.create(srcSize, CV_32FC1);
745     ymap.create(srcSize, CV_32FC1);
746
747     for (int i = 0; i < srcSize.height; ++i)
748     {
749         float * const xmap_row = xmap.ptr<float>(i);
750         float * const ymap_row = ymap.ptr<float>(i);
751
752         for (int j = 0; j < srcSize.width; ++j)
753         {
754             xmap_row[j] = (j - srcSize.width * 0.5f) * 0.75f + srcSize.width * 0.5f;
755             ymap_row[j] = (i - srcSize.height * 0.5f) * 0.75f + srcSize.height * 0.5f;
756         }
757     }
758
759     const int borderMode = BORDER_CONSTANT;
760
761     if (RUN_OCL_IMPL)
762     {
763         ocl::oclMat oclSrc(src), oclDst(srcSize, type);
764         ocl::oclMat oclXMap(xmap), oclYMap(ymap);
765
766         OCL_TEST_CYCLE() cv::ocl::remap(oclSrc, oclDst, oclXMap, oclYMap, interpolation, borderMode);
767
768         oclDst.download(dst);
769
770         SANITY_CHECK(dst, 1 + DBL_EPSILON);
771     }
772     else if (RUN_PLAIN_IMPL)
773     {
774         TEST_CYCLE() cv::remap(src, dst, xmap, ymap, interpolation, borderMode);
775
776         SANITY_CHECK(dst, 1 + DBL_EPSILON);
777     }
778     else
779         OCL_PERF_ELSE
780 }
781
782 ///////////// CLAHE ////////////////////////
783
784 typedef TestBaseWithParam<Size> CLAHEFixture;
785
786 PERF_TEST_P(CLAHEFixture, CLAHE, OCL_TYPICAL_MAT_SIZES)
787 {
788     const Size srcSize = GetParam();
789     const string impl = getSelectedImpl();
790
791     Mat src(srcSize, CV_8UC1), dst;
792     const double clipLimit = 40.0;
793     declare.in(src, WARMUP_RNG);
794
795     if (srcSize == OCL_SIZE_4000)
796         declare.time(11);
797
798     if (RUN_OCL_IMPL)
799     {
800         ocl::oclMat oclSrc(src), oclDst;
801         cv::Ptr<cv::CLAHE> oclClahe = cv::ocl::createCLAHE(clipLimit);
802
803         OCL_TEST_CYCLE() oclClahe->apply(oclSrc, oclDst);
804
805         oclDst.download(dst);
806
807         SANITY_CHECK(dst);
808     }
809     else if (RUN_PLAIN_IMPL)
810     {
811         cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(clipLimit);
812         TEST_CYCLE() clahe->apply(src, dst);
813
814         SANITY_CHECK(dst);
815     }
816     else
817         OCL_PERF_ELSE
818 }
819
820 ///////////// columnSum////////////////////////
821
822 typedef TestBaseWithParam<Size> columnSumFixture;
823
824 static void columnSumPerfTest(const Mat & src, Mat & dst)
825 {
826     for (int j = 0; j < src.cols; j++)
827         dst.at<float>(0, j) = src.at<float>(0, j);
828
829     for (int i = 1; i < src.rows; ++i)
830         for (int j = 0; j < src.cols; ++j)
831             dst.at<float>(i, j) = dst.at<float>(i - 1 , j) + src.at<float>(i , j);
832 }
833
834 PERF_TEST_P(columnSumFixture, columnSum, OCL_TYPICAL_MAT_SIZES)
835 {
836     const Size srcSize = GetParam();
837
838     Mat src(srcSize, CV_32FC1), dst(srcSize, CV_32FC1);
839     declare.in(src, WARMUP_RNG).out(dst);
840
841     if (srcSize == OCL_SIZE_4000)
842         declare.time(5);
843
844     if (RUN_OCL_IMPL)
845     {
846         ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
847
848         OCL_TEST_CYCLE() cv::ocl::columnSum(oclSrc, oclDst);
849
850         oclDst.download(dst);
851
852         SANITY_CHECK(dst);
853     }
854     else if (RUN_PLAIN_IMPL)
855     {
856         TEST_CYCLE() columnSumPerfTest(src, dst);
857
858         SANITY_CHECK(dst);
859     }
860     else
861         OCL_PERF_ELSE
862 }