further simplify the logics in filter tests
[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 "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         cv::RNG &rng = TS::ptr()->get_rng();
95         roicols = rng.uniform(2, mat1.cols);
96         roirows = rng.uniform(2, mat1.rows);
97         src1x   = rng.uniform(0, mat1.cols - roicols);
98         src1y   = rng.uniform(0, mat1.rows - roirows);
99         dstx    = rng.uniform(0, dst.cols  - roicols);
100         dsty    = rng.uniform(0, dst.rows  - roirows);
101 #else
102         roicols = mat1.cols;
103         roirows = mat1.rows;
104         src1x = 0;
105         src1y = 0;
106         dstx = 0;
107         dsty = 0;
108 #endif
109
110         mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
111         dst_roi  = dst(Rect(dstx, dsty, roicols, roirows));
112
113         gdst_whole = dst;
114         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
115
116         gmat1 = mat1_roi;
117     }
118
119     void Init(int mat_type)
120     {
121         cv::Size size(MWIDTH, MHEIGHT);
122         mat1 = randomMat(size, mat_type, 5, 16);
123         dst  = randomMat(size, mat_type, 5, 16);
124     }
125
126     void Near(double threshold)
127     {
128         cv::Mat cpu_dst;
129         gdst_whole.download(cpu_dst);
130         EXPECT_MAT_NEAR(dst, cpu_dst, threshold, "");
131     }
132 };
133
134 /////////////////////////////////////////////////////////////////////////////////////////////////
135 // blur
136 struct Blur : FilterTestBase
137 {
138     int type;
139     cv::Size ksize;
140     int bordertype;
141
142     virtual void SetUp()
143     {
144         type = GET_PARAM(0);
145         ksize = GET_PARAM(1);
146         bordertype = GET_PARAM(3);
147         Init(type);
148     }
149 };
150
151 TEST_P(Blur, Mat)
152 {
153     for(int j = 0; j < LOOP_TIMES; j++)
154     {
155         random_roi();
156         cv::blur(mat1_roi, dst_roi, ksize, Point(-1, -1), bordertype);
157         cv::ocl::blur(gmat1, gdst, ksize, Point(-1, -1), bordertype);
158         Near(1.0);
159     }
160 }
161
162
163 /////////////////////////////////////////////////////////////////////////////////////////////////
164 //Laplacian
165 struct Laplacian : FilterTestBase
166 {
167     int type;
168     cv::Size ksize;
169
170     virtual void SetUp()
171     {
172         type = GET_PARAM(0);
173         ksize = GET_PARAM(1);
174         Init(type);
175     }
176 };
177
178 TEST_P(Laplacian, Accuracy)
179 {
180     for(int j = 0; j < LOOP_TIMES; j++)
181     {
182         random_roi();
183         cv::Laplacian(mat1_roi, dst_roi, -1, ksize.width, 1);
184         cv::ocl::Laplacian(gmat1, gdst, -1, ksize.width, 1);
185         Near(1e-5);
186     }
187 }
188
189
190
191 /////////////////////////////////////////////////////////////////////////////////////////////////
192 // erode & dilate
193 struct ErodeDilate : FilterTestBase
194 {
195     int type;
196     int iterations;
197
198     //erode or dilate kernel
199     cv::Mat kernel;
200
201     virtual void SetUp()
202     {
203         type = GET_PARAM(0);
204         iterations = GET_PARAM(3);
205         Init(type);
206         //              rng.fill(kernel, cv::RNG::UNIFORM, cv::Scalar::all(0), cv::Scalar::all(3));
207         kernel = randomMat(Size(3, 3), CV_8UC1, 0, 3);
208     }
209
210 };
211
212 TEST_P(ErodeDilate, Mat)
213 {
214     for(int j = 0; j < LOOP_TIMES; j++)
215     {
216         random_roi();
217         cv::erode(mat1_roi, dst_roi, kernel, Point(-1, -1), iterations);
218         cv::ocl::erode(gmat1, gdst, kernel, Point(-1, -1), iterations);
219         Near(1e-5);
220     }
221     for(int j = 0; j < LOOP_TIMES; j++)
222     {
223         random_roi();
224         cv::dilate(mat1_roi, dst_roi, kernel, Point(-1, -1), iterations);
225         cv::ocl::dilate(gmat1, gdst, kernel, Point(-1, -1), iterations);
226         Near(1e-5);
227     }
228 }
229
230
231 /////////////////////////////////////////////////////////////////////////////////////////////////
232 // Sobel
233 struct Sobel : FilterTestBase
234 {
235     int type;
236     int dx, dy, ksize, bordertype;
237
238     virtual void SetUp()
239     {
240         type = GET_PARAM(0);
241         Size s = GET_PARAM(1);
242         ksize = s.width;
243         s = GET_PARAM(2);
244         dx = s.width;
245         dy = s.height;
246         bordertype = GET_PARAM(3);
247         Init(type);
248     }
249 };
250
251 TEST_P(Sobel, Mat)
252 {
253     for(int j = 0; j < LOOP_TIMES; j++)
254     {
255         random_roi();
256         cv::Sobel(mat1_roi, dst_roi, -1, dx, dy, ksize, /*scale*/0.00001,/*delta*/0, bordertype);
257         cv::ocl::Sobel(gmat1, gdst, -1, dx, dy, ksize,/*scale*/0.00001,/*delta*/0, bordertype);
258         Near(1);
259     }
260 }
261
262
263 /////////////////////////////////////////////////////////////////////////////////////////////////
264 // Scharr
265 struct Scharr : FilterTestBase
266 {
267     int type;
268     int dx, dy, bordertype;
269
270     virtual void SetUp()
271     {
272         type = GET_PARAM(0);
273         Size s = GET_PARAM(2);
274         dx = s.width;
275         dy = s.height;
276         bordertype = GET_PARAM(3);
277         Init(type);
278     }
279 };
280
281 TEST_P(Scharr, Mat)
282 {
283     for(int j = 0; j < LOOP_TIMES; j++)
284     {
285         random_roi();
286         cv::Scharr(mat1_roi, dst_roi, -1, dx, dy, /*scale*/1,/*delta*/0, bordertype);
287         cv::ocl::Scharr(gmat1, gdst, -1, dx, dy,/*scale*/1,/*delta*/0, bordertype);
288         Near(1);
289     }
290
291 }
292
293
294 /////////////////////////////////////////////////////////////////////////////////////////////////
295 // GaussianBlur
296 struct GaussianBlur : FilterTestBase
297 {
298     int type;
299     cv::Size ksize;
300     int bordertype;
301     double sigma1, sigma2;
302
303     virtual void SetUp()
304     {
305         type = GET_PARAM(0);
306         ksize = GET_PARAM(1);
307         bordertype = GET_PARAM(3);
308         Init(type);
309         cv::RNG &rng = TS::ptr()->get_rng();
310         sigma1 = rng.uniform(0.1, 1.0);
311         sigma2 = rng.uniform(0.1, 1.0);
312     }
313 };
314
315 TEST_P(GaussianBlur, Mat)
316 {
317     for(int j = 0; j < LOOP_TIMES; j++)
318     {
319         random_roi();
320         cv::GaussianBlur(mat1_roi, dst_roi, ksize, sigma1, sigma2, bordertype);
321         cv::ocl::GaussianBlur(gmat1, gdst, ksize, sigma1, sigma2, bordertype);
322         Near(1);
323     }
324
325 }
326
327
328
329 INSTANTIATE_TEST_CASE_P(Filter, Blur, Combine(
330                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
331                         Values(cv::Size(3, 3), cv::Size(5, 5), cv::Size(7, 7)),
332                         Values(Size(0, 0)), //not use
333                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE, (MatType)cv::BORDER_REFLECT, (MatType)cv::BORDER_REFLECT_101)));
334
335
336 INSTANTIATE_TEST_CASE_P(Filters, Laplacian, Combine(
337                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
338                         Values(Size(3, 3)),
339                         Values(Size(0, 0)), //not use
340                         Values(0)));        //not use
341
342 INSTANTIATE_TEST_CASE_P(Filter, ErodeDilate, Combine(
343                         Values(CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4), 
344                         Values(Size(0, 0)), //not use
345                         Values(Size(0, 0)), //not use
346                         Values(1)));
347
348
349 INSTANTIATE_TEST_CASE_P(Filter, Sobel, Combine(
350                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
351                         Values(Size(3, 3), Size(5, 5)),
352                         Values(Size(1, 0), Size(1, 1), Size(2, 0), Size(2, 1)),
353                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
354
355
356 INSTANTIATE_TEST_CASE_P(Filter, Scharr, Combine(
357                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
358                         Values(Size(0, 0)), //not use
359                         Values(Size(0, 1), Size(1, 0)), 
360                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
361
362 INSTANTIATE_TEST_CASE_P(Filter, GaussianBlur, Combine(
363                         Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
364                         Values(Size(3, 3), Size(5, 5)),
365                         Values(Size(0, 0)), //not use
366                         Values((MatType)cv::BORDER_CONSTANT, (MatType)cv::BORDER_REPLICATE)));
367
368
369
370 #endif // HAVE_OPENCL