Merge pull request #1574 from alalek:svm_workaround
[profile/ivi/opencv.git] / modules / ocl / test / test_filters.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, 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 //    Zero Lin, Zero.Lin@amd.com
22 //    Zhang Ying, zhangying913@gmail.com
23 //    Yao Wang, bitwangyaoyao@gmail.com
24 //
25 // Redistribution and use in source and binary forms, with or without modification,
26 // are permitted provided that the following conditions are met:
27 //
28 //   * Redistribution's of source code must retain the above copyright notice,
29 //     this list of conditions and the following disclaimer.
30 //
31 //   * Redistribution's in binary form must reproduce the above copyright notice,
32 //     this list of conditions and the following disclaimer in the documentation
33 //     and/or other oclMaterials provided with the distribution.
34 //
35 //   * The name of the copyright holders may not be used to endorse or promote products
36 //     derived from this software without specific prior written permission.
37 //
38 // This software is provided by the copyright holders and contributors "as is" and
39 // any express or implied warranties, including, but not limited to, the implied
40 // warranties of merchantability and fitness for a particular purpose are disclaimed.
41 // In no event shall the Intel Corporation or contributors be liable for any direct,
42 // indirect, incidental, special, exemplary, or consequential damages
43 // (including, but not limited to, procurement of substitute goods or services;
44 // loss of use, data, or profits; or business interruption) however caused
45 // and on any theory of liability, whether in contract, strict liability,
46 // or tort (including negligence or otherwise) arising in any way out of
47 // the use of this software, even if advised of the possibility of such damage.
48 //
49 //M*/
50
51 #include "test_precomp.hpp"
52
53 #ifdef HAVE_OPENCL
54
55 using namespace cvtest;
56 using namespace testing;
57 using namespace std;
58
59
60 PARAM_TEST_CASE(FilterTestBase,
61                 MatType,
62                 cv::Size, // kernel size
63                 cv::Size, // dx,dy
64                 int       // border type, or iteration
65                 )
66 {
67     //src mat
68     cv::Mat mat1;
69     cv::Mat dst;
70
71     // set up roi
72     int roicols;
73     int roirows;
74     int src1x;
75     int src1y;
76     int dstx;
77     int dsty;
78
79     //src mat with roi
80     cv::Mat mat1_roi;
81     cv::Mat dst_roi;
82
83     //ocl dst mat for testing
84     cv::ocl::oclMat gdst_whole;
85
86     //ocl mat with roi
87     cv::ocl::oclMat gmat1;
88     cv::ocl::oclMat gdst;
89
90     void random_roi()
91     {
92 #ifdef RANDOMROI
93         //randomize ROI
94         roicols = rng.uniform(2, mat1.cols);
95         roirows = rng.uniform(2, mat1.rows);
96         src1x   = rng.uniform(0, mat1.cols - roicols);
97         src1y   = rng.uniform(0, mat1.rows - roirows);
98         dstx    = rng.uniform(0, dst.cols  - roicols);
99         dsty    = rng.uniform(0, dst.rows  - roirows);
100 #else
101         roicols = mat1.cols;
102         roirows = mat1.rows;
103         src1x = 0;
104         src1y = 0;
105         dstx = 0;
106         dsty = 0;
107 #endif
108
109         mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
110         dst_roi  = dst(Rect(dstx, dsty, roicols, roirows));
111
112         gdst_whole = dst;
113         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
114
115         gmat1 = mat1_roi;
116     }
117
118     void Init(int mat_type)
119     {
120         cv::Size size(MWIDTH, MHEIGHT);
121         mat1 = randomMat(size, mat_type, 5, 16);
122         dst  = randomMat(size, mat_type, 5, 16);
123     }
124
125     void Near(double threshold)
126     {
127         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), threshold);
128     }
129 };
130
131 /////////////////////////////////////////////////////////////////////////////////////////////////
132 // blur
133 struct Blur : FilterTestBase
134 {
135     int type;
136     cv::Size ksize;
137     int bordertype;
138
139     virtual void SetUp()
140     {
141         type = GET_PARAM(0);
142         ksize = GET_PARAM(1);
143         bordertype = GET_PARAM(3);
144         Init(type);
145     }
146 };
147
148 TEST_P(Blur, Mat)
149 {
150     for(int j = 0; j < LOOP_TIMES; j++)
151     {
152         random_roi();
153         cv::blur(mat1_roi, dst_roi, ksize, Point(-1, -1), bordertype);
154         cv::ocl::blur(gmat1, gdst, ksize, Point(-1, -1), bordertype);
155         Near(1.0);
156     }
157 }
158
159
160 /////////////////////////////////////////////////////////////////////////////////////////////////
161 //Laplacian
162 struct Laplacian : FilterTestBase
163 {
164     int type;
165     cv::Size ksize;
166
167     virtual void SetUp()
168     {
169         type = GET_PARAM(0);
170         ksize = GET_PARAM(1);
171         Init(type);
172     }
173 };
174
175 TEST_P(Laplacian, Accuracy)
176 {
177     for(int j = 0; j < LOOP_TIMES; j++)
178     {
179         random_roi();
180         cv::Laplacian(mat1_roi, dst_roi, -1, ksize.width, 1);
181         cv::ocl::Laplacian(gmat1, gdst, -1, ksize.width, 1);
182         Near(1e-5);
183     }
184 }
185
186
187
188 /////////////////////////////////////////////////////////////////////////////////////////////////
189 // erode & dilate
190 struct ErodeDilate : FilterTestBase
191 {
192     int type;
193     int iterations;
194
195     //erode or dilate kernel
196     cv::Mat kernel;
197
198     virtual void SetUp()
199     {
200         type = GET_PARAM(0);
201         iterations = GET_PARAM(3);
202         Init(type);
203         kernel = randomMat(Size(3, 3), CV_8UC1, 0, 3);
204     }
205
206 };
207
208 TEST_P(ErodeDilate, Mat)
209 {
210     for(int j = 0; j < LOOP_TIMES; j++)
211     {
212         random_roi();
213         cv::erode(mat1_roi, dst_roi, kernel, Point(-1, -1), iterations);
214         cv::ocl::erode(gmat1, gdst, kernel, Point(-1, -1), iterations);
215         Near(1e-5);
216     }
217     for(int j = 0; j < LOOP_TIMES; j++)
218     {
219         random_roi();
220         cv::dilate(mat1_roi, dst_roi, kernel, Point(-1, -1), iterations);
221         cv::ocl::dilate(gmat1, gdst, kernel, Point(-1, -1), iterations);
222         Near(1e-5);
223     }
224 }
225
226
227 /////////////////////////////////////////////////////////////////////////////////////////////////
228 // Sobel
229 struct Sobel : FilterTestBase
230 {
231     int type;
232     int dx, dy, ksize, bordertype;
233
234     virtual void SetUp()
235     {
236         type = GET_PARAM(0);
237         Size s = GET_PARAM(1);
238         ksize = s.width;
239         s = GET_PARAM(2);
240         dx = s.width;
241         dy = s.height;
242         bordertype = GET_PARAM(3);
243         Init(type);
244     }
245 };
246
247 TEST_P(Sobel, Mat)
248 {
249     for(int j = 0; j < LOOP_TIMES; j++)
250     {
251         random_roi();
252         cv::Sobel(mat1_roi, dst_roi, -1, dx, dy, ksize, /*scale*/0.00001,/*delta*/0, bordertype);
253         cv::ocl::Sobel(gmat1, gdst, -1, dx, dy, ksize,/*scale*/0.00001,/*delta*/0, bordertype);
254         Near(1);
255     }
256 }
257
258
259 /////////////////////////////////////////////////////////////////////////////////////////////////
260 // Scharr
261 struct Scharr : FilterTestBase
262 {
263     int type;
264     int dx, dy, bordertype;
265
266     virtual void SetUp()
267     {
268         type = GET_PARAM(0);
269         Size s = GET_PARAM(2);
270         dx = s.width;
271         dy = s.height;
272         bordertype = GET_PARAM(3);
273         Init(type);
274     }
275 };
276
277 TEST_P(Scharr, Mat)
278 {
279     for(int j = 0; j < LOOP_TIMES; j++)
280     {
281         random_roi();
282         cv::Scharr(mat1_roi, dst_roi, -1, dx, dy, /*scale*/1,/*delta*/0, bordertype);
283         cv::ocl::Scharr(gmat1, gdst, -1, dx, dy,/*scale*/1,/*delta*/0, bordertype);
284         Near(1);
285     }
286
287 }
288
289
290 /////////////////////////////////////////////////////////////////////////////////////////////////
291 // GaussianBlur
292 struct GaussianBlur : FilterTestBase
293 {
294     int type;
295     cv::Size ksize;
296     int bordertype;
297     double sigma1, sigma2;
298
299     virtual void SetUp()
300     {
301         type = GET_PARAM(0);
302         ksize = GET_PARAM(1);
303         bordertype = GET_PARAM(3);
304         Init(type);
305         sigma1 = rng.uniform(0.1, 1.0);
306         sigma2 = rng.uniform(0.1, 1.0);
307     }
308 };
309
310 TEST_P(GaussianBlur, Mat)
311 {
312     for(int j = 0; j < LOOP_TIMES; j++)
313     {
314         random_roi();
315         cv::GaussianBlur(mat1_roi, dst_roi, ksize, sigma1, sigma2, bordertype);
316         cv::ocl::GaussianBlur(gmat1, gdst, ksize, sigma1, sigma2, bordertype);
317         Near(1);
318     }
319
320 }
321
322
323
324 ////////////////////////////////////////////////////////////////////////////////////////////////////
325 // Filter2D
326 struct Filter2D : FilterTestBase
327 {
328     int type;
329     cv::Size ksize;
330     int bordertype;
331     Point anchor;
332     virtual void SetUp()
333     {
334         type = GET_PARAM(0);
335         ksize = GET_PARAM(1);
336         bordertype = GET_PARAM(3);
337         Init(type);
338         anchor = Point(-1,-1);
339     }
340 };
341
342 TEST_P(Filter2D, Mat)
343 {
344     cv::Mat kernel = randomMat(cv::Size(ksize.width, ksize.height), CV_32FC1, 0.0, 1.0);
345     for(int j = 0; j < LOOP_TIMES; j++)
346     {
347         random_roi();
348         cv::filter2D(mat1_roi, dst_roi, -1, kernel, anchor, 0.0, bordertype);
349         cv::ocl::filter2D(gmat1, gdst, -1, kernel, anchor, bordertype);
350         Near(1);
351     }
352 }
353 ////////////////////////////////////////////////////////////////////////////////////////////////////
354 // Bilateral
355 struct Bilateral : FilterTestBase
356 {
357     int type;
358     cv::Size ksize;
359     int bordertype;
360     double sigmacolor, sigmaspace;
361
362     virtual void SetUp()
363     {
364         type = GET_PARAM(0);
365         ksize = GET_PARAM(1);
366         bordertype = GET_PARAM(3);
367         Init(type);
368         sigmacolor = rng.uniform(20, 100);
369         sigmaspace = rng.uniform(10, 40);
370     }
371 };
372
373 TEST_P(Bilateral, Mat)
374 {
375     for(int j = 0; j < LOOP_TIMES; j++)
376     {
377         random_roi();
378         cv::bilateralFilter(mat1_roi, dst_roi, ksize.width, sigmacolor, sigmaspace, bordertype);
379         cv::ocl::bilateralFilter(gmat1, gdst, ksize.width, sigmacolor, sigmaspace, bordertype);
380         Near(1);
381     }
382
383 }
384
385 ////////////////////////////////////////////////////////////////////////////////////////////////////
386 // AdaptiveBilateral
387 struct AdaptiveBilateral : FilterTestBase
388 {
389     int type;
390     cv::Size ksize;
391     int bordertype;
392     Point anchor;
393     virtual void SetUp()
394     {
395         type = GET_PARAM(0);
396         ksize = GET_PARAM(1);
397         bordertype = GET_PARAM(3);
398         Init(type);
399         anchor = Point(-1,-1);
400     }
401 };
402
403 TEST_P(AdaptiveBilateral, Mat)
404 {
405     for(int j = 0; j < LOOP_TIMES; j++)
406     {
407         random_roi();
408         cv::adaptiveBilateralFilter(mat1_roi, dst_roi, ksize, 5, anchor, bordertype);
409         cv::ocl::adaptiveBilateralFilter(gmat1, gdst, ksize, 5, anchor, bordertype);
410         Near(1);
411     }
412
413 }
414
415 INSTANTIATE_TEST_CASE_P(Filter, Blur, Combine(
416                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
417                         Values(cv::Size(3, 3), cv::Size(5, 5), cv::Size(7, 7)),
418                         Values(Size(0, 0)), //not use
419                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE, (MatType)cv::BORDER_REFLECT, (MatType)cv::BORDER_REFLECT_101)));
420
421
422 INSTANTIATE_TEST_CASE_P(Filter, Laplacian, Combine(
423                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
424                         Values(Size(3, 3)),
425                         Values(Size(0, 0)), //not use
426                         Values(0)));        //not use
427
428 INSTANTIATE_TEST_CASE_P(Filter, ErodeDilate, Combine(
429                         Values(CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
430                         Values(Size(0, 0)), //not use
431                         Values(Size(0, 0)), //not use
432                         Values(1)));
433
434
435 INSTANTIATE_TEST_CASE_P(Filter, Sobel, Combine(
436                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
437                         Values(Size(3, 3), Size(5, 5)),
438                         Values(Size(1, 0), Size(1, 1), Size(2, 0), Size(2, 1)),
439                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
440
441
442 INSTANTIATE_TEST_CASE_P(Filter, Scharr, Combine(
443                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
444                         Values(Size(0, 0)), //not use
445                         Values(Size(0, 1), Size(1, 0)),
446                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
447
448 INSTANTIATE_TEST_CASE_P(Filter, GaussianBlur, Combine(
449                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
450                         Values(Size(3, 3), Size(5, 5)),
451                         Values(Size(0, 0)), //not use
452                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
453
454
455
456 INSTANTIATE_TEST_CASE_P(Filter, Filter2D, testing::Combine(
457                         Values(CV_8UC1, CV_32FC1, CV_32FC4),
458                         Values(Size(3, 3), Size(15, 15), Size(25, 25)),
459                         Values(Size(0, 0)), //not use
460                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REFLECT101, (MatType)cv::BORDER_REPLICATE, (MatType)cv::BORDER_REFLECT)));
461
462 INSTANTIATE_TEST_CASE_P(Filter, Bilateral, Combine(
463                         Values(CV_8UC1, CV_8UC3),
464                         Values(Size(5, 5), Size(9, 9)),
465                         Values(Size(0, 0)), //not use
466                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE,
467                                (MatType)cv::BORDER_REFLECT, (MatType)cv::BORDER_WRAP, (MatType)cv::BORDER_REFLECT_101)));
468
469 INSTANTIATE_TEST_CASE_P(Filter, AdaptiveBilateral, Combine(
470                         Values(CV_8UC1, CV_8UC3),
471                         Values(Size(5, 5), Size(9, 9)),
472                         Values(Size(0, 0)), //not use
473                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE,
474                                (MatType)cv::BORDER_REFLECT,  (MatType)cv::BORDER_REFLECT_101)));
475 #endif // HAVE_OPENCL