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, 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.
19 // Niko Li, newlife20080214@gmail.com
20 // Jia Haipeng, jiahaipeng95@gmail.com
21 // Shengen Yan, yanshengen@gmail.com
22 // Jiang Liyuan, lyuan001.good@163.com
23 // Rock Li, Rock.Li@amd.com
24 // Wu Zailong, bullet@yeah.net
25 // Xu Pang, pangxu010@163.com
26 // Sen Liu, swjtuls1987@126.com
28 // Redistribution and use in source and binary forms, with or without modification,
29 // are permitted provided that the following conditions are met:
31 // * Redistribution's of source code must retain the above copyright notice,
32 // this list of conditions and the following disclaimer.
34 // * Redistribution's in binary form must reproduce the above copyright notice,
35 // this list of conditions and the following disclaimer in the documentation
36 // and/or other oclMaterials provided with the distribution.
38 // * The name of the copyright holders may not be used to endorse or promote products
39 // derived from this software without specific prior written permission.
41 // This software is provided by the copyright holders and contributors "as is" and
42 // any express or implied warranties, including, but not limited to, the implied
43 // warranties of merchantability and fitness for a particular purpose are disclaimed.
44 // In no event shall the Intel Corporation or contributors be liable for any direct,
45 // indirect, incidental, special, exemplary, or consequential damages
46 // (including, but not limited to, procurement of substitute goods or services;
47 // loss of use, data, or profits; or business interruption) however caused
48 // and on any theory of liability, whether in contract, strict liability,
49 // or tort (including negligence or otherwise) arising in any way out of
50 // the use of this software, even if advised of the possibility of such damage.
54 #include "test_precomp.hpp"
58 using namespace cvtest;
59 using namespace testing;
62 MatType nulltype = -1;
64 #define ONE_TYPE(type) testing::ValuesIn(typeVector(type))
65 #define NULL_TYPE testing::ValuesIn(typeVector(nulltype))
67 vector<MatType> typeVector(MatType type)
80 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)
88 int revx = 0, revy = 0;
93 // iterate meanshift procedure
94 for(iter = 0; iter < maxIter; iter++ )
97 int s0 = 0, s1 = 0, s2 = 0, sx = 0, sy = 0;
99 //mean shift: process pixels in window (p-sigmaSp)x(p+sigmaSp)
105 //deal with the image boundary
106 if(minx < 0) minx = 0;
107 if(miny < 0) miny = 0;
108 if(maxx >= size.width) maxx = size.width - 1;
109 if(maxy >= size.height) maxy = size.height - 1;
116 pstart = pstart + revy * sstep + (revx << 2); //point to the new position
119 ptr = ptr + (miny - y0) * sstep + ((minx - x0) << 2); //point to the start in the row
121 for( int y = miny; y <= maxy; y++, ptr += sstep - ((maxx - minx + 1) << 2))
125 #if CV_ENABLE_UNROLLED
126 for( ; x + 4 <= maxx; x += 4, ptr += 16)
129 t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
130 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
138 t0 = ptr[4], t1 = ptr[5], t2 = ptr[6];
139 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
147 t0 = ptr[8], t1 = ptr[9], t2 = ptr[10];
148 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
156 t0 = ptr[12], t1 = ptr[13], t2 = ptr[14];
157 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
167 for(; x <= maxx; x++, ptr += 4)
169 int t0 = ptr[0], t1 = ptr[1], t2 = ptr[2];
170 if(tab[t0 - c0 + 255] + tab[t1 - c1 + 255] + tab[t2 - c2 + 255] <= isr2)
194 bool stopFlag = (x0 == x1 && y0 == y1) || (abs(x1 - x0) + abs(y1 - y0) +
195 tab[s0 - c0 + 255] + tab[s1 - c1 + 255] + tab[s2 - c2 + 255] <= eps);
197 //revise the pointer corresponding to the new (y0,x0)
222 void meanShiftFiltering_(const Mat &src_roi, Mat &dst_roi, int sp, int sr, cv::TermCriteria crit)
224 if( src_roi.empty() )
225 CV_Error( CV_StsBadArg, "The input image is empty" );
227 if( src_roi.depth() != CV_8U || src_roi.channels() != 4 )
228 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
230 CV_Assert( (src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) );
231 CV_Assert( !(dst_roi.step & 0x3) );
233 if( !(crit.type & cv::TermCriteria::MAX_ITER) )
235 int maxIter = std::min(std::max(crit.maxCount, 1), 100);
237 if( !(crit.type & cv::TermCriteria::EPS) )
239 eps = (float)std::max(crit.epsilon, 0.0);
242 for(int i = 0; i < 512; i++)
243 tab[i] = (i - 255) * (i - 255);
244 uchar *sptr = src_roi.data;
245 uchar *dptr = dst_roi.data;
246 int sstep = (int)src_roi.step;
247 int dstep = (int)dst_roi.step;
248 cv::Size size = src_roi.size();
250 for(int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
251 dptr += dstep - (size.width << 2))
253 for(int j = 0; j < size.width; j++, sptr += 4, dptr += 4)
255 do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
260 void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi, int sp, int sr, cv::TermCriteria crit)
263 if( src_roi.empty() )
264 CV_Error( CV_StsBadArg, "The input image is empty" );
265 if( src_roi.depth() != CV_8U || src_roi.channels() != 4 )
266 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
267 CV_Assert( (src_roi.cols == dst_roi.cols) && (src_roi.rows == dst_roi.rows) &&
268 (src_roi.cols == dstCoor_roi.cols) && (src_roi.rows == dstCoor_roi.rows));
269 CV_Assert( !(dstCoor_roi.step & 0x3) );
271 if( !(crit.type & cv::TermCriteria::MAX_ITER) )
273 int maxIter = std::min(std::max(crit.maxCount, 1), 100);
275 if( !(crit.type & cv::TermCriteria::EPS) )
277 eps = (float)std::max(crit.epsilon, 0.0);
280 for(int i = 0; i < 512; i++)
281 tab[i] = (i - 255) * (i - 255);
282 uchar *sptr = src_roi.data;
283 uchar *dptr = dst_roi.data;
284 short *dCoorptr = (short *)dstCoor_roi.data;
285 int sstep = (int)src_roi.step;
286 int dstep = (int)dst_roi.step;
287 int dCoorstep = (int)dstCoor_roi.step >> 1;
288 cv::Size size = src_roi.size();
290 for(int i = 0; i < size.height; i++, sptr += sstep - (size.width << 2),
291 dptr += dstep - (size.width << 2), dCoorptr += dCoorstep - (size.width << 1))
293 for(int j = 0; j < size.width; j++, sptr += 4, dptr += 4, dCoorptr += 2)
295 *((COOR *)dCoorptr) = do_meanShift(j, i, sptr, dptr, sstep, size, sp, sr, maxIter, eps, tab);
301 PARAM_TEST_CASE(ImgprocTestBase, MatType, MatType, MatType, MatType, MatType, bool)
303 int type1, type2, type3, type4, type5;
324 cv::Mat dst1; //bak, for two outputs
331 cv::Mat dst1_roi; //bak
334 cv::ocl::oclMat clmat1;
335 cv::ocl::oclMat clmat2;
336 cv::ocl::oclMat clmask;
337 cv::ocl::oclMat cldst;
338 cv::ocl::oclMat cldst1; //bak
341 cv::ocl::oclMat clmat1_roi;
342 cv::ocl::oclMat clmat2_roi;
343 cv::ocl::oclMat clmask_roi;
344 cv::ocl::oclMat cldst_roi;
345 cv::ocl::oclMat cldst1_roi;
349 type1 = GET_PARAM(0);
350 type2 = GET_PARAM(1);
351 type3 = GET_PARAM(2);
352 type4 = GET_PARAM(3);
353 type5 = GET_PARAM(4);
354 cv::RNG &rng = TS::ptr()->get_rng();
355 cv::Size size(MWIDTH, MHEIGHT);
356 double min = 1, max = 20;
358 if(type1 != nulltype)
360 mat1 = randomMat(rng, size, type1, min, max, false);
363 if(type2 != nulltype)
365 mat2 = randomMat(rng, size, type2, min, max, false);
368 if(type3 != nulltype)
370 dst = randomMat(rng, size, type3, min, max, false);
373 if(type4 != nulltype)
375 dst1 = randomMat(rng, size, type4, min, max, false);
378 if(type5 != nulltype)
380 mask = randomMat(rng, size, CV_8UC1, 0, 2, false);
381 cv::threshold(mask, mask, 0.5, 255., type5);
384 val = cv::Scalar(rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0));
391 cv::RNG &rng = TS::ptr()->get_rng();
392 roicols = rng.uniform(1, mat1.cols);
393 roirows = rng.uniform(1, mat1.rows);
394 src1x = rng.uniform(0, mat1.cols - roicols);
395 src1y = rng.uniform(0, mat1.rows - roirows);
396 src2x = rng.uniform(0, mat2.cols - roicols);
397 src2y = rng.uniform(0, mat2.rows - roirows);
398 dstx = rng.uniform(0, dst.cols - roicols);
399 dsty = rng.uniform(0, dst.rows - roirows);
400 dst1x = rng.uniform(0, dst1.cols - roicols);
401 dst1y = rng.uniform(0, dst1.rows - roirows);
402 maskx = rng.uniform(0, mask.cols - roicols);
403 masky = rng.uniform(0, mask.rows - roirows);
420 if(type1 != nulltype)
422 mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
423 clmat1_roi = clmat1(Rect(src1x, src1y, roicols, roirows));
425 if(type2 != nulltype)
427 mat2_roi = mat2(Rect(src2x, src2y, roicols, roirows));
428 clmat2_roi = clmat2(Rect(src2x, src2y, roicols, roirows));
430 if(type3 != nulltype)
432 dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
433 cldst_roi = cldst(Rect(dstx, dsty, roicols, roirows));
435 if(type4 != nulltype)
437 dst1_roi = dst1(Rect(dst1x, dst1y, roicols, roirows));
438 cldst1_roi = cldst1(Rect(dst1x, dst1y, roicols, roirows));
440 if(type5 != nulltype)
442 mask_roi = mask(Rect(maskx, masky, roicols, roirows));
443 clmask_roi = clmask(Rect(maskx, masky, roicols, roirows));
447 void Near(double threshold)
450 cldst.download(cpu_cldst);
451 EXPECT_MAT_NEAR(dst, cpu_cldst, threshold);
454 ////////////////////////////////equalizeHist//////////////////////////////////////////
456 struct equalizeHist : ImgprocTestBase {};
458 TEST_P(equalizeHist, Mat)
460 if (mat1.type() != CV_8UC1 || mat1.type() != dst.type())
462 cout << "Unsupported type" << endl;
463 EXPECT_DOUBLE_EQ(0.0, 0.0);
467 for(int j = 0; j < LOOP_TIMES; j++)
470 cv::equalizeHist(mat1_roi, dst_roi);
471 cv::ocl::equalizeHist(clmat1_roi, cldst_roi);
481 ////////////////////////////////bilateralFilter////////////////////////////////////////////
483 struct bilateralFilter : ImgprocTestBase {};
485 TEST_P(bilateralFilter, Mat)
487 double sigmacolor = 50.0;
489 int d = 2 * radius + 1;
490 double sigmaspace = 20.0;
491 int bordertype[] = {cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT, cv::BORDER_WRAP, cv::BORDER_REFLECT_101};
492 //const char *borderstr[] = {"BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101"};
494 if (mat1.depth() != CV_8U || mat1.type() != dst.type())
496 cout << "Unsupported type" << endl;
497 EXPECT_DOUBLE_EQ(0.0, 0.0);
501 for(size_t i = 0; i < sizeof(bordertype) / sizeof(int); i++)
502 for(int j = 0; j < LOOP_TIMES; j++)
505 if(((bordertype[i] != cv::BORDER_CONSTANT) && (bordertype[i] != cv::BORDER_REPLICATE) && (mat1_roi.cols <= radius)) || (mat1_roi.cols <= radius) || (mat1_roi.rows <= radius) || (mat1_roi.rows <= radius))
509 //if((dstx>=radius) && (dsty >= radius) && (dstx+cldst_roi.cols+radius <=cldst_roi.wholecols) && (dsty+cldst_roi.rows+radius <= cldst_roi.wholerows))
511 // dst_roi.adjustROI(radius, radius, radius, radius);
512 // cldst_roi.adjustROI(radius, radius, radius, radius);
519 cv::bilateralFilter(mat1_roi, dst_roi, d, sigmacolor, sigmaspace, bordertype[i] | cv::BORDER_ISOLATED);
520 cv::ocl::bilateralFilter(clmat1_roi, cldst_roi, d, sigmacolor, sigmaspace, bordertype[i] | cv::BORDER_ISOLATED);
528 ////////////////////////////////copyMakeBorder////////////////////////////////////////////
530 struct CopyMakeBorder : ImgprocTestBase {};
532 TEST_P(CopyMakeBorder, Mat)
534 int bordertype[] = {cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT, cv::BORDER_WRAP, cv::BORDER_REFLECT_101};
535 //const char *borderstr[] = {"BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101"};
536 cv::RNG &rng = TS::ptr()->get_rng();
537 int top = rng.uniform(0, 10);
538 int bottom = rng.uniform(0, 10);
539 int left = rng.uniform(0, 10);
540 int right = rng.uniform(0, 10);
541 if (mat1.type() != dst.type())
543 cout << "Unsupported type" << endl;
544 EXPECT_DOUBLE_EQ(0.0, 0.0);
548 for(size_t i = 0; i < sizeof(bordertype) / sizeof(int); i++)
549 for(int j = 0; j < LOOP_TIMES; j++)
553 if(((bordertype[i] != cv::BORDER_CONSTANT) && (bordertype[i] != cv::BORDER_REPLICATE)) && (mat1_roi.cols <= left) || (mat1_roi.cols <= right) || (mat1_roi.rows <= top) || (mat1_roi.rows <= bottom))
557 if((dstx >= left) && (dsty >= top) && (dstx + cldst_roi.cols + right <= cldst_roi.wholecols) && (dsty + cldst_roi.rows + bottom <= cldst_roi.wholerows))
559 dst_roi.adjustROI(top, bottom, left, right);
560 cldst_roi.adjustROI(top, bottom, left, right);
567 cv::copyMakeBorder(mat1_roi, dst_roi, top, bottom, left, right, bordertype[i] | cv::BORDER_ISOLATED, cv::Scalar(1.0));
568 cv::ocl::copyMakeBorder(clmat1_roi, cldst_roi, top, bottom, left, right, bordertype[i] | cv::BORDER_ISOLATED, cv::Scalar(1.0));
572 cldst_roi.download(cpu_cldst);
573 EXPECT_MAT_NEAR(dst_roi, cpu_cldst, 0.0);
575 cldst.download(cpu_cldst);
576 EXPECT_MAT_NEAR(dst, cpu_cldst, 0.0);
585 ////////////////////////////////cornerMinEigenVal//////////////////////////////////////////
587 struct cornerMinEigenVal : ImgprocTestBase {};
589 TEST_P(cornerMinEigenVal, Mat)
591 for(int j = 0; j < LOOP_TIMES; j++)
595 int blockSize = 3, apertureSize = 3;//1 + 2 * (rand() % 4);
596 //int borderType = cv::BORDER_CONSTANT;
597 //int borderType = cv::BORDER_REPLICATE;
598 int borderType = cv::BORDER_REFLECT;
599 cv::cornerMinEigenVal(mat1_roi, dst_roi, blockSize, apertureSize, borderType);
600 cv::ocl::cornerMinEigenVal(clmat1_roi, cldst_roi, blockSize, apertureSize, borderType);
607 ////////////////////////////////cornerHarris//////////////////////////////////////////
609 struct cornerHarris : ImgprocTestBase {};
611 TEST_P(cornerHarris, Mat)
613 for(int j = 0; j < LOOP_TIMES; j++)
617 int blockSize = 3, apertureSize = 3; //1 + 2 * (rand() % 4);
619 //int borderType = cv::BORDER_CONSTANT;
620 //int borderType = cv::BORDER_REPLICATE;
621 int borderType = cv::BORDER_REFLECT;
622 cv::cornerHarris(mat1_roi, dst_roi, blockSize, apertureSize, k, borderType);
623 cv::ocl::cornerHarris(clmat1_roi, cldst_roi, blockSize, apertureSize, k, borderType);
629 ////////////////////////////////integral/////////////////////////////////////////////////
631 struct integral : ImgprocTestBase {};
633 TEST_P(integral, Mat)
635 for(int j = 0; j < LOOP_TIMES; j++)
639 cv::ocl::integral(clmat1_roi, cldst_roi, cldst1_roi);
640 cv::integral(mat1_roi, dst_roi, dst1_roi);
644 cldst1.download(cpu_cldst1);
645 EXPECT_MAT_NEAR(dst1, cpu_cldst1, 0.0);
650 /////////////////////////////////////////////////////////////////////////////////////////////////
651 // warpAffine & warpPerspective
653 PARAM_TEST_CASE(WarpTestBase, MatType, int)
678 //ocl dst mat for testing
679 cv::ocl::oclMat gdst_whole;
682 cv::ocl::oclMat gmat1;
683 cv::ocl::oclMat gdst;
688 //dsize = GET_PARAM(1);
689 interpolation = GET_PARAM(1);
691 cv::RNG &rng = TS::ptr()->get_rng();
692 size = cv::Size(MWIDTH, MHEIGHT);
694 mat1 = randomMat(rng, size, type, 5, 16, false);
695 dst = randomMat(rng, size, type, 5, 16, false);
703 cv::RNG &rng = TS::ptr()->get_rng();
704 src_roicols = rng.uniform(1, mat1.cols);
705 src_roirows = rng.uniform(1, mat1.rows);
706 dst_roicols = rng.uniform(1, dst.cols);
707 dst_roirows = rng.uniform(1, dst.rows);
708 src1x = rng.uniform(0, mat1.cols - src_roicols);
709 src1y = rng.uniform(0, mat1.rows - src_roirows);
710 dstx = rng.uniform(0, dst.cols - dst_roicols);
711 dsty = rng.uniform(0, dst.rows - dst_roirows);
713 src_roicols = mat1.cols;
714 src_roirows = mat1.rows;
715 dst_roicols = dst.cols;
716 dst_roirows = dst.rows;
724 mat1_roi = mat1(Rect(src1x, src1y, src_roicols, src_roirows));
725 dst_roi = dst(Rect(dstx, dsty, dst_roicols, dst_roirows));
728 gdst = gdst_whole(Rect(dstx, dsty, dst_roicols, dst_roirows));
738 struct WarpAffine : WarpTestBase {};
740 TEST_P(WarpAffine, Mat)
742 static const double coeffs[2][3] =
744 {cos(CV_PI / 6), -sin(CV_PI / 6), 100.0},
745 {sin(CV_PI / 6), cos(CV_PI / 6), -100.0}
747 Mat M(2, 3, CV_64F, (void *)coeffs);
749 for(int j = 0; j < LOOP_TIMES; j++)
753 cv::warpAffine(mat1_roi, dst_roi, M, size, interpolation);
754 cv::ocl::warpAffine(gmat1, gdst, M, size, interpolation);
757 gdst_whole.download(cpu_dst);
758 EXPECT_MAT_NEAR(dst, cpu_dst, 1.0);
766 struct WarpPerspective : WarpTestBase {};
768 TEST_P(WarpPerspective, Mat)
770 static const double coeffs[3][3] =
772 {cos(3.14 / 6), -sin(3.14 / 6), 100.0},
773 {sin(3.14 / 6), cos(3.14 / 6), -100.0},
776 Mat M(3, 3, CV_64F, (void *)coeffs);
778 for(int j = 0; j < LOOP_TIMES; j++)
782 cv::warpPerspective(mat1_roi, dst_roi, M, size, interpolation);
783 cv::ocl::warpPerspective(gmat1, gdst, M, size, interpolation);
786 gdst_whole.download(cpu_dst);
787 EXPECT_MAT_NEAR(dst, cpu_dst, 1.0);
792 /////////////////////////////////////////////////////////////////////////////////////////////////
794 //////////////////////////////////////////////////////////////////////////////////////////////////
796 PARAM_TEST_CASE(Remap, MatType, MatType, MatType, int, int)
811 //std::vector<cv::ocl::Info> oclinfo;
835 //ocl mat for testing
836 cv::ocl::oclMat gdst;
839 cv::ocl::oclMat gsrc_roi;
840 cv::ocl::oclMat gdst_roi;
841 cv::ocl::oclMat gmap1_roi;
842 cv::ocl::oclMat gmap2_roi;
846 srcType = GET_PARAM(0);
847 map1Type = GET_PARAM(1);
848 map2Type = GET_PARAM(2);
849 interpolation = GET_PARAM(3);
850 bordertype = GET_PARAM(4);
852 cv::RNG &rng = TS::ptr()->get_rng();
853 cv::Size srcSize = cv::Size(MWIDTH, MHEIGHT);
854 cv::Size map1Size = cv::Size(MWIDTH, MHEIGHT);
855 double min = 5, max = 16;
857 if(srcType != nulltype)
859 src = randomMat(rng, srcSize, srcType, min, max, false);
861 if((map1Type == CV_16SC2 && map2Type == nulltype) || (map1Type == CV_32FC2 && map2Type == nulltype))
863 map1 = randomMat(rng, map1Size, map1Type, min, max, false);
865 else if (map1Type == CV_32FC1 && map2Type == CV_32FC1)
867 map1 = randomMat(rng, map1Size, map1Type, min, max, false);
868 map2 = randomMat(rng, map1Size, map1Type, min, max, false);
873 cout << "The wrong input type" << endl;
877 dst = randomMat(rng, map1Size, srcType, min, max, false);
878 switch (src.channels())
881 val = cv::Scalar(rng.uniform(0.0, 10.0), 0, 0, 0);
884 val = cv::Scalar(rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0), 0, 0);
887 val = cv::Scalar(rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0), 0);
890 val = cv::Scalar(rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0), rng.uniform(0.0, 10.0));
897 cv::RNG &rng = TS::ptr()->get_rng();
899 dst_roicols = rng.uniform(1, dst.cols);
900 dst_roirows = rng.uniform(1, dst.rows);
902 src_roicols = rng.uniform(1, src.cols);
903 src_roirows = rng.uniform(1, src.rows);
906 srcx = rng.uniform(0, src.cols - src_roicols);
907 srcy = rng.uniform(0, src.rows - src_roirows);
908 dstx = rng.uniform(0, dst.cols - dst_roicols);
909 dsty = rng.uniform(0, dst.rows - dst_roirows);
910 map1_roicols = dst_roicols;
911 map1_roirows = dst_roirows;
912 map2_roicols = dst_roicols;
913 map2_roirows = dst_roirows;
919 if((map1Type == CV_16SC2 && map2Type == nulltype) || (map1Type == CV_32FC2 && map2Type == nulltype))
921 map1_roi = map1(Rect(map1x, map1y, map1_roicols, map1_roirows));
922 gmap1_roi = map1_roi;
925 else if (map1Type == CV_32FC1 && map2Type == CV_32FC1)
927 map1_roi = map1(Rect(map1x, map1y, map1_roicols, map1_roirows));
928 gmap1_roi = map1_roi;
929 map2_roi = map2(Rect(map2x, map2y, map2_roicols, map2_roirows));
930 gmap2_roi = map2_roi;
932 src_roi = src(Rect(srcx, srcy, src_roicols, src_roirows));
933 dst_roi = dst(Rect(dstx, dsty, dst_roicols, dst_roirows));
936 gdst_roi = gdst(Rect(dstx, dsty, dst_roicols, dst_roirows));
942 if((interpolation == 1 && map1Type == CV_16SC2) || (map1Type == CV_32FC1 && map2Type == nulltype) || (map1Type == CV_16SC2 && map2Type == CV_32FC1) || (map1Type == CV_32FC2 && map2Type == CV_32FC1))
944 cout << "Don't support the dataType" << endl;
947 int bordertype[] = {cv::BORDER_CONSTANT, cv::BORDER_REPLICATE/*,BORDER_REFLECT,BORDER_WRAP,BORDER_REFLECT_101*/};
948 //const char *borderstr[] = {"BORDER_CONSTANT", "BORDER_REPLICATE"/*, "BORDER_REFLECT","BORDER_WRAP","BORDER_REFLECT_101"*/};
949 // for(int i = 0; i < sizeof(bordertype)/sizeof(int); i++)
950 for(int j = 0; j < LOOP_TIMES; j++)
953 cv::remap(src_roi, dst_roi, map1_roi, map2_roi, interpolation, bordertype[0], val);
954 cv::ocl::remap(gsrc_roi, gdst_roi, gmap1_roi, gmap2_roi, interpolation, bordertype[0], val);
956 gdst.download(cpu_dst);
958 if(interpolation == 0)
959 EXPECT_MAT_NEAR(dst, cpu_dst, 1.0);
960 EXPECT_MAT_NEAR(dst, cpu_dst, 2.0);
967 /////////////////////////////////////////////////////////////////////////////////////////////////
970 PARAM_TEST_CASE(Resize, MatType, cv::Size, double, double, int)
995 //ocl dst mat for testing
996 cv::ocl::oclMat gdst_whole;
999 cv::ocl::oclMat gmat1;
1000 cv::ocl::oclMat gdst;
1002 virtual void SetUp()
1004 type = GET_PARAM(0);
1005 dsize = GET_PARAM(1);
1008 interpolation = GET_PARAM(4);
1010 cv::RNG &rng = TS::ptr()->get_rng();
1012 cv::Size size(MWIDTH, MHEIGHT);
1014 if(dsize == cv::Size() && !(fx > 0 && fy > 0))
1016 cout << "invalid dsize and fx fy" << endl;
1020 if(dsize == cv::Size())
1022 dsize.width = (int)(size.width * fx);
1023 dsize.height = (int)(size.height * fy);
1026 mat1 = randomMat(rng, size, type, 5, 16, false);
1027 dst = randomMat(rng, dsize, type, 5, 16, false);
1035 cv::RNG &rng = TS::ptr()->get_rng();
1036 src_roicols = rng.uniform(1, mat1.cols);
1037 src_roirows = rng.uniform(1, mat1.rows);
1038 dst_roicols = (int)(src_roicols * fx);
1039 dst_roirows = (int)(src_roirows * fy);
1040 src1x = rng.uniform(0, mat1.cols - src_roicols);
1041 src1y = rng.uniform(0, mat1.rows - src_roirows);
1042 dstx = rng.uniform(0, dst.cols - dst_roicols);
1043 dsty = rng.uniform(0, dst.rows - dst_roirows);
1045 src_roicols = mat1.cols;
1046 src_roirows = mat1.rows;
1047 dst_roicols = dst.cols;
1048 dst_roirows = dst.rows;
1054 dsize.width = dst_roicols;
1055 dsize.height = dst_roirows;
1056 mat1_roi = mat1(Rect(src1x, src1y, src_roicols, src_roirows));
1057 dst_roi = dst(Rect(dstx, dsty, dst_roicols, dst_roirows));
1060 gdst = gdst_whole(Rect(dstx, dsty, dst_roicols, dst_roirows));
1062 dsize.width = (int)(mat1_roi.size().width * fx);
1063 dsize.height = (int)(mat1_roi.size().height * fy);
1072 for(int j = 0; j < LOOP_TIMES; j++)
1076 // cv::resize(mat1_roi, dst_roi, dsize, fx, fy, interpolation);
1077 // cv::ocl::resize(gmat1, gdst, dsize, fx, fy, interpolation);
1078 if(dst_roicols < 1 || dst_roirows < 1) continue;
1079 cv::resize(mat1_roi, dst_roi, dsize, fx, fy, interpolation);
1080 cv::ocl::resize(gmat1, gdst, dsize, fx, fy, interpolation);
1083 gdst_whole.download(cpu_dst);
1084 EXPECT_MAT_NEAR(dst, cpu_dst, 1.0);
1090 /////////////////////////////////////////////////////////////////////////////////////////////////
1093 PARAM_TEST_CASE(Threshold, MatType, ThreshOp)
1114 //ocl dst mat for testing
1115 cv::ocl::oclMat gdst_whole;
1118 cv::ocl::oclMat gmat1;
1119 cv::ocl::oclMat gdst;
1121 virtual void SetUp()
1123 type = GET_PARAM(0);
1124 threshOp = GET_PARAM(1);
1126 cv::RNG &rng = TS::ptr()->get_rng();
1127 cv::Size size(MWIDTH, MHEIGHT);
1129 mat1 = randomMat(rng, size, type, 5, 16, false);
1130 dst = randomMat(rng, size, type, 5, 16, false);
1137 cv::RNG &rng = TS::ptr()->get_rng();
1138 roicols = rng.uniform(1, mat1.cols);
1139 roirows = rng.uniform(1, mat1.rows);
1140 src1x = rng.uniform(0, mat1.cols - roicols);
1141 src1y = rng.uniform(0, mat1.rows - roirows);
1142 dstx = rng.uniform(0, dst.cols - roicols);
1143 dsty = rng.uniform(0, dst.rows - roirows);
1145 roicols = mat1.cols;
1146 roirows = mat1.rows;
1153 mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
1154 dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
1157 gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
1165 TEST_P(Threshold, Mat)
1167 for(int j = 0; j < LOOP_TIMES; j++)
1170 double maxVal = randomDouble(20.0, 127.0);
1171 double thresh = randomDouble(0.0, maxVal);
1173 cv::threshold(mat1_roi, dst_roi, thresh, maxVal, threshOp);
1174 cv::ocl::threshold(gmat1, gdst, thresh, maxVal, threshOp);
1177 gdst_whole.download(cpu_dst);
1178 EXPECT_MAT_NEAR(dst, cpu_dst, 1);
1183 PARAM_TEST_CASE(meanShiftTestBase, MatType, MatType, int, int, cv::TermCriteria)
1187 cv::TermCriteria crit;
1204 cv::Mat dstCoor_roi;
1207 cv::ocl::oclMat gdst;
1208 cv::ocl::oclMat gdstCoor;
1211 cv::ocl::oclMat gsrc_roi;
1212 cv::ocl::oclMat gdst_roi;
1213 cv::ocl::oclMat gdstCoor_roi;
1215 virtual void SetUp()
1217 type = GET_PARAM(0);
1218 typeCoor = GET_PARAM(1);
1221 crit = GET_PARAM(4);
1223 cv::RNG &rng = TS::ptr()->get_rng();
1225 // MWIDTH=256, MHEIGHT=256. defined in utility.hpp
1226 cv::Size size = cv::Size(MWIDTH, MHEIGHT);
1228 src = randomMat(rng, size, type, 5, 16, false);
1229 dst = randomMat(rng, size, type, 5, 16, false);
1230 dstCoor = randomMat(rng, size, typeCoor, 5, 16, false);
1237 cv::RNG &rng = TS::ptr()->get_rng();
1240 roicols = rng.uniform(1, src.cols);
1241 roirows = rng.uniform(1, src.rows);
1242 srcx = rng.uniform(0, src.cols - roicols);
1243 srcy = rng.uniform(0, src.rows - roirows);
1244 dstx = rng.uniform(0, dst.cols - roicols);
1245 dsty = rng.uniform(0, dst.rows - roirows);
1254 src_roi = src(Rect(srcx, srcy, roicols, roirows));
1255 dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
1256 dstCoor_roi = dstCoor(Rect(dstx, dsty, roicols, roirows));
1262 gdst_roi = gdst(Rect(dstx, dsty, roicols, roirows)); //gdst_roi
1263 gdstCoor_roi = gdstCoor(Rect(dstx, dsty, roicols, roirows));
1267 /////////////////////////meanShiftFiltering/////////////////////////////
1268 struct meanShiftFiltering : meanShiftTestBase {};
1270 TEST_P(meanShiftFiltering, Mat)
1273 for(int j = 0; j < LOOP_TIMES; j++)
1278 gdst.download(cpu_gdst);
1280 meanShiftFiltering_(src_roi, dst_roi, sp, sr, crit);
1281 cv::ocl::meanShiftFiltering(gsrc_roi, gdst_roi, sp, sr, crit);
1283 gdst.download(cpu_gdst);
1284 EXPECT_MAT_NEAR(dst, cpu_gdst, 0.0);
1288 ///////////////////////////meanShiftProc//////////////////////////////////
1289 struct meanShiftProc : meanShiftTestBase {};
1291 TEST_P(meanShiftProc, Mat)
1294 for(int j = 0; j < LOOP_TIMES; j++)
1299 cv::Mat cpu_gdstCoor;
1301 meanShiftProc_(src_roi, dst_roi, dstCoor_roi, sp, sr, crit);
1302 cv::ocl::meanShiftProc(gsrc_roi, gdst_roi, gdstCoor_roi, sp, sr, crit);
1304 gdst.download(cpu_gdst);
1305 gdstCoor.download(cpu_gdstCoor);
1306 EXPECT_MAT_NEAR(dst, cpu_gdst, 0.0);
1307 EXPECT_MAT_NEAR(dstCoor, cpu_gdstCoor, 0.0);
1311 ///////////////////////////////////////////////////////////////////////////////////////
1313 void calcHistGold(const cv::Mat &src, cv::Mat &hist)
1315 hist.create(1, 256, CV_32SC1);
1316 hist.setTo(cv::Scalar::all(0));
1318 int *hist_row = hist.ptr<int>();
1319 for (int y = 0; y < src.rows; ++y)
1321 const uchar *src_row = src.ptr(y);
1323 for (int x = 0; x < src.cols; ++x)
1324 ++hist_row[src_row[x]];
1328 PARAM_TEST_CASE(histTestBase, MatType, MatType)
1342 //ocl dst mat, dst_hist and gdst_hist don't have roi
1343 cv::ocl::oclMat gdst_hist;
1345 cv::ocl::oclMat gsrc_roi;
1347 virtual void SetUp()
1349 type_src = GET_PARAM(0);
1351 cv::RNG &rng = TS::ptr()->get_rng();
1352 cv::Size size = cv::Size(MWIDTH, MHEIGHT);
1354 src = randomMat(rng, size, type_src, 0, 256, false);
1361 cv::RNG &rng = TS::ptr()->get_rng();
1364 roicols = rng.uniform(1, src.cols);
1365 roirows = rng.uniform(1, src.rows);
1366 srcx = rng.uniform(0, src.cols - roicols);
1367 srcy = rng.uniform(0, src.rows - roirows);
1374 src_roi = src(Rect(srcx, srcy, roicols, roirows));
1379 ///////////////////////////calcHist///////////////////////////////////////
1380 struct calcHist : histTestBase {};
1382 TEST_P(calcHist, Mat)
1384 for(int j = 0; j < LOOP_TIMES; j++)
1390 calcHistGold(src_roi, dst_hist);
1391 cv::ocl::calcHist(gsrc_roi, gdst_hist);
1393 gdst_hist.download(cpu_hist);
1394 EXPECT_MAT_NEAR(dst_hist, cpu_hist, 0.0);
1397 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1401 IMPLEMENT_PARAM_CLASS(ClipLimit, double)
1404 PARAM_TEST_CASE(CLAHE, cv::Size, ClipLimit)
1412 cv::ocl::oclMat g_src;
1413 cv::ocl::oclMat g_dst;
1415 virtual void SetUp()
1417 size = GET_PARAM(0);
1418 clipLimit = GET_PARAM(1);
1420 cv::RNG &rng = TS::ptr()->get_rng();
1421 src = randomMat(rng, size, CV_8UC1, 0, 256, false);
1426 TEST_P(CLAHE, Accuracy)
1428 cv::Ptr<cv::CLAHE> clahe = cv::ocl::createCLAHE(clipLimit);
1429 clahe->apply(g_src, g_dst);
1432 cv::Ptr<cv::CLAHE> clahe_gold = cv::createCLAHE(clipLimit);
1433 clahe_gold->apply(src, dst_gold);
1435 EXPECT_MAT_NEAR(dst_gold, dst, 1.0);
1438 ///////////////////////////Convolve//////////////////////////////////
1439 PARAM_TEST_CASE(ConvolveTestBase, MatType, bool)
1446 cv::Mat dst1; //bak, for two outputs
1460 cv::Mat dst1_roi; //bak
1461 //ocl dst mat for testing
1462 cv::ocl::oclMat gdst_whole;
1463 cv::ocl::oclMat gdst1_whole; //bak
1465 cv::ocl::oclMat gmat1;
1466 cv::ocl::oclMat gmat2;
1467 cv::ocl::oclMat gdst;
1468 cv::ocl::oclMat gdst1; //bak
1469 virtual void SetUp()
1471 type = GET_PARAM(0);
1473 cv::RNG &rng = TS::ptr()->get_rng();
1475 cv::Size size(MWIDTH, MHEIGHT);
1477 mat1 = randomMat(rng, size, type, 5, 16, false);
1478 mat2 = randomMat(rng, size, type, 5, 16, false);
1479 dst = randomMat(rng, size, type, 5, 16, false);
1480 dst1 = randomMat(rng, size, type, 5, 16, false);
1484 cv::RNG &rng = TS::ptr()->get_rng();
1488 roicols = rng.uniform(1, mat1.cols);
1489 roirows = rng.uniform(1, mat1.rows);
1490 src1x = rng.uniform(0, mat1.cols - roicols);
1491 src1y = rng.uniform(0, mat1.rows - roirows);
1492 dstx = rng.uniform(0, dst.cols - roicols);
1493 dsty = rng.uniform(0, dst.rows - roirows);
1495 roicols = mat1.cols;
1496 roirows = mat1.rows;
1502 src2x = rng.uniform(0, mat2.cols - roicols);
1503 src2y = rng.uniform(0, mat2.rows - roirows);
1504 mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
1505 mat2_roi = mat2(Rect(src2x, src2y, roicols, roirows));
1506 dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
1507 dst1_roi = dst1(Rect(dstx, dsty, roicols, roirows));
1510 gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
1513 gdst1 = gdst1_whole(Rect(dstx, dsty, roicols, roirows));
1521 struct Convolve : ConvolveTestBase {};
1523 void conv2( cv::Mat x, cv::Mat y, cv::Mat z)
1534 float *kerneldata = (float *)(x.data);
1535 float *srcdata = (float *)(y.data);
1536 float *dstdata = (float *)(z.data);
1538 for(i = 0; i < N2; i++)
1539 for(j = 0; j < M2; j++)
1542 for(m = 0; m < N1; m++)
1543 for(n = 0; n < M1; n++)
1546 r = min(max((i - N1 / 2 + m), 0), N2 - 1);
1547 c = min(max((j - M1 / 2 + n), 0), M2 - 1);
1548 temp += kerneldata[m * (x.step >> 2) + n] * srcdata[r * (y.step >> 2) + c];
1550 dstdata[i * (z.step >> 2) + j] = temp;
1553 TEST_P(Convolve, Mat)
1555 if(mat1.type() != CV_32FC1)
1557 cout << "\tUnsupported type\t\n";
1559 for(int j = 0; j < LOOP_TIMES; j++)
1562 cv::ocl::oclMat temp1;
1563 cv::Mat kernel_cpu = mat2(Rect(0, 0, 7, 7));
1566 conv2(kernel_cpu, mat1_roi, dst_roi);
1567 cv::ocl::convolve(gmat1, temp1, gdst);
1570 gdst_whole.download(cpu_dst);
1571 EXPECT_MAT_NEAR(dst, cpu_dst, .1);
1576 //////////////////////////////// ColumnSum //////////////////////////////////////
1577 PARAM_TEST_CASE(ColumnSum, cv::Size)
1582 virtual void SetUp()
1584 size = GET_PARAM(0);
1588 TEST_P(ColumnSum, Accuracy)
1590 cv::Mat src = randomMat(size, CV_32FC1);
1591 cv::ocl::oclMat d_dst;
1592 cv::ocl::oclMat d_src(src);
1594 cv::ocl::columnSum(d_src, d_dst);
1598 for (int j = 0; j < src.cols; ++j)
1600 float gold = src.at<float>(0, j);
1601 float res = dst.at<float>(0, j);
1602 ASSERT_NEAR(res, gold, 1e-5);
1605 for (int i = 1; i < src.rows; ++i)
1607 for (int j = 0; j < src.cols; ++j)
1609 float gold = src.at<float>(i, j) += src.at<float>(i - 1, j);
1610 float res = dst.at<float>(i, j);
1611 ASSERT_NEAR(res, gold, 1e-5);
1615 /////////////////////////////////////////////////////////////////////////////////////
1617 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, equalizeHist, Combine(
1623 Values(false))); // Values(false) is the reserved parameter
1625 //INSTANTIATE_TEST_CASE_P(ImgprocTestBase, bilateralFilter, Combine(
1626 // ONE_TYPE(CV_8UC1),
1628 // ONE_TYPE(CV_8UC1),
1631 // Values(false))); // Values(false) is the reserved parameter
1632 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, bilateralFilter, Combine(
1633 Values(CV_8UC1, CV_8UC3),
1635 Values(CV_8UC1, CV_8UC3),
1638 Values(false))); // Values(false) is the reserved parameter
1641 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, CopyMakeBorder, Combine(
1642 Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC3, CV_32FC4),
1644 Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32SC1, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC3, CV_32FC4),
1647 Values(false))); // Values(false) is the reserved parameter
1649 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, cornerMinEigenVal, Combine(
1650 Values(CV_8UC1, CV_32FC1),
1655 Values(false))); // Values(false) is the reserved parameter
1657 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, cornerHarris, Combine(
1658 Values(CV_8UC1, CV_32FC1),
1663 Values(false))); // Values(false) is the reserved parameter
1666 INSTANTIATE_TEST_CASE_P(ImgprocTestBase, integral, Combine(
1672 Values(false))); // Values(false) is the reserved parameter
1674 INSTANTIATE_TEST_CASE_P(Imgproc, WarpAffine, Combine(
1675 Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
1676 Values((MatType)cv::INTER_NEAREST, (MatType)cv::INTER_LINEAR,
1677 (MatType)cv::INTER_CUBIC, (MatType)(cv::INTER_NEAREST | cv::WARP_INVERSE_MAP),
1678 (MatType)(cv::INTER_LINEAR | cv::WARP_INVERSE_MAP), (MatType)(cv::INTER_CUBIC | cv::WARP_INVERSE_MAP))));
1681 INSTANTIATE_TEST_CASE_P(Imgproc, WarpPerspective, Combine
1682 (Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
1683 Values((MatType)cv::INTER_NEAREST, (MatType)cv::INTER_LINEAR,
1684 (MatType)cv::INTER_CUBIC, (MatType)(cv::INTER_NEAREST | cv::WARP_INVERSE_MAP),
1685 (MatType)(cv::INTER_LINEAR | cv::WARP_INVERSE_MAP), (MatType)(cv::INTER_CUBIC | cv::WARP_INVERSE_MAP))));
1688 INSTANTIATE_TEST_CASE_P(Imgproc, Resize, Combine(
1689 Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4), Values(cv::Size()),
1690 Values(0.5, 1.5, 2), Values(0.5, 1.5, 2), Values((MatType)cv::INTER_NEAREST, (MatType)cv::INTER_LINEAR)));
1693 INSTANTIATE_TEST_CASE_P(Imgproc, Threshold, Combine(
1694 Values(CV_8UC1, CV_32FC1), Values(ThreshOp(cv::THRESH_BINARY),
1695 ThreshOp(cv::THRESH_BINARY_INV), ThreshOp(cv::THRESH_TRUNC),
1696 ThreshOp(cv::THRESH_TOZERO), ThreshOp(cv::THRESH_TOZERO_INV))));
1699 INSTANTIATE_TEST_CASE_P(Imgproc, meanShiftFiltering, Combine(
1704 Values(cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 5, 1))
1708 INSTANTIATE_TEST_CASE_P(Imgproc, meanShiftProc, Combine(
1713 Values(cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 5, 1))
1716 INSTANTIATE_TEST_CASE_P(Imgproc, Remap, Combine(
1717 Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
1718 Values(CV_32FC1, CV_16SC2, CV_32FC2), Values(-1, CV_32FC1),
1719 Values((int)cv::INTER_NEAREST, (int)cv::INTER_LINEAR),
1720 Values((int)cv::BORDER_CONSTANT)));
1723 INSTANTIATE_TEST_CASE_P(histTestBase, calcHist, Combine(
1725 ONE_TYPE(CV_32SC1) //no use
1728 INSTANTIATE_TEST_CASE_P(ImgProc, CLAHE, Combine(
1729 Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300)),
1730 Values(0.0, 40.0)));
1732 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, ColumnSum, DIFFERENT_SIZES);
1734 #endif // HAVE_OPENCL