CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / perf / perf_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, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 #include "precomp.hpp"
47
48 ///////////// Blur////////////////////////
49 PERFTEST(Blur)
50 {
51     Mat src1, dst, ocl_dst;
52     ocl::oclMat d_src1, d_dst;
53
54     Size ksize = Size(3, 3);
55     int bordertype = BORDER_CONSTANT;
56     int all_type[] = {CV_8UC1, CV_8UC4};
57     std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
58
59     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
60     {
61         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
62         {
63             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
64
65             gen(src1, size, size, all_type[j], 0, 256);
66             gen(dst, size, size, all_type[j], 0, 256);
67
68             blur(src1, dst, ksize, Point(-1, -1), bordertype);
69
70             CPU_ON;
71             blur(src1, dst, ksize, Point(-1, -1), bordertype);
72             CPU_OFF;
73
74             d_src1.upload(src1);
75
76             WARMUP_ON;
77             ocl::blur(d_src1, d_dst, ksize, Point(-1, -1), bordertype);
78             WARMUP_OFF;
79
80             GPU_ON;
81             ocl::blur(d_src1, d_dst, ksize, Point(-1, -1), bordertype);
82             GPU_OFF;
83
84             GPU_FULL_ON;
85             d_src1.upload(src1);
86             ocl::blur(d_src1, d_dst, ksize, Point(-1, -1), bordertype);
87             d_dst.download(ocl_dst);
88             GPU_FULL_OFF;
89
90             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1.0);
91         }
92
93     }
94 }
95 ///////////// Laplacian////////////////////////
96 PERFTEST(Laplacian)
97 {
98     Mat src1, dst, ocl_dst;
99     ocl::oclMat d_src1, d_dst;
100
101     int ksize = 3;
102     int all_type[] = {CV_8UC1, CV_8UC4};
103     std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
104
105     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
106     {
107         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
108         {
109             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
110
111             gen(src1, size, size, all_type[j], 0, 256);
112             gen(dst, size, size, all_type[j], 0, 256);
113
114             Laplacian(src1, dst, -1, ksize, 1);
115
116             CPU_ON;
117             Laplacian(src1, dst, -1, ksize, 1);
118             CPU_OFF;
119
120             d_src1.upload(src1);
121
122             WARMUP_ON;
123             ocl::Laplacian(d_src1, d_dst, -1, ksize, 1);
124             WARMUP_OFF;
125
126             GPU_ON;
127             ocl::Laplacian(d_src1, d_dst, -1, ksize, 1);
128             GPU_OFF;
129
130             GPU_FULL_ON;
131             d_src1.upload(src1);
132             ocl::Laplacian(d_src1, d_dst, -1, ksize, 1);
133             d_dst.download(ocl_dst);
134             GPU_FULL_OFF;
135
136             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1e-5);
137         }
138
139     }
140 }
141
142 ///////////// Erode ////////////////////
143 PERFTEST(Erode)
144 {
145     Mat src, dst, ker, ocl_dst;
146     ocl::oclMat d_src, d_dst;
147
148     int all_type[] = {CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4};
149     std::string type_name[] = {"CV_8UC1", "CV_8UC4", "CV_32FC1", "CV_32FC4"};
150
151     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
152     {
153         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
154         {
155             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
156
157             gen(src, size, size, all_type[j], Scalar::all(0), Scalar::all(256));
158             ker = getStructuringElement(MORPH_RECT, Size(3, 3));
159
160             erode(src, dst, ker);
161
162             CPU_ON;
163             erode(src, dst, ker);
164             CPU_OFF;
165
166             d_src.upload(src);
167
168             WARMUP_ON;
169             ocl::erode(d_src, d_dst, ker);
170             WARMUP_OFF;
171
172             GPU_ON;
173             ocl::erode(d_src, d_dst, ker);
174             GPU_OFF;
175
176             GPU_FULL_ON;
177             d_src.upload(src);
178             ocl::erode(d_src, d_dst, ker);
179             d_dst.download(ocl_dst);
180             GPU_FULL_OFF;
181
182             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1e-5);
183         }
184
185     }
186 }
187
188 ///////////// Sobel ////////////////////////
189 PERFTEST(Sobel)
190 {
191     Mat src, dst, ocl_dst;
192     ocl::oclMat d_src, d_dst;
193
194     int dx = 1;
195     int dy = 1;
196     int all_type[] = {CV_8UC1, CV_8UC4};
197     std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
198
199     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
200     {
201         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
202         {
203             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
204
205             gen(src, size, size, all_type[j], 0, 256);
206
207             Sobel(src, dst, -1, dx, dy);
208
209             CPU_ON;
210             Sobel(src, dst, -1, dx, dy);
211             CPU_OFF;
212
213             d_src.upload(src);
214
215             WARMUP_ON;
216             ocl::Sobel(d_src, d_dst, -1, dx, dy);
217             WARMUP_OFF;
218
219             GPU_ON;
220             ocl::Sobel(d_src, d_dst, -1, dx, dy);
221             GPU_OFF;
222
223             GPU_FULL_ON;
224             d_src.upload(src);
225             ocl::Sobel(d_src, d_dst, -1, dx, dy);
226             d_dst.download(ocl_dst);
227             GPU_FULL_OFF;
228
229             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1);
230         }
231
232     }
233 }
234 ///////////// Scharr ////////////////////////
235 PERFTEST(Scharr)
236 {
237     Mat src, dst, ocl_dst;
238     ocl::oclMat d_src, d_dst;
239
240     int dx = 1;
241     int dy = 0;
242     int all_type[] = {CV_8UC1, CV_8UC4};
243     std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
244
245     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
246     {
247         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
248         {
249             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
250
251             gen(src, size, size, all_type[j], 0, 256);
252
253             Scharr(src, dst, -1, dx, dy);
254
255             CPU_ON;
256             Scharr(src, dst, -1, dx, dy);
257             CPU_OFF;
258
259             d_src.upload(src);
260
261             WARMUP_ON;
262             ocl::Scharr(d_src, d_dst, -1, dx, dy);
263             WARMUP_OFF;
264
265             GPU_ON;
266             ocl::Scharr(d_src, d_dst, -1, dx, dy);
267             GPU_OFF;
268
269             GPU_FULL_ON;
270             d_src.upload(src);
271             ocl::Scharr(d_src, d_dst, -1, dx, dy);
272             d_dst.download(ocl_dst);
273             GPU_FULL_OFF;
274
275             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1);
276         }
277
278     }
279 }
280
281 ///////////// GaussianBlur ////////////////////////
282 PERFTEST(GaussianBlur)
283 {
284     Mat src, dst, ocl_dst;
285     int all_type[] = {CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4};
286     std::string type_name[] = {"CV_8UC1", "CV_8UC4", "CV_32FC1", "CV_32FC4"};
287
288     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
289     {
290         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
291         {
292             SUBTEST << size << 'x' << size << "; " << type_name[j] ;
293
294             gen(src, size, size, all_type[j], 5, 16);
295
296             GaussianBlur(src, dst, Size(9, 9), 0);
297
298             CPU_ON;
299             GaussianBlur(src, dst, Size(9, 9), 0);
300             CPU_OFF;
301
302             ocl::oclMat d_src(src);
303             ocl::oclMat d_dst(src.size(), src.type());
304             ocl::oclMat d_buf;
305
306             WARMUP_ON;
307             ocl::GaussianBlur(d_src, d_dst, Size(9, 9), 0);
308             WARMUP_OFF;
309
310             GPU_ON;
311             ocl::GaussianBlur(d_src, d_dst, Size(9, 9), 0);
312             GPU_OFF;
313
314             GPU_FULL_ON;
315             d_src.upload(src);
316             ocl::GaussianBlur(d_src, d_dst, Size(9, 9), 0);
317             d_dst.download(ocl_dst);
318             GPU_FULL_OFF;
319
320             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1.0);
321         }
322
323     }
324 }
325
326 ///////////// filter2D////////////////////////
327 PERFTEST(filter2D)
328 {
329     Mat src;
330
331     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
332     {
333         int all_type[] = {CV_8UC1, CV_8UC4};
334         std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
335
336         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
337         {
338             gen(src, size, size, all_type[j], 0, 256);
339
340             const int ksize = 3;
341
342             SUBTEST << "ksize = " << ksize << "; " << size << 'x' << size << "; " << type_name[j] ;
343
344             Mat kernel;
345             gen(kernel, ksize, ksize, CV_32SC1, -3.0, 3.0);
346
347             Mat dst, ocl_dst;
348
349             cv::filter2D(src, dst, -1, kernel);
350
351             CPU_ON;
352             cv::filter2D(src, dst, -1, kernel);
353             CPU_OFF;
354
355             ocl::oclMat d_src(src), d_dst;
356
357             WARMUP_ON;
358             ocl::filter2D(d_src, d_dst, -1, kernel);
359             WARMUP_OFF;
360
361             GPU_ON;
362             ocl::filter2D(d_src, d_dst, -1, kernel);
363             GPU_OFF;
364
365             GPU_FULL_ON;
366             d_src.upload(src);
367             ocl::filter2D(d_src, d_dst, -1, kernel);
368             d_dst.download(ocl_dst);
369             GPU_FULL_OFF;
370
371             TestSystem::instance().ExpectedMatNear(ocl_dst, dst, 1e-5);
372
373         }
374
375
376     }
377 }