1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Fangfang Bai, fangfang@multicorewareinc.com
19 // Jin Ma, jin@multicorewareinc.com
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
24 // * Redistribution's of source code must retain the above copyright notice,
25 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #include "perf_precomp.hpp"
49 using std::tr1::tuple;
52 ///////////// equalizeHist ////////////////////////
54 typedef TestBaseWithParam<Size> equalizeHistFixture;
56 PERF_TEST_P(equalizeHistFixture, equalizeHist, OCL_TYPICAL_MAT_SIZES)
58 const Size srcSize = GetParam();
59 const double eps = 1 + DBL_EPSILON;
61 Mat src(srcSize, CV_8UC1), dst(srcSize, CV_8UC1);
62 declare.in(src, WARMUP_RNG).out(dst);
66 ocl::oclMat oclSrc(src), oclDst(srcSize, src.type());
68 OCL_TEST_CYCLE() cv::ocl::equalizeHist(oclSrc, oclDst);
72 SANITY_CHECK(dst, eps);
74 else if (RUN_PLAIN_IMPL)
76 TEST_CYCLE() cv::equalizeHist(src, dst);
78 SANITY_CHECK(dst, eps);
84 /////////// CopyMakeBorder //////////////////////
86 CV_ENUM(Border, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,
87 BORDER_WRAP, BORDER_REFLECT_101)
89 typedef tuple<Size, MatType, Border> CopyMakeBorderParamType;
90 typedef TestBaseWithParam<CopyMakeBorderParamType> CopyMakeBorderFixture;
92 PERF_TEST_P(CopyMakeBorderFixture, CopyMakeBorder,
93 ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
94 OCL_PERF_ENUM(CV_8UC1, CV_8UC4),
97 const CopyMakeBorderParamType params = GetParam();
98 const Size srcSize = get<0>(params);
99 const int type = get<1>(params), borderType = get<2>(params);
101 Mat src(srcSize, type), dst;
102 const Size dstSize = srcSize + Size(12, 12);
103 dst.create(dstSize, type);
104 declare.in(src, WARMUP_RNG).out(dst);
108 ocl::oclMat oclSrc(src), oclDst(dstSize, type);
110 OCL_TEST_CYCLE() cv::ocl::copyMakeBorder(oclSrc, oclDst, 7, 5, 5, 7, borderType, cv::Scalar(1.0));
112 oclDst.download(dst);
116 else if (RUN_PLAIN_IMPL)
118 TEST_CYCLE() cv::copyMakeBorder(src, dst, 7, 5, 5, 7, borderType, cv::Scalar(1.0));
126 ///////////// cornerMinEigenVal ////////////////////////
128 typedef Size_MatType cornerMinEigenValFixture;
130 PERF_TEST_P(cornerMinEigenValFixture, cornerMinEigenVal,
131 ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
132 OCL_PERF_ENUM(CV_8UC1, CV_32FC1)))
134 const Size_MatType_t params = GetParam();
135 const Size srcSize = get<0>(params);
136 const int type = get<1>(params), borderType = BORDER_REFLECT;
137 const int blockSize = 7, apertureSize = 1 + 2 * 3;
139 Mat src(srcSize, type), dst(srcSize, CV_32FC1);
140 declare.in(src, WARMUP_RNG).out(dst)
141 .time(srcSize == OCL_SIZE_4000 ? 20 : srcSize == OCL_SIZE_2000 ? 5 : 3);
143 const int depth = CV_MAT_DEPTH(type);
144 const ERROR_TYPE errorType = depth == CV_8U ? ERROR_ABSOLUTE : ERROR_RELATIVE;
148 ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
150 OCL_TEST_CYCLE() cv::ocl::cornerMinEigenVal(oclSrc, oclDst, blockSize, apertureSize, borderType);
152 oclDst.download(dst);
154 SANITY_CHECK(dst, 1e-6, errorType);
156 else if (RUN_PLAIN_IMPL)
158 TEST_CYCLE() cv::cornerMinEigenVal(src, dst, blockSize, apertureSize, borderType);
160 SANITY_CHECK(dst, 1e-6, errorType);
166 ///////////// cornerHarris ////////////////////////
168 typedef Size_MatType cornerHarrisFixture;
170 PERF_TEST_P(cornerHarrisFixture, cornerHarris,
171 ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
172 OCL_PERF_ENUM(CV_8UC1, CV_32FC1)))
174 const Size_MatType_t params = GetParam();
175 const Size srcSize = get<0>(params);
176 const int type = get<1>(params), borderType = BORDER_REFLECT;
178 Mat src(srcSize, type), dst(srcSize, CV_32FC1);
180 declare.in(src).out(dst)
181 .time(srcSize == OCL_SIZE_4000 ? 20 : srcSize == OCL_SIZE_2000 ? 5 : 3);
185 ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
187 OCL_TEST_CYCLE() cv::ocl::cornerHarris(oclSrc, oclDst, 5, 7, 0.1, borderType);
189 oclDst.download(dst);
191 SANITY_CHECK(dst, 3e-5);
193 else if (RUN_PLAIN_IMPL)
195 TEST_CYCLE() cv::cornerHarris(src, dst, 5, 7, 0.1, borderType);
197 SANITY_CHECK(dst, 3e-5);
203 ///////////// integral ////////////////////////
205 typedef TestBaseWithParam<Size> integralFixture;
207 PERF_TEST_P(integralFixture, integral, OCL_TYPICAL_MAT_SIZES)
209 const Size srcSize = GetParam();
211 Mat src(srcSize, CV_8UC1), dst;
212 declare.in(src, WARMUP_RNG);
216 ocl::oclMat oclSrc(src), oclDst;
218 OCL_TEST_CYCLE() cv::ocl::integral(oclSrc, oclDst);
220 oclDst.download(dst);
224 else if (RUN_PLAIN_IMPL)
226 TEST_CYCLE() cv::integral(src, dst);
234 ///////////// threshold////////////////////////
236 CV_ENUM(ThreshType, THRESH_BINARY, THRESH_TOZERO_INV)
238 typedef tuple<Size, MatType, ThreshType> ThreshParams;
239 typedef TestBaseWithParam<ThreshParams> ThreshFixture;
241 PERF_TEST_P(ThreshFixture, threshold,
242 ::testing::Combine(OCL_TYPICAL_MAT_SIZES,
243 OCL_PERF_ENUM(CV_8UC1, CV_8UC4, CV_16SC1, CV_16SC4, CV_32FC1),
246 const ThreshParams params = GetParam();
247 const Size srcSize = get<0>(params);
248 const int srcType = get<1>(params);
249 const int threshType = get<2>(params);
250 const double maxValue = 220.0, threshold = 50;
252 Mat src(srcSize, srcType), dst(srcSize, srcType);
254 declare.in(src).out(dst);
258 ocl::oclMat oclSrc(src), oclDst(srcSize, CV_8U);
260 OCL_TEST_CYCLE() cv::ocl::threshold(oclSrc, oclDst, threshold, maxValue, threshType);
262 oclDst.download(dst);
266 else if (RUN_PLAIN_IMPL)
268 TEST_CYCLE() cv::threshold(src, dst, threshold, maxValue, threshType);
276 ///////////// meanShiftFiltering////////////////////////
284 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)
291 uchar *pstart = NULL;
292 int revx = 0, revy = 0;
297 // iterate meanshift procedure
298 for(iter = 0; iter < maxIter; iter++ )
301 int s0 = 0, s1 = 0, s2 = 0, sx = 0, sy = 0;
303 //mean shift: process pixels in window (p-sigmaSp)x(p+sigmaSp)
309 //deal with the image boundary
310 if(minx < 0) minx = 0;
311 if(miny < 0) miny = 0;
312 if(maxx >= size.width) maxx = size.width - 1;
313 if(maxy >= size.height) maxy = size.height - 1;
320 pstart = pstart + revy * sstep + (revx << 2); //point to the new position
323 ptr = ptr + (miny - y0) * sstep + ((minx - x0) << 2); //point to the start in the row
325 for( int y = miny; y <= maxy; y++, ptr += sstep - ((maxx - minx + 1) << 2))
329 #if CV_ENABLE_UNROLLED
330 for( ; x + 4 <= maxx; x += 4, ptr += 16)
333 t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
334 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
342 t0 = ptr[4], t1 = ptr[5], t2 = ptr[6];
343 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
351 t0 = ptr[8], t1 = ptr[9], t2 = ptr[10];
352 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
360 t0 = ptr[12], t1 = ptr[13], t2 = ptr[14];
361 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
371 for(; x <= maxx; x++, ptr += 4)
373 int t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
374 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
398 bool stopFlag = (x0 == x1 && y0 == y1) || (abs(x1 - x0) + abs(y1 - y0) +
399 tab[s0 - c0 + 255] + tab[s1 - c1 + 255] + tab[s2 - c2 + 255] <= eps);
401 //revise the pointer corresponding to the new (y0,x0)
421 coor.x = static_cast<short>(x0);
422 coor.y = static_cast<short>(y0);
426 static void meanShiftFiltering_(const Mat &src_roi, Mat &dst_roi, int sp, int sr, cv::TermCriteria crit)
428 if( src_roi.empty() )
429 CV_Error( Error::StsBadArg, "The input image is empty" );
431 if( src_roi.depth() != CV_8U || src_roi.channels() != 4 )
432 CV_Error( Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
434 dst_roi.create(src_roi.size(), src_roi.type());
436 CV_Assert( (src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) );
437 CV_Assert( !(dst_roi.step & 0x3) );
439 if( !(crit.type & cv::TermCriteria::MAX_ITER) )
441 int maxIter = std::min(std::max(crit.maxCount, 1), 100);
443 if( !(crit.type & cv::TermCriteria::EPS) )
445 eps = (float)std::max(crit.epsilon, 0.0);
448 for(int i = 0; i < 512; i++)
449 tab[i] = (i - 255) * (i - 255);
450 uchar *sptr = src_roi.data;
451 uchar *dptr = dst_roi.data;
452 int sstep = (int)src_roi.step;
453 int dstep = (int)dst_roi.step;
454 cv::Size size = src_roi.size();
456 for(int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
457 dptr += dstep - (size.width << 2))
459 for(int j = 0; j < size.width; j++, sptr += 4, dptr += 4)
461 do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
466 typedef TestBaseWithParam<Size> meanShiftFilteringFixture;
468 PERF_TEST_P(meanShiftFilteringFixture, meanShiftFiltering,
469 OCL_TYPICAL_MAT_SIZES)
471 const Size srcSize = GetParam();
472 const int sp = 5, sr = 6;
473 cv::TermCriteria crit(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 5, 1);
475 Mat src(srcSize, CV_8UC4), dst(srcSize, CV_8UC4);
476 declare.in(src, WARMUP_RNG).out(dst)
477 .time(srcSize == OCL_SIZE_4000 ?
478 56 : srcSize == OCL_SIZE_2000 ? 15 : 3.8);
482 TEST_CYCLE() meanShiftFiltering_(src, dst, sp, sr, crit);
486 else if (RUN_OCL_IMPL)
488 ocl::oclMat oclSrc(src), oclDst(srcSize, CV_8UC4);
490 OCL_TEST_CYCLE() ocl::meanShiftFiltering(oclSrc, oclDst, sp, sr, crit);
492 oclDst.download(dst);
500 static void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi, int sp, int sr, cv::TermCriteria crit)
504 CV_Error(Error::StsBadArg, "The input image is empty");
506 if (src_roi.depth() != CV_8U || src_roi.channels() != 4)
508 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
511 dst_roi.create(src_roi.size(), src_roi.type());
512 dstCoor_roi.create(src_roi.size(), CV_16SC2);
514 CV_Assert((src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) &&
515 (src_roi.cols == dstCoor_roi.cols) && (src_roi.rows == dstCoor_roi.rows));
516 CV_Assert(!(dstCoor_roi.step & 0x3));
518 if (!(crit.type & cv::TermCriteria::MAX_ITER))
523 int maxIter = std::min(std::max(crit.maxCount, 1), 100);
526 if (!(crit.type & cv::TermCriteria::EPS))
531 eps = (float)std::max(crit.epsilon, 0.0);
535 for (int i = 0; i < 512; i++)
537 tab[i] = (i - 255) * (i - 255);
540 uchar *sptr = src_roi.data;
541 uchar *dptr = dst_roi.data;
542 short *dCoorptr = (short *)dstCoor_roi.data;
543 int sstep = (int)src_roi.step;
544 int dstep = (int)dst_roi.step;
545 int dCoorstep = (int)dstCoor_roi.step >> 1;
546 cv::Size size = src_roi.size();
548 for (int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
549 dptr += dstep - (size.width << 2), dCoorptr += dCoorstep - (size.width << 1))
551 for (int j = 0; j < size.width; j++, sptr += 4, dptr += 4, dCoorptr += 2)
553 *((COOR *)dCoorptr) = do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
559 typedef TestBaseWithParam<Size> meanShiftProcFixture;
561 PERF_TEST_P(meanShiftProcFixture, meanShiftProc,
562 OCL_TYPICAL_MAT_SIZES)
564 const Size srcSize = GetParam();
565 TermCriteria crit(TermCriteria::COUNT + TermCriteria::EPS, 5, 1);
567 Mat src(srcSize, CV_8UC4), dst1(srcSize, CV_8UC4),
568 dst2(srcSize, CV_16SC2);
569 declare.in(src, WARMUP_RNG).out(dst1, dst2)
570 .time(srcSize == OCL_SIZE_4000 ?
571 56 : srcSize == OCL_SIZE_2000 ? 15 : 3.8);;
575 TEST_CYCLE() meanShiftProc_(src, dst1, dst2, 5, 6, crit);
580 else if (RUN_OCL_IMPL)
582 ocl::oclMat oclSrc(src), oclDst1(srcSize, CV_8UC4),
583 oclDst2(srcSize, CV_16SC2);
585 OCL_TEST_CYCLE() ocl::meanShiftProc(oclSrc, oclDst1, oclDst2, 5, 6, crit);
587 oclDst1.download(dst1);
588 oclDst2.download(dst2);
597 ///////////// CLAHE ////////////////////////
599 typedef TestBaseWithParam<Size> CLAHEFixture;
601 PERF_TEST_P(CLAHEFixture, CLAHE, OCL_TYPICAL_MAT_SIZES)
603 const Size srcSize = GetParam();
604 const string impl = getSelectedImpl();
606 Mat src(srcSize, CV_8UC1), dst;
607 const double clipLimit = 40.0;
608 declare.in(src, WARMUP_RNG);
610 if (srcSize == OCL_SIZE_4000)
615 ocl::oclMat oclSrc(src), oclDst;
616 cv::Ptr<cv::CLAHE> oclClahe = cv::ocl::createCLAHE(clipLimit);
618 OCL_TEST_CYCLE() oclClahe->apply(oclSrc, oclDst);
620 oclDst.download(dst);
624 else if (RUN_PLAIN_IMPL)
626 cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(clipLimit);
627 TEST_CYCLE() clahe->apply(src, dst);
635 ///////////// columnSum////////////////////////
637 typedef TestBaseWithParam<Size> columnSumFixture;
639 static void columnSumPerfTest(const Mat & src, Mat & dst)
641 for (int j = 0; j < src.cols; j++)
642 dst.at<float>(0, j) = src.at<float>(0, j);
644 for (int i = 1; i < src.rows; ++i)
645 for (int j = 0; j < src.cols; ++j)
646 dst.at<float>(i, j) = dst.at<float>(i - 1 , j) + src.at<float>(i , j);
649 PERF_TEST_P(columnSumFixture, columnSum, OCL_TYPICAL_MAT_SIZES)
651 const Size srcSize = GetParam();
653 Mat src(srcSize, CV_32FC1), dst(srcSize, CV_32FC1);
654 declare.in(src, WARMUP_RNG).out(dst);
656 if (srcSize == OCL_SIZE_4000)
661 ocl::oclMat oclSrc(src), oclDst(srcSize, CV_32FC1);
663 OCL_TEST_CYCLE() cv::ocl::columnSum(oclSrc, oclDst);
665 oclDst.download(dst);
669 else if (RUN_PLAIN_IMPL)
671 TEST_CYCLE() columnSumPerfTest(src, dst);
679 //////////////////////////////distanceToCenters////////////////////////////////////////////////
681 CV_ENUM(DistType, NORM_L1, NORM_L2SQR)
683 typedef tuple<Size, DistType> distanceToCentersParameters;
684 typedef TestBaseWithParam<distanceToCentersParameters> distanceToCentersFixture;
686 static void distanceToCentersPerfTest(Mat& src, Mat& centers, Mat& dists, Mat& labels, int distType)
689 cv::batchDistance(src, centers, batch_dists, CV_32FC1, noArray(), distType);
691 std::vector<float> dists_v;
692 std::vector<int> labels_v;
694 for (int i = 0; i < batch_dists.rows; i++)
696 Mat r = batch_dists.row(i);
700 minMaxLoc(r, &mVal, NULL, &mLoc, NULL);
701 dists_v.push_back(static_cast<float>(mVal));
702 labels_v.push_back(mLoc.x);
705 Mat(dists_v).copyTo(dists);
706 Mat(labels_v).copyTo(labels);
709 PERF_TEST_P(distanceToCentersFixture, distanceToCenters, ::testing::Combine(::testing::Values(cv::Size(256,256), cv::Size(512,512)), DistType::all()) )
711 Size size = get<0>(GetParam());
712 int distType = get<1>(GetParam());
714 Mat src(size, CV_32FC1), centers(size, CV_32FC1);
715 Mat dists(src.rows, 1, CV_32FC1), labels(src.rows, 1, CV_32SC1);
717 declare.in(src, centers, WARMUP_RNG).out(dists, labels);
721 ocl::oclMat ocl_src(src), ocl_centers(centers);
723 OCL_TEST_CYCLE() ocl::distanceToCenters(ocl_src, ocl_centers, dists, labels, distType);
725 SANITY_CHECK(dists, 1e-6, ERROR_RELATIVE);
726 SANITY_CHECK(labels);
728 else if (RUN_PLAIN_IMPL)
730 TEST_CYCLE() distanceToCentersPerfTest(src, centers, dists, labels, distType);
732 SANITY_CHECK(dists, 1e-6, ERROR_RELATIVE);
733 SANITY_CHECK(labels);