optimized some operations
[profile/ivi/opencv.git] / modules / imgproc / test / ocl / 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 materials 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 #include "cvconfig.h"
53 #include "opencv2/ts/ocl_test.hpp"
54
55 #ifdef HAVE_OPENCL
56
57 namespace cvtest {
58 namespace ocl {
59
60 PARAM_TEST_CASE(FilterTestBase, MatType,
61                 int, // kernel size
62                 Size, // dx, dy
63                 BorderType, // border type
64                 double, // optional parameter
65                 bool, // roi or not
66                 int)  // width multiplier
67 {
68     int type, borderType, ksize;
69     Size size;
70     double param;
71     bool useRoi;
72     int widthMultiple;
73
74     TEST_DECLARE_INPUT_PARAMETER(src);
75     TEST_DECLARE_OUTPUT_PARAMETER(dst);
76
77     virtual void SetUp()
78     {
79         type = GET_PARAM(0);
80         ksize = GET_PARAM(1);
81         size = GET_PARAM(2);
82         borderType = GET_PARAM(3);
83         param = GET_PARAM(4);
84         useRoi = GET_PARAM(5);
85         widthMultiple = GET_PARAM(6);
86     }
87
88     void random_roi(int minSize = 1)
89     {
90         if (minSize == 0)
91             minSize = ksize;
92
93         Size roiSize = randomSize(minSize, MAX_VALUE);
94         roiSize.width &= ~((widthMultiple * 2) - 1);
95         roiSize.width += widthMultiple;
96
97         Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
98         randomSubMat(src, src_roi, roiSize, srcBorder, type, 5, 256);
99
100         Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
101         randomSubMat(dst, dst_roi, roiSize, dstBorder, type, -60, 70);
102
103         UMAT_UPLOAD_INPUT_PARAMETER(src);
104         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
105     }
106
107     void Near()
108     {
109         int depth = CV_MAT_DEPTH(type);
110         bool isFP = depth >= CV_32F;
111
112         if (isFP)
113             Near(1e-6, true);
114         else
115             Near(1, false);
116     }
117
118     void Near(double threshold, bool relative)
119     {
120         if (relative)
121             OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
122         else
123             OCL_EXPECT_MATS_NEAR(dst, threshold);
124     }
125 };
126
127 ////////////////////////////////////////////////////////////////////////////////////////////////////
128 // Bilateral
129
130 typedef FilterTestBase Bilateral;
131
132 OCL_TEST_P(Bilateral, Mat)
133 {
134     for (int j = 0; j < test_loop_times; j++)
135     {
136         random_roi();
137
138         double sigmacolor = rng.uniform(20, 100);
139         double sigmaspace = rng.uniform(10, 40);
140
141         OCL_OFF(cv::bilateralFilter(src_roi, dst_roi, ksize, sigmacolor, sigmaspace, borderType));
142         OCL_ON(cv::bilateralFilter(usrc_roi, udst_roi, ksize, sigmacolor, sigmaspace, borderType));
143
144         Near();
145     }
146 }
147
148 /////////////////////////////////////////////////////////////////////////////////////////////////
149 // Laplacian
150
151 typedef FilterTestBase LaplacianTest;
152
153 OCL_TEST_P(LaplacianTest, Accuracy)
154 {
155     double scale = param;
156
157     for (int j = 0; j < test_loop_times; j++)
158     {
159         random_roi();
160
161         OCL_OFF(cv::Laplacian(src_roi, dst_roi, -1, ksize, scale, 10, borderType));
162         OCL_ON(cv::Laplacian(usrc_roi, udst_roi, -1, ksize, scale, 10, borderType));
163
164         Near();
165     }
166 }
167
168
169 /////////////////////////////////////////////////////////////////////////////////////////////////
170 // Sobel
171
172 typedef FilterTestBase SobelTest;
173
174 OCL_TEST_P(SobelTest, Mat)
175 {
176     int dx = size.width, dy = size.height;
177     double scale = param;
178
179     for (int j = 0; j < test_loop_times; j++)
180     {
181         random_roi();
182
183         OCL_OFF(cv::Sobel(src_roi, dst_roi, -1, dx, dy, ksize, scale, /* delta */0, borderType));
184         OCL_ON(cv::Sobel(usrc_roi, udst_roi, -1, dx, dy, ksize, scale, /* delta */0, borderType));
185
186         Near();
187     }
188 }
189
190 /////////////////////////////////////////////////////////////////////////////////////////////////
191 // Scharr
192
193 typedef FilterTestBase ScharrTest;
194
195 OCL_TEST_P(ScharrTest, Mat)
196 {
197     int dx = size.width, dy = size.height;
198     double scale = param;
199
200     for (int j = 0; j < test_loop_times; j++)
201     {
202         random_roi();
203
204         OCL_OFF(cv::Scharr(src_roi, dst_roi, -1, dx, dy, scale, /* delta */ 0, borderType));
205         OCL_ON(cv::Scharr(usrc_roi, udst_roi, -1, dx, dy, scale, /* delta */ 0, borderType));
206
207         Near();
208     }
209 }
210
211 /////////////////////////////////////////////////////////////////////////////////////////////////
212 // GaussianBlur
213
214 typedef FilterTestBase GaussianBlurTest;
215
216 OCL_TEST_P(GaussianBlurTest, Mat)
217 {
218     for (int j = 0; j < test_loop_times + 1; j++)
219     {
220         random_roi();
221
222         double sigma1 = rng.uniform(0.1, 1.0);
223         double sigma2 = j % 2 == 0 ? sigma1 : rng.uniform(0.1, 1.0);
224
225         OCL_OFF(cv::GaussianBlur(src_roi, dst_roi, Size(ksize, ksize), sigma1, sigma2, borderType));
226         OCL_ON(cv::GaussianBlur(usrc_roi, udst_roi, Size(ksize, ksize), sigma1, sigma2, borderType));
227
228         Near(CV_MAT_DEPTH(type) >= CV_32F ? 5e-5 : 1, false);
229     }
230 }
231
232 /////////////////////////////////////////////////////////////////////////////////////////////////
233 // Erode
234
235 typedef FilterTestBase Erode;
236
237 OCL_TEST_P(Erode, Mat)
238 {
239     Size kernelSize(ksize, ksize);
240     int iterations = (int)param;
241
242     for (int j = 0; j < test_loop_times; j++)
243     {
244         random_roi();
245         Mat kernel = randomMat(kernelSize, CV_8UC1, 0, 3);
246
247         OCL_OFF(cv::erode(src_roi, dst_roi, kernel, Point(-1, -1), iterations) );
248         OCL_ON(cv::erode(usrc_roi, udst_roi, kernel, Point(-1, -1), iterations) );
249
250         Near();
251     }
252 }
253
254 /////////////////////////////////////////////////////////////////////////////////////////////////
255 // Dilate
256
257 typedef FilterTestBase Dilate;
258
259 OCL_TEST_P(Dilate, Mat)
260 {
261     Size kernelSize(ksize, ksize);
262     int iterations = (int)param;
263
264     for (int j = 0; j < test_loop_times; j++)
265     {
266         random_roi();
267         Mat kernel = randomMat(kernelSize, CV_8UC1, 0, 3);
268
269         OCL_OFF(cv::dilate(src_roi, dst_roi, kernel, Point(-1, -1), iterations) );
270         OCL_ON(cv::dilate(usrc_roi, udst_roi, kernel, Point(-1, -1), iterations) );
271
272         Near();
273     }
274 }
275
276 /////////////////////////////////////////////////////////////////////////////////////////////////
277 // MorphologyEx
278
279 typedef FilterTestBase MorphologyEx;
280
281 OCL_TEST_P(MorphologyEx, Mat)
282 {
283     Size kernelSize(ksize, ksize);
284     int iterations = (int)param;
285     int op = size.height;
286
287     for (int j = 0; j < test_loop_times; j++)
288     {
289         random_roi();
290         Mat kernel = randomMat(kernelSize, CV_8UC1, 0, 3);
291
292         OCL_OFF(cv::morphologyEx(src_roi, dst_roi, op, kernel, Point(-1, -1), iterations) );
293         OCL_ON(cv::morphologyEx(usrc_roi, udst_roi, op, kernel, Point(-1, -1), iterations) );
294
295         Near();
296     }
297 }
298
299 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
300
301 #define FILTER_BORDER_SET_NO_ISOLATED \
302     Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE, (BorderType)BORDER_REFLECT, (BorderType)BORDER_WRAP, (BorderType)BORDER_REFLECT_101/*, \
303             (int)BORDER_CONSTANT|BORDER_ISOLATED, (int)BORDER_REPLICATE|BORDER_ISOLATED, \
304             (int)BORDER_REFLECT|BORDER_ISOLATED, (int)BORDER_WRAP|BORDER_ISOLATED, \
305             (int)BORDER_REFLECT_101|BORDER_ISOLATED*/) // WRAP and ISOLATED are not supported by cv:: version
306
307 #define FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED \
308     Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE, (BorderType)BORDER_REFLECT, /*(int)BORDER_WRAP,*/ (BorderType)BORDER_REFLECT_101/*, \
309             (int)BORDER_CONSTANT|BORDER_ISOLATED, (int)BORDER_REPLICATE|BORDER_ISOLATED, \
310             (int)BORDER_REFLECT|BORDER_ISOLATED, (int)BORDER_WRAP|BORDER_ISOLATED, \
311             (int)BORDER_REFLECT_101|BORDER_ISOLATED*/) // WRAP and ISOLATED are not supported by cv:: version
312
313 #define FILTER_TYPES Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_16UC1, CV_16UC3, CV_16UC4, CV_32FC1, CV_32FC3, CV_32FC4)
314
315 OCL_INSTANTIATE_TEST_CASE_P(Filter, Bilateral, Combine(
316                             Values(CV_8UC1, CV_8UC3),
317                             Values(5, 9), // kernel size
318                             Values(Size(0, 0)), // not used
319                             FILTER_BORDER_SET_NO_ISOLATED,
320                             Values(0.0), // not used
321                             Bool(),
322                             Values(1, 4)));
323
324 OCL_INSTANTIATE_TEST_CASE_P(Filter, LaplacianTest, Combine(
325                             FILTER_TYPES,
326                             Values(1, 3, 5), // kernel size
327                             Values(Size(0, 0)), // not used
328                             FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
329                             Values(1.0, 0.2, 3.0), // kernel scale
330                             Bool(),
331                             Values(1))); // not used
332
333 OCL_INSTANTIATE_TEST_CASE_P(Filter, SobelTest, Combine(
334                             FILTER_TYPES,
335                             Values(3, 5), // kernel size
336                             Values(Size(1, 0), Size(1, 1), Size(2, 0), Size(2, 1)), // dx, dy
337                             FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
338                             Values(0.0), // not used
339                             Bool(),
340                             Values(1))); // not used
341
342 OCL_INSTANTIATE_TEST_CASE_P(Filter, ScharrTest, Combine(
343                             FILTER_TYPES,
344                             Values(0), // not used
345                             Values(Size(0, 1), Size(1, 0)), // dx, dy
346                             FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
347                             Values(1.0, 0.2), // kernel scale
348                             Bool(),
349                             Values(1))); // not used
350
351 OCL_INSTANTIATE_TEST_CASE_P(Filter, GaussianBlurTest, Combine(
352                             FILTER_TYPES,
353                             Values(3, 5), // kernel size
354                             Values(Size(0, 0)), // not used
355                             FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
356                             Values(0.0), // not used
357                             Bool(),
358                             Values(1))); // not used
359
360 OCL_INSTANTIATE_TEST_CASE_P(Filter, Erode, Combine(
361                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4, CV_64FC1, CV_64FC4),
362                             Values(3, 5, 7),
363                             Values(Size(0, 0)), //not used
364                             Values((BorderType)BORDER_CONSTANT),
365                             Values(1.0, 2.0, 3.0),
366                             Bool(),
367                             Values(1))); // not used
368
369 OCL_INSTANTIATE_TEST_CASE_P(Filter, Dilate, Combine(
370                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4, CV_64FC1, CV_64FC4),
371                             Values(3, 5, 7),
372                             Values(Size(0, 0)), // not used
373                             Values((BorderType)BORDER_CONSTANT),
374                             Values(1.0, 2.0, 3.0),
375                             Bool(),
376                             Values(1))); // not used
377
378 OCL_INSTANTIATE_TEST_CASE_P(Filter, MorphologyEx, Combine(
379                             Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
380                             Values(3, 5, 7),
381                             Values(Size(0, 2), Size(0, 3), Size(0, 4), Size(0, 5), Size(0, 6)), // used as generator of operations
382                             Values((BorderType)BORDER_CONSTANT),
383                             Values(1.0, 2.0, 3.0),
384                             Bool(),
385                             Values(1))); // not used
386
387
388 } } // namespace cvtest::ocl
389
390 #endif // HAVE_OPENCL