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, 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, 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
27 //
28 // Redistribution and use in source and binary forms, with or without modification,
29 // are permitted provided that the following conditions are met:
30 //
31 // * Redistribution's of source code must retain the above copyright notice,
32 // this list of conditions and the following disclaimer.
33 //
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 materials provided with the distribution.
37 //
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.
40 //
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.
51 //
52 //M*/
53
54 #include "../test_precomp.hpp"
55 #include "cvconfig.h"
56 #include "opencv2/ts/ocl_test.hpp"
57
58 #ifdef HAVE_OPENCL
59
60 namespace cvtest {
61 namespace ocl {
62
63 ///////////////////////////////////////////////////////////////////////////////
64
PARAM_TEST_CASE(ImgprocTestBase,MatType,int,int,bool)65 PARAM_TEST_CASE(ImgprocTestBase, MatType,
66 int, // blockSize
67 int, // border type
68 bool) // roi or not
69 {
70 int type, borderType, blockSize;
71 bool useRoi;
72
73 TEST_DECLARE_INPUT_PARAMETER(src);
74 TEST_DECLARE_OUTPUT_PARAMETER(dst);
75
76 virtual void SetUp()
77 {
78 type = GET_PARAM(0);
79 blockSize = GET_PARAM(1);
80 borderType = GET_PARAM(2);
81 useRoi = GET_PARAM(3);
82 }
83
84 virtual void random_roi()
85 {
86 Size roiSize = randomSize(1, MAX_VALUE);
87 Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
88 randomSubMat(src, src_roi, roiSize, srcBorder, type, 5, 256);
89
90 Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
91 randomSubMat(dst, dst_roi, roiSize, dstBorder, type, 5, 16);
92
93 UMAT_UPLOAD_INPUT_PARAMETER(src);
94 UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
95 }
96
97 void Near(double threshold = 0.0, bool relative = false)
98 {
99 if (relative)
100 OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
101 else
102 OCL_EXPECT_MATS_NEAR(dst, threshold);
103 }
104 };
105
106 //////////////////////////////// copyMakeBorder ////////////////////////////////////////////
107
PARAM_TEST_CASE(CopyMakeBorder,MatDepth,Channels,bool,BorderType,bool)108 PARAM_TEST_CASE(CopyMakeBorder, MatDepth, // depth
109 Channels, // channels
110 bool, // isolated or not
111 BorderType, // border type
112 bool) // roi or not
113 {
114 int type, borderType;
115 bool useRoi;
116
117 TestUtils::Border border;
118 Scalar val;
119
120 TEST_DECLARE_INPUT_PARAMETER(src);
121 TEST_DECLARE_OUTPUT_PARAMETER(dst);
122
123 virtual void SetUp()
124 {
125 type = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
126 borderType = GET_PARAM(3);
127
128 if (GET_PARAM(2))
129 borderType |= BORDER_ISOLATED;
130
131 useRoi = GET_PARAM(4);
132 }
133
134 void random_roi()
135 {
136 border = randomBorder(0, MAX_VALUE << 2);
137 val = randomScalar(-MAX_VALUE, MAX_VALUE);
138
139 Size roiSize = randomSize(1, MAX_VALUE);
140 Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
141 randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
142
143 Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
144 dstBorder.top += border.top;
145 dstBorder.lef += border.lef;
146 dstBorder.rig += border.rig;
147 dstBorder.bot += border.bot;
148
149 randomSubMat(dst, dst_roi, roiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
150
151 UMAT_UPLOAD_INPUT_PARAMETER(src);
152 UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
153 }
154
155 void Near()
156 {
157 OCL_EXPECT_MATS_NEAR(dst, 0);
158 }
159 };
160
OCL_TEST_P(CopyMakeBorder,Mat)161 OCL_TEST_P(CopyMakeBorder, Mat)
162 {
163 for (int i = 0; i < test_loop_times; ++i)
164 {
165 random_roi();
166
167 OCL_OFF(cv::copyMakeBorder(src_roi, dst_roi, border.top, border.bot, border.lef, border.rig, borderType, val));
168 OCL_ON(cv::copyMakeBorder(usrc_roi, udst_roi, border.top, border.bot, border.lef, border.rig, borderType, val));
169
170 Near();
171 }
172 }
173
174 //////////////////////////////// equalizeHist //////////////////////////////////////////////
175
176 typedef ImgprocTestBase EqualizeHist;
177
OCL_TEST_P(EqualizeHist,Mat)178 OCL_TEST_P(EqualizeHist, Mat)
179 {
180 for (int j = 0; j < test_loop_times; j++)
181 {
182 random_roi();
183
184 OCL_OFF(cv::equalizeHist(src_roi, dst_roi));
185 OCL_ON(cv::equalizeHist(usrc_roi, udst_roi));
186
187 Near(1);
188 }
189 }
190
191 //////////////////////////////// Corners test //////////////////////////////////////////
192
193 struct CornerTestBase :
194 public ImgprocTestBase
195 {
random_roicvtest::ocl::CornerTestBase196 virtual void random_roi()
197 {
198 Mat image = readImageType("../gpu/stereobm/aloe-L.png", type);
199 ASSERT_FALSE(image.empty());
200
201 bool isFP = CV_MAT_DEPTH(type) >= CV_32F;
202 float val = 255.0f;
203 if (isFP)
204 {
205 image.convertTo(image, -1, 1.0 / 255);
206 val /= 255.0f;
207 }
208
209 Size roiSize = image.size();
210 Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
211
212 Size wholeSize = Size(roiSize.width + srcBorder.lef + srcBorder.rig, roiSize.height + srcBorder.top + srcBorder.bot);
213 src = randomMat(wholeSize, type, -val, val, false);
214 src_roi = src(Rect(srcBorder.lef, srcBorder.top, roiSize.width, roiSize.height));
215 image.copyTo(src_roi);
216
217 Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
218 randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_32FC1, 5, 16);
219
220 UMAT_UPLOAD_INPUT_PARAMETER(src);
221 UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
222 }
223 };
224
225 typedef CornerTestBase CornerMinEigenVal;
226
OCL_TEST_P(CornerMinEigenVal,Mat)227 OCL_TEST_P(CornerMinEigenVal, Mat)
228 {
229 for (int j = 0; j < test_loop_times; j++)
230 {
231 random_roi();
232
233 int apertureSize = 3;
234
235 OCL_OFF(cv::cornerMinEigenVal(src_roi, dst_roi, blockSize, apertureSize, borderType));
236 OCL_ON(cv::cornerMinEigenVal(usrc_roi, udst_roi, blockSize, apertureSize, borderType));
237
238 Near(1e-5, true);
239 }
240 }
241
242 //////////////////////////////// cornerHarris //////////////////////////////////////////
243
244 typedef CornerTestBase CornerHarris;
245
OCL_TEST_P(CornerHarris,Mat)246 OCL_TEST_P(CornerHarris, Mat)
247 {
248 for (int j = 0; j < test_loop_times; j++)
249 {
250 random_roi();
251
252 int apertureSize = 3;
253 double k = randomDouble(0.01, 0.9);
254
255 OCL_OFF(cv::cornerHarris(src_roi, dst_roi, blockSize, apertureSize, k, borderType));
256 OCL_ON(cv::cornerHarris(usrc_roi, udst_roi, blockSize, apertureSize, k, borderType));
257
258 Near(1e-6, true);
259 }
260 }
261
262 //////////////////////////////// preCornerDetect //////////////////////////////////////////
263
264 typedef ImgprocTestBase PreCornerDetect;
265
OCL_TEST_P(PreCornerDetect,Mat)266 OCL_TEST_P(PreCornerDetect, Mat)
267 {
268 for (int j = 0; j < test_loop_times; j++)
269 {
270 random_roi();
271
272 const int apertureSize = blockSize;
273
274 OCL_OFF(cv::preCornerDetect(src_roi, dst_roi, apertureSize, borderType));
275 OCL_ON(cv::preCornerDetect(usrc_roi, udst_roi, apertureSize, borderType));
276
277 Near(1e-6, true);
278 }
279 }
280
281
282 ////////////////////////////////// integral /////////////////////////////////////////////////
283
284 struct Integral :
285 public ImgprocTestBase
286 {
287 int sdepth, sqdepth;
288
289 TEST_DECLARE_OUTPUT_PARAMETER(dst2);
290
SetUpcvtest::ocl::Integral291 virtual void SetUp()
292 {
293 type = GET_PARAM(0);
294 sdepth = GET_PARAM(1);
295 sqdepth = GET_PARAM(2);
296 useRoi = GET_PARAM(3);
297 }
298
random_roicvtest::ocl::Integral299 virtual void random_roi()
300 {
301 ASSERT_EQ(CV_MAT_CN(type), 1);
302
303 Size roiSize = randomSize(1, MAX_VALUE), isize = Size(roiSize.width + 1, roiSize.height + 1);
304 Border srcBorder = randomBorder(0, useRoi ? 2 : 0);
305 randomSubMat(src, src_roi, roiSize, srcBorder, type, 5, 256);
306
307 Border dstBorder = randomBorder(0, useRoi ? 2 : 0);
308 randomSubMat(dst, dst_roi, isize, dstBorder, sdepth, 5, 16);
309
310 Border dst2Border = randomBorder(0, useRoi ? 2 : 0);
311 randomSubMat(dst2, dst2_roi, isize, dst2Border, sqdepth, 5, 16);
312
313 UMAT_UPLOAD_INPUT_PARAMETER(src);
314 UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
315 UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
316 }
317
Near2cvtest::ocl::Integral318 void Near2(double threshold = 0.0, bool relative = false)
319 {
320 if (relative)
321 OCL_EXPECT_MATS_NEAR_RELATIVE(dst2, threshold);
322 else
323 OCL_EXPECT_MATS_NEAR(dst2, threshold);
324 }
325 };
326
OCL_TEST_P(Integral,Mat1)327 OCL_TEST_P(Integral, Mat1)
328 {
329 for (int j = 0; j < test_loop_times; j++)
330 {
331 random_roi();
332
333 OCL_OFF(cv::integral(src_roi, dst_roi, sdepth));
334 OCL_ON(cv::integral(usrc_roi, udst_roi, sdepth));
335
336 Near();
337 }
338 }
339
OCL_TEST_P(Integral,Mat2)340 OCL_TEST_P(Integral, Mat2)
341 {
342 for (int j = 0; j < test_loop_times; j++)
343 {
344 random_roi();
345
346 OCL_OFF(cv::integral(src_roi, dst_roi, dst2_roi, sdepth, sqdepth));
347 OCL_ON(cv::integral(usrc_roi, udst_roi, udst2_roi, sdepth, sqdepth));
348
349 Near();
350 sqdepth == CV_32F ? Near2(1e-6, true) : Near2();
351 }
352 }
353
354 //////////////////////////////////////// threshold //////////////////////////////////////////////
355
356 struct Threshold :
357 public ImgprocTestBase
358 {
359 int thresholdType;
360
SetUpcvtest::ocl::Threshold361 virtual void SetUp()
362 {
363 type = GET_PARAM(0);
364 thresholdType = GET_PARAM(2);
365 useRoi = GET_PARAM(3);
366 }
367 };
368
OCL_TEST_P(Threshold,Mat)369 OCL_TEST_P(Threshold, Mat)
370 {
371 for (int j = 0; j < test_loop_times; j++)
372 {
373 random_roi();
374
375 double maxVal = randomDouble(20.0, 127.0);
376 double thresh = randomDouble(0.0, maxVal);
377
378 OCL_OFF(cv::threshold(src_roi, dst_roi, thresh, maxVal, thresholdType));
379 OCL_ON(cv::threshold(usrc_roi, udst_roi, thresh, maxVal, thresholdType));
380
381 Near(1);
382 }
383 }
384
385 /////////////////////////////////////////// CLAHE //////////////////////////////////////////////////
386
PARAM_TEST_CASE(CLAHETest,Size,double,bool)387 PARAM_TEST_CASE(CLAHETest, Size, double, bool)
388 {
389 Size gridSize;
390 double clipLimit;
391 bool useRoi;
392
393 TEST_DECLARE_INPUT_PARAMETER(src);
394 TEST_DECLARE_OUTPUT_PARAMETER(dst);
395
396 virtual void SetUp()
397 {
398 gridSize = GET_PARAM(0);
399 clipLimit = GET_PARAM(1);
400 useRoi = GET_PARAM(2);
401 }
402
403 void random_roi()
404 {
405 Size roiSize = randomSize(std::max(gridSize.height, gridSize.width), MAX_VALUE);
406 Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
407 randomSubMat(src, src_roi, roiSize, srcBorder, CV_8UC1, 5, 256);
408
409 Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
410 randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_8UC1, 5, 16);
411
412 UMAT_UPLOAD_INPUT_PARAMETER(src);
413 UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
414 }
415
416 void Near(double threshold = 0.0)
417 {
418 OCL_EXPECT_MATS_NEAR(dst, threshold);
419 }
420 };
421
OCL_TEST_P(CLAHETest,Accuracy)422 OCL_TEST_P(CLAHETest, Accuracy)
423 {
424 for (int i = 0; i < test_loop_times; ++i)
425 {
426 random_roi();
427
428 Ptr<CLAHE> clahe = cv::createCLAHE(clipLimit, gridSize);
429
430 OCL_OFF(clahe->apply(src_roi, dst_roi));
431 OCL_ON(clahe->apply(usrc_roi, udst_roi));
432
433 Near(1.0);
434 }
435 }
436
437 /////////////////////////////////////////////////////////////////////////////////////
438
439 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, EqualizeHist, Combine(
440 Values((MatType)CV_8UC1),
441 Values(0), // not used
442 Values(0), // not used
443 Bool()));
444
445 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CornerMinEigenVal, Combine(
446 Values((MatType)CV_8UC1, (MatType)CV_32FC1),
447 Values(3, 5),
448 Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
449 (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT101),
450 Bool()));
451
452 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CornerHarris, Combine(
453 Values((MatType)CV_8UC1, CV_32FC1),
454 Values(3, 5),
455 Values( (BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
456 (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT_101),
457 Bool()));
458
459 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, PreCornerDetect, Combine(
460 Values((MatType)CV_8UC1, CV_32FC1),
461 Values(3, 5),
462 Values( (BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
463 (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT_101),
464 Bool()));
465
466 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Integral, Combine(
467 Values((MatType)CV_8UC1), // TODO does not work with CV_32F, CV_64F
468 Values(CV_32SC1, CV_32FC1), // desired sdepth
469 Values(CV_32FC1, CV_64FC1), // desired sqdepth
470 Bool()));
471
472 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Threshold, Combine(
473 Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4,
474 CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4,
475 CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4),
476 Values(0),
477 Values(ThreshOp(THRESH_BINARY),
478 ThreshOp(THRESH_BINARY_INV), ThreshOp(THRESH_TRUNC),
479 ThreshOp(THRESH_TOZERO), ThreshOp(THRESH_TOZERO_INV)),
480 Bool()));
481
482 OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CLAHETest, Combine(
483 Values(Size(4, 4), Size(32, 8), Size(8, 64)),
484 Values(0.0, 10.0, 62.0, 300.0),
485 Bool()));
486
487 OCL_INSTANTIATE_TEST_CASE_P(ImgprocTestBase, CopyMakeBorder, Combine(
488 testing::Values((MatDepth)CV_8U, (MatDepth)CV_16S, (MatDepth)CV_32S, (MatDepth)CV_32F),
489 testing::Values(Channels(1), Channels(3), (Channels)4),
490 Bool(), // border isolated or not
491 Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE, (BorderType)BORDER_REFLECT,
492 (BorderType)BORDER_WRAP, (BorderType)BORDER_REFLECT_101),
493 Bool()));
494
495 } } // namespace cvtest::ocl
496
497 #endif // HAVE_OPENCL
498