gpu separable filters for CV_8UC3, CV_32FC3 and CV_32FC4 types
[profile/ivi/opencv.git] / modules / gpu / 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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 #ifdef HAVE_CUDA
45
46 namespace {
47
48 IMPLEMENT_PARAM_CLASS(KSize, cv::Size)
49
50 cv::Mat getInnerROI(cv::InputArray m_, cv::Size ksize)
51 {
52     cv::Mat m = getMat(m_);
53     cv::Rect roi(ksize.width, ksize.height, m.cols - 2 * ksize.width, m.rows - 2 * ksize.height);
54     return m(roi);
55 }
56
57 cv::Mat getInnerROI(cv::InputArray m, int ksize)
58 {
59     return getInnerROI(m, cv::Size(ksize, ksize));
60 }
61
62 /////////////////////////////////////////////////////////////////////////////////////////////////
63 // Blur
64
65 IMPLEMENT_PARAM_CLASS(Anchor, cv::Point)
66
67 PARAM_TEST_CASE(Blur, cv::gpu::DeviceInfo, cv::Size, MatType, KSize, Anchor, UseRoi)
68 {
69     cv::gpu::DeviceInfo devInfo;
70     cv::Size size;
71     int type;
72     cv::Size ksize;
73     cv::Point anchor;
74     bool useRoi;
75
76     virtual void SetUp()
77     {
78         devInfo = GET_PARAM(0);
79         size = GET_PARAM(1);
80         type = GET_PARAM(2);
81         ksize = GET_PARAM(3);
82         anchor = GET_PARAM(4);
83         useRoi = GET_PARAM(5);
84
85         cv::gpu::setDevice(devInfo.deviceID());
86     }
87 };
88
89 TEST_P(Blur, Accuracy)
90 {
91     cv::Mat src = randomMat(size, type);
92
93     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
94     cv::gpu::blur(loadMat(src, useRoi), dst, ksize, anchor);
95
96     cv::Mat dst_gold;
97     cv::blur(src, dst_gold, ksize, anchor);
98
99     EXPECT_MAT_NEAR(getInnerROI(dst_gold, ksize), getInnerROI(dst, ksize), 1.0);
100 }
101
102 INSTANTIATE_TEST_CASE_P(GPU_Filter, Blur, testing::Combine(
103     ALL_DEVICES,
104     DIFFERENT_SIZES,
105     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),
106     testing::Values(KSize(cv::Size(3, 3)), KSize(cv::Size(5, 5)), KSize(cv::Size(7, 7))),
107     testing::Values(Anchor(cv::Point(-1, -1)), Anchor(cv::Point(0, 0)), Anchor(cv::Point(2, 2))),
108     WHOLE_SUBMAT));
109
110 /////////////////////////////////////////////////////////////////////////////////////////////////
111 // Sobel
112
113 IMPLEMENT_PARAM_CLASS(Deriv_X, int)
114 IMPLEMENT_PARAM_CLASS(Deriv_Y, int)
115
116 PARAM_TEST_CASE(Sobel, cv::gpu::DeviceInfo, cv::Size, MatType, KSize, Deriv_X, Deriv_Y, BorderType, UseRoi)
117 {
118     cv::gpu::DeviceInfo devInfo;
119     cv::Size size;
120     int type;
121     cv::Size ksize;
122     int dx;
123     int dy;
124     int borderType;
125     bool useRoi;
126
127     virtual void SetUp()
128     {
129         devInfo = GET_PARAM(0);
130         size = GET_PARAM(1);
131         type = GET_PARAM(2);
132         ksize = GET_PARAM(3);
133         dx = GET_PARAM(4);
134         dy = GET_PARAM(5);
135         borderType = GET_PARAM(6);
136         useRoi = GET_PARAM(7);
137
138         cv::gpu::setDevice(devInfo.deviceID());
139     }
140 };
141
142 TEST_P(Sobel, Accuracy)
143 {
144     if (dx == 0 && dy == 0)
145         return;
146
147     cv::Mat src = randomMat(size, type);
148
149     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
150     cv::gpu::Sobel(loadMat(src, useRoi), dst, -1, dx, dy, ksize.width, 1.0, borderType);
151
152     cv::Mat dst_gold;
153     cv::Sobel(src, dst_gold, -1, dx, dy, ksize.width, 1.0, 0.0, borderType);
154
155     EXPECT_MAT_NEAR(dst_gold, dst, CV_MAT_DEPTH(type) < CV_32F ? 0.0 : 0.1);
156 }
157
158 INSTANTIATE_TEST_CASE_P(GPU_Filter, Sobel, testing::Combine(
159     ALL_DEVICES,
160     DIFFERENT_SIZES,
161     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
162     testing::Values(KSize(cv::Size(3, 3)), KSize(cv::Size(5, 5)), KSize(cv::Size(7, 7))),
163     testing::Values(Deriv_X(0), Deriv_X(1), Deriv_X(2)),
164     testing::Values(Deriv_Y(0), Deriv_Y(1), Deriv_Y(2)),
165     testing::Values(BorderType(cv::BORDER_REFLECT101),
166                     BorderType(cv::BORDER_REPLICATE),
167                     BorderType(cv::BORDER_CONSTANT),
168                     BorderType(cv::BORDER_REFLECT)),
169     WHOLE_SUBMAT));
170
171 /////////////////////////////////////////////////////////////////////////////////////////////////
172 // Scharr
173
174 PARAM_TEST_CASE(Scharr, cv::gpu::DeviceInfo, cv::Size, MatType, Deriv_X, Deriv_Y, BorderType, UseRoi)
175 {
176     cv::gpu::DeviceInfo devInfo;
177     cv::Size size;
178     int type;
179     int dx;
180     int dy;
181     int borderType;
182     bool useRoi;
183
184     virtual void SetUp()
185     {
186         devInfo = GET_PARAM(0);
187         size = GET_PARAM(1);
188         type = GET_PARAM(2);
189         dx = GET_PARAM(3);
190         dy = GET_PARAM(4);
191         borderType = GET_PARAM(5);
192         useRoi = GET_PARAM(6);
193
194         cv::gpu::setDevice(devInfo.deviceID());
195     }
196 };
197
198 TEST_P(Scharr, Accuracy)
199 {
200     if (dx + dy != 1)
201         return;
202
203     cv::Mat src = randomMat(size, type);
204
205     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
206     cv::gpu::Scharr(loadMat(src, useRoi), dst, -1, dx, dy, 1.0, borderType);
207
208     cv::Mat dst_gold;
209     cv::Scharr(src, dst_gold, -1, dx, dy, 1.0, 0.0, borderType);
210
211     EXPECT_MAT_NEAR(dst_gold, dst, CV_MAT_DEPTH(type) < CV_32F ? 0.0 : 0.1);
212 }
213
214 INSTANTIATE_TEST_CASE_P(GPU_Filter, Scharr, testing::Combine(
215     ALL_DEVICES,
216     DIFFERENT_SIZES,
217     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
218     testing::Values(Deriv_X(0), Deriv_X(1)),
219     testing::Values(Deriv_Y(0), Deriv_Y(1)),
220     testing::Values(BorderType(cv::BORDER_REFLECT101),
221                     BorderType(cv::BORDER_REPLICATE),
222                     BorderType(cv::BORDER_CONSTANT),
223                     BorderType(cv::BORDER_REFLECT)),
224     WHOLE_SUBMAT));
225
226 /////////////////////////////////////////////////////////////////////////////////////////////////
227 // GaussianBlur
228
229 PARAM_TEST_CASE(GaussianBlur, cv::gpu::DeviceInfo, cv::Size, MatType, KSize, BorderType, UseRoi)
230 {
231     cv::gpu::DeviceInfo devInfo;
232     cv::Size size;
233     int type;
234     cv::Size ksize;
235     int borderType;
236     bool useRoi;
237
238     virtual void SetUp()
239     {
240         devInfo = GET_PARAM(0);
241         size = GET_PARAM(1);
242         type = GET_PARAM(2);
243         ksize = GET_PARAM(3);
244         borderType = GET_PARAM(4);
245         useRoi = GET_PARAM(5);
246
247         cv::gpu::setDevice(devInfo.deviceID());
248     }
249 };
250
251 TEST_P(GaussianBlur, Accuracy)
252 {
253     cv::Mat src = randomMat(size, type);
254     double sigma1 = randomDouble(0.1, 1.0);
255     double sigma2 = randomDouble(0.1, 1.0);
256
257     if (ksize.height > 16 && !supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
258     {
259         try
260         {
261             cv::gpu::GpuMat dst;
262             cv::gpu::GaussianBlur(loadMat(src), dst, ksize, sigma1, sigma2, borderType);
263         }
264         catch (const cv::Exception& e)
265         {
266             ASSERT_EQ(CV_StsNotImplemented, e.code);
267         }
268     }
269     else
270     {
271         cv::gpu::GpuMat dst = createMat(size, type, useRoi);
272         cv::gpu::GaussianBlur(loadMat(src, useRoi), dst, ksize, sigma1, sigma2, borderType);
273
274         cv::Mat dst_gold;
275         cv::GaussianBlur(src, dst_gold, ksize, sigma1, sigma2, borderType);
276
277         EXPECT_MAT_NEAR(dst_gold, dst, 4.0);
278     }
279 }
280
281 INSTANTIATE_TEST_CASE_P(GPU_Filter, GaussianBlur, testing::Combine(
282     ALL_DEVICES,
283     DIFFERENT_SIZES,
284     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
285     testing::Values(KSize(cv::Size(3, 3)),
286                     KSize(cv::Size(5, 5)),
287                     KSize(cv::Size(7, 7)),
288                     KSize(cv::Size(9, 9)),
289                     KSize(cv::Size(11, 11)),
290                     KSize(cv::Size(13, 13)),
291                     KSize(cv::Size(15, 15)),
292                     KSize(cv::Size(17, 17)),
293                     KSize(cv::Size(19, 19)),
294                     KSize(cv::Size(21, 21)),
295                     KSize(cv::Size(23, 23)),
296                     KSize(cv::Size(25, 25)),
297                     KSize(cv::Size(27, 27)),
298                     KSize(cv::Size(29, 29)),
299                     KSize(cv::Size(31, 31))),
300     testing::Values(BorderType(cv::BORDER_REFLECT101),
301                     BorderType(cv::BORDER_REPLICATE),
302                     BorderType(cv::BORDER_CONSTANT),
303                     BorderType(cv::BORDER_REFLECT)),
304     WHOLE_SUBMAT));
305
306 /////////////////////////////////////////////////////////////////////////////////////////////////
307 // Laplacian
308
309 PARAM_TEST_CASE(Laplacian, cv::gpu::DeviceInfo, cv::Size, MatType, KSize, UseRoi)
310 {
311     cv::gpu::DeviceInfo devInfo;
312     cv::Size size;
313     int type;
314     cv::Size ksize;
315     bool useRoi;
316
317     virtual void SetUp()
318     {
319         devInfo = GET_PARAM(0);
320         size = GET_PARAM(1);
321         type = GET_PARAM(2);
322         ksize = GET_PARAM(3);
323         useRoi = GET_PARAM(4);
324
325         cv::gpu::setDevice(devInfo.deviceID());
326     }
327 };
328
329 TEST_P(Laplacian, Accuracy)
330 {
331     cv::Mat src = randomMat(size, type);
332
333     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
334     cv::gpu::Laplacian(loadMat(src, useRoi), dst, -1, ksize.width);
335
336     cv::Mat dst_gold;
337     cv::Laplacian(src, dst_gold, -1, ksize.width);
338
339     EXPECT_MAT_NEAR(dst_gold, dst, src.depth() < CV_32F ? 0.0 : 1e-3);
340 }
341
342 INSTANTIATE_TEST_CASE_P(GPU_Filter, Laplacian, testing::Combine(
343     ALL_DEVICES,
344     DIFFERENT_SIZES,
345     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4), MatType(CV_32FC1)),
346     testing::Values(KSize(cv::Size(1, 1)), KSize(cv::Size(3, 3))),
347     WHOLE_SUBMAT));
348
349 /////////////////////////////////////////////////////////////////////////////////////////////////
350 // Erode
351
352 IMPLEMENT_PARAM_CLASS(Iterations, int)
353
354 PARAM_TEST_CASE(Erode, cv::gpu::DeviceInfo, cv::Size, MatType, Anchor, Iterations, UseRoi)
355 {
356     cv::gpu::DeviceInfo devInfo;
357     cv::Size size;
358     int type;
359     cv::Point anchor;
360     int iterations;
361     bool useRoi;
362
363     virtual void SetUp()
364     {
365         devInfo = GET_PARAM(0);
366         size = GET_PARAM(1);
367         type = GET_PARAM(2);
368         anchor = GET_PARAM(3);
369         iterations = GET_PARAM(4);
370         useRoi = GET_PARAM(5);
371
372         cv::gpu::setDevice(devInfo.deviceID());
373     }
374 };
375
376 TEST_P(Erode, Accuracy)
377 {
378     cv::Mat src = randomMat(size, type);
379     cv::Mat kernel = cv::Mat::ones(3, 3, CV_8U);
380
381     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
382     cv::gpu::erode(loadMat(src, useRoi), dst, kernel, anchor, iterations);
383
384     cv::Mat dst_gold;
385     cv::erode(src, dst_gold, kernel, anchor, iterations);
386
387     cv::Size ksize = cv::Size(kernel.cols + iterations * (kernel.cols - 1), kernel.rows + iterations * (kernel.rows - 1));
388
389     EXPECT_MAT_NEAR(getInnerROI(dst_gold, ksize), getInnerROI(dst, ksize), 0.0);
390 }
391
392 INSTANTIATE_TEST_CASE_P(GPU_Filter, Erode, testing::Combine(
393     ALL_DEVICES,
394     DIFFERENT_SIZES,
395     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),
396     testing::Values(Anchor(cv::Point(-1, -1)), Anchor(cv::Point(0, 0)), Anchor(cv::Point(2, 2))),
397     testing::Values(Iterations(1), Iterations(2), Iterations(3)),
398     WHOLE_SUBMAT));
399
400 /////////////////////////////////////////////////////////////////////////////////////////////////
401 // Dilate
402
403 PARAM_TEST_CASE(Dilate, cv::gpu::DeviceInfo, cv::Size, MatType, Anchor, Iterations, UseRoi)
404 {
405     cv::gpu::DeviceInfo devInfo;
406     cv::Size size;
407     int type;
408     cv::Point anchor;
409     int iterations;
410     bool useRoi;
411
412     virtual void SetUp()
413     {
414         devInfo = GET_PARAM(0);
415         size = GET_PARAM(1);
416         type = GET_PARAM(2);
417         anchor = GET_PARAM(3);
418         iterations = GET_PARAM(4);
419         useRoi = GET_PARAM(5);
420
421         cv::gpu::setDevice(devInfo.deviceID());
422     }
423 };
424
425 TEST_P(Dilate, Accuracy)
426 {
427     cv::Mat src = randomMat(size, type);
428     cv::Mat kernel = cv::Mat::ones(3, 3, CV_8U);
429
430     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
431     cv::gpu::dilate(loadMat(src, useRoi), dst, kernel, anchor, iterations);
432
433     cv::Mat dst_gold;
434     cv::dilate(src, dst_gold, kernel, anchor, iterations);
435
436     cv::Size ksize = cv::Size(kernel.cols + iterations * (kernel.cols - 1), kernel.rows + iterations * (kernel.rows - 1));
437
438     EXPECT_MAT_NEAR(getInnerROI(dst_gold, ksize), getInnerROI(dst, ksize), 0.0);
439 }
440
441 INSTANTIATE_TEST_CASE_P(GPU_Filter, Dilate, testing::Combine(
442     ALL_DEVICES,
443     DIFFERENT_SIZES,
444     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),
445     testing::Values(Anchor(cv::Point(-1, -1)), Anchor(cv::Point(0, 0)), Anchor(cv::Point(2, 2))),
446     testing::Values(Iterations(1), Iterations(2), Iterations(3)),
447     WHOLE_SUBMAT));
448
449 /////////////////////////////////////////////////////////////////////////////////////////////////
450 // MorphEx
451
452 CV_ENUM(MorphOp, cv::MORPH_OPEN, cv::MORPH_CLOSE, cv::MORPH_GRADIENT, cv::MORPH_TOPHAT, cv::MORPH_BLACKHAT)
453 #define ALL_MORPH_OPS testing::Values(MorphOp(cv::MORPH_OPEN), MorphOp(cv::MORPH_CLOSE), MorphOp(cv::MORPH_GRADIENT), MorphOp(cv::MORPH_TOPHAT), MorphOp(cv::MORPH_BLACKHAT))
454
455 PARAM_TEST_CASE(MorphEx, cv::gpu::DeviceInfo, cv::Size, MatType, MorphOp, Anchor, Iterations, UseRoi)
456 {
457     cv::gpu::DeviceInfo devInfo;
458     cv::Size size;
459     int type;
460     int morphOp;
461     cv::Point anchor;
462     int iterations;
463     bool useRoi;
464
465     virtual void SetUp()
466     {
467         devInfo = GET_PARAM(0);
468         size = GET_PARAM(1);
469         type = GET_PARAM(2);
470         morphOp = GET_PARAM(3);
471         anchor = GET_PARAM(4);
472         iterations = GET_PARAM(5);
473         useRoi = GET_PARAM(6);
474
475         cv::gpu::setDevice(devInfo.deviceID());
476     }
477 };
478
479 TEST_P(MorphEx, Accuracy)
480 {
481     cv::Mat src = randomMat(size, type);
482     cv::Mat kernel = cv::Mat::ones(3, 3, CV_8U);
483
484     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
485     cv::gpu::morphologyEx(loadMat(src, useRoi), dst, morphOp, kernel, anchor, iterations);
486
487     cv::Mat dst_gold;
488     cv::morphologyEx(src, dst_gold, morphOp, kernel, anchor, iterations);
489
490     cv::Size border = cv::Size(kernel.cols + (iterations + 1) * kernel.cols + 2, kernel.rows + (iterations + 1) * kernel.rows + 2);
491
492     EXPECT_MAT_NEAR(getInnerROI(dst_gold, border), getInnerROI(dst, border), 0.0);
493 }
494
495 INSTANTIATE_TEST_CASE_P(GPU_Filter, MorphEx, testing::Combine(
496     ALL_DEVICES,
497     DIFFERENT_SIZES,
498     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4)),
499     ALL_MORPH_OPS,
500     testing::Values(Anchor(cv::Point(-1, -1)), Anchor(cv::Point(0, 0)), Anchor(cv::Point(2, 2))),
501     testing::Values(Iterations(1), Iterations(2), Iterations(3)),
502     WHOLE_SUBMAT));
503
504 /////////////////////////////////////////////////////////////////////////////////////////////////
505 // Filter2D
506
507 PARAM_TEST_CASE(Filter2D, cv::gpu::DeviceInfo, cv::Size, MatType, KSize, Anchor, BorderType, UseRoi)
508 {
509     cv::gpu::DeviceInfo devInfo;
510     cv::Size size;
511     int type;
512     cv::Size ksize;
513     cv::Point anchor;
514     int borderType;
515     bool useRoi;
516
517     cv::Mat img;
518
519     virtual void SetUp()
520     {
521         devInfo = GET_PARAM(0);
522         size = GET_PARAM(1);
523         type = GET_PARAM(2);
524         ksize = GET_PARAM(3);
525         anchor = GET_PARAM(4);
526         borderType = GET_PARAM(5);
527         useRoi = GET_PARAM(6);
528
529         cv::gpu::setDevice(devInfo.deviceID());
530     }
531 };
532
533 TEST_P(Filter2D, Accuracy)
534 {
535     cv::Mat src = randomMat(size, type);
536     cv::Mat kernel = randomMat(cv::Size(ksize.width, ksize.height), CV_32FC1, 0.0, 1.0);
537
538     cv::gpu::GpuMat dst = createMat(size, type, useRoi);
539     cv::gpu::filter2D(loadMat(src, useRoi), dst, -1, kernel, anchor, borderType);
540
541     cv::Mat dst_gold;
542     cv::filter2D(src, dst_gold, -1, kernel, anchor, 0, borderType);
543
544     EXPECT_MAT_NEAR(dst_gold, dst, CV_MAT_DEPTH(type) == CV_32F ? 1e-1 : 1.0);
545 }
546
547 INSTANTIATE_TEST_CASE_P(GPU_Filter, Filter2D, testing::Combine(
548     ALL_DEVICES,
549     DIFFERENT_SIZES,
550     testing::Values(MatType(CV_8UC1), MatType(CV_8UC4), MatType(CV_16UC1), MatType(CV_16UC4), MatType(CV_32FC1), MatType(CV_32FC4)),
551     testing::Values(KSize(cv::Size(3, 3)), KSize(cv::Size(5, 5)), KSize(cv::Size(7, 7)), KSize(cv::Size(11, 11)), KSize(cv::Size(13, 13)), KSize(cv::Size(15, 15))),
552     testing::Values(Anchor(cv::Point(-1, -1)), Anchor(cv::Point(0, 0)), Anchor(cv::Point(2, 2))),
553     testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_CONSTANT), BorderType(cv::BORDER_REFLECT)),
554     WHOLE_SUBMAT));
555
556 } // namespace
557
558 #endif // HAVE_CUDA