File: test_filters.cpp

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 124,160 kB
  • ctags: 63,847
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 80
file content (476 lines) | stat: -rw-r--r-- 16,195 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Niko Li, newlife20080214@gmail.com
//    Jia Haipeng, jiahaipeng95@gmail.com
//    Zero Lin, Zero.Lin@amd.com
//    Zhang Ying, zhangying913@gmail.com
//    Yao Wang, bitwangyaoyao@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"

#ifdef HAVE_OPENCL

using namespace testing;
using namespace std;
using namespace cv;

PARAM_TEST_CASE(FilterTestBase, MatType,
                int, // kernel size
                Size, // dx, dy
                int, // border type
                double, // optional parameter
                bool) // roi or not
{
    bool isFP;

    int type, borderType, ksize;
    Size size;
    double param;
    bool useRoi;

    Mat src, dst_whole, src_roi, dst_roi;
    ocl::oclMat gsrc_whole, gsrc_roi, gdst_whole, gdst_roi;

    virtual void SetUp()
    {
        type = GET_PARAM(0);
        ksize = GET_PARAM(1);
        size = GET_PARAM(2);
        borderType = GET_PARAM(3);
        param = GET_PARAM(4);
        useRoi = GET_PARAM(5);

        isFP = (CV_MAT_DEPTH(type) == CV_32F || CV_MAT_DEPTH(type) == CV_64F);
    }

    void random_roi(int minSize = 1)
    {
        if (minSize == 0)
            minSize = ksize;
        Size roiSize = randomSize(minSize, MAX_VALUE);
        Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
        randomSubMat(src, src_roi, roiSize, srcBorder, type, isFP ? 0 : 5, isFP ? 1 : 256);

        Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
        randomSubMat(dst_whole, dst_roi, roiSize, dstBorder, type, isFP ? 0.20 : 60, isFP ? 0.25 : 70);

        generateOclMat(gsrc_whole, gsrc_roi, src, roiSize, srcBorder);
        generateOclMat(gdst_whole, gdst_roi, dst_whole, roiSize, dstBorder);
    }

    void Near()
    {
        if (isFP)
            Near(1e-6, true);
        else
            Near(1, false);
    }

    void Near(double threshold, bool relative)
    {
        Mat roi, whole;
        gdst_whole.download(whole);
        gdst_roi.download(roi);

        if (relative)
        {
            EXPECT_MAT_NEAR_RELATIVE(dst_whole, whole, threshold);
            EXPECT_MAT_NEAR_RELATIVE(dst_roi, roi, threshold);
        }
        else
        {
            EXPECT_MAT_NEAR(dst_whole, whole, threshold);
            EXPECT_MAT_NEAR(dst_roi, roi, threshold);
        }
    }
};

/////////////////////////////////////////////////////////////////////////////////////////////////
// blur

typedef FilterTestBase Blur;

#ifdef ANDROID
OCL_TEST_P(Blur, DISABLED_Mat)
#else
OCL_TEST_P(Blur, Mat)
#endif
{
    Size kernelSize(ksize, ksize);

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi(0); // TODO NOTE: min value for size is kernel size (temporary bypass border issues in CPU implementation)

        blur(src_roi, dst_roi, kernelSize, Point(-1, -1), borderType);
        ocl::blur(gsrc_roi, gdst_roi, kernelSize, Point(-1, -1), borderType); // TODO anchor

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// Laplacian

typedef FilterTestBase LaplacianTest;

OCL_TEST_P(LaplacianTest, Accuracy)
{
    double scale = param;

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        Laplacian(src_roi, dst_roi, -1, ksize, scale, 0, borderType);
        ocl::Laplacian(gsrc_roi, gdst_roi, -1, ksize, scale, 0, borderType);

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// erode & dilate

typedef FilterTestBase Erode;

OCL_TEST_P(Erode, Mat)
{
    // erode or dilate kernel
    Size kernelSize(ksize, ksize);
    Mat kernel;
    int iterations = (int)param;

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        kernel = randomMat(kernelSize, CV_8UC1, 0, 3);

        cv::erode(src_roi, dst_roi, kernel, Point(-1, -1), iterations);//, borderType);
        ocl::erode(gsrc_roi, gdst_roi, kernel, Point(-1, -1), iterations);//, borderType);

        Near();
    }
}

typedef FilterTestBase Dilate;

OCL_TEST_P(Dilate, Mat)
{
    // erode or dilate kernel
    Mat kernel;
    int iterations = (int)param;

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        kernel = randomMat(Size(3, 3), CV_8UC1, 0, 3);

        random_roi();

        cv::dilate(src_roi, dst_roi, kernel, Point(-1, -1), iterations);
        ocl::dilate(gsrc_roi, gdst_roi, kernel, Point(-1, -1), iterations); // TODO iterations, borderType

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// Sobel

typedef FilterTestBase SobelTest;

OCL_TEST_P(SobelTest, Mat)
{
    int dx = size.width, dy = size.height;
    double scale = param;

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        Sobel(src_roi, dst_roi, -1, dx, dy, ksize, scale, /* delta */0, borderType);
        ocl::Sobel(gsrc_roi, gdst_roi, -1, dx, dy, ksize, scale, /* delta */0, borderType);

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// Scharr

typedef FilterTestBase ScharrTest;

OCL_TEST_P(ScharrTest, Mat)
{
    int dx = size.width, dy = size.height;
    double scale = param;

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        Scharr(src_roi, dst_roi, -1, dx, dy, scale, /* delta */ 0, borderType);
        ocl::Scharr(gsrc_roi, gdst_roi, -1, dx, dy, scale, /* delta */ 0, borderType);

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// GaussianBlur

typedef FilterTestBase GaussianBlurTest;

OCL_TEST_P(GaussianBlurTest, Mat)
{
    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        double sigma1 = rng.uniform(0.1, 1.0);
        double sigma2 = rng.uniform(0.1, 1.0);

        GaussianBlur(src_roi, dst_roi, Size(ksize, ksize), sigma1, sigma2, borderType);
        ocl::GaussianBlur(gsrc_roi, gdst_roi, Size(ksize, ksize), sigma1, sigma2, borderType);

        Near(CV_MAT_DEPTH(type) == CV_8U ? 3 : 5e-5, false);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Filter2D

typedef FilterTestBase Filter2D;

OCL_TEST_P(Filter2D, Mat)
{
    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        Point anchor(-1, -1);
        if (size.width >= 0)
            anchor.x = size.width % ksize;
        if (size.height >= 0)
            anchor.y = size.height % ksize;

        const Size kernelSize(ksize, ksize);
        Mat kernel = randomMat(kernelSize, CV_32FC1, 0, 1.0);
        kernel *= 1.0 / (double)(ksize * ksize);

        cv::filter2D(src_roi, dst_roi, -1, kernel, anchor, 0.0, borderType);
        ocl::filter2D(gsrc_roi, gdst_roi, -1, kernel, anchor, 0.0, borderType);

        Near();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Bilateral

typedef FilterTestBase Bilateral;

OCL_TEST_P(Bilateral, Mat)
{
    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        double sigmacolor = rng.uniform(20, 100);
        double sigmaspace = rng.uniform(10, 40);

        cv::bilateralFilter(src_roi, dst_roi, ksize, sigmacolor, sigmaspace, borderType);
        ocl::bilateralFilter(gsrc_roi, gdst_roi, ksize, sigmacolor, sigmaspace, borderType);

        Near();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// AdaptiveBilateral

typedef FilterTestBase AdaptiveBilateral;

OCL_TEST_P(AdaptiveBilateral, DISABLED_Mat)
{
    const Size kernelSize(ksize, ksize);

    for (int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        adaptiveBilateralFilter(src_roi, dst_roi, kernelSize, 5, 1, Point(-1, -1), borderType); // TODO anchor
        ocl::adaptiveBilateralFilter(gsrc_roi, gdst_roi, kernelSize, 5, 1, Point(-1, -1), borderType);

        Near();
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
// MedianFilter

typedef FilterTestBase MedianFilter;

OCL_TEST_P(MedianFilter, Mat)
{
    for (int i = 0; i < LOOP_TIMES; ++i)
    {
        random_roi();

        medianBlur(src_roi, dst_roi, ksize);
        ocl::medianFilter(gsrc_roi, gdst_roi, ksize);

        Near();
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define FILTER_BORDER_SET_NO_ISOLATED \
    Values((int)BORDER_CONSTANT, (int)BORDER_REPLICATE, (int)BORDER_REFLECT, (int)BORDER_WRAP, (int)BORDER_REFLECT_101/*, \
            (int)BORDER_CONSTANT|BORDER_ISOLATED, (int)BORDER_REPLICATE|BORDER_ISOLATED, \
            (int)BORDER_REFLECT|BORDER_ISOLATED, (int)BORDER_WRAP|BORDER_ISOLATED, \
            (int)BORDER_REFLECT_101|BORDER_ISOLATED*/) // WRAP and ISOLATED are not supported by cv:: version

#define FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED \
    Values((int)BORDER_CONSTANT, (int)BORDER_REPLICATE, (int)BORDER_REFLECT, /*(int)BORDER_WRAP,*/ (int)BORDER_REFLECT_101/*, \
            (int)BORDER_CONSTANT|BORDER_ISOLATED, (int)BORDER_REPLICATE|BORDER_ISOLATED, \
            (int)BORDER_REFLECT|BORDER_ISOLATED, (int)BORDER_WRAP|BORDER_ISOLATED, \
            (int)BORDER_REFLECT_101|BORDER_ISOLATED*/) // WRAP and ISOLATED are not supported by cv:: version

#define FILTER_DATATYPES Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, \
                                CV_32FC1, CV_32FC3, CV_32FC4, \
                                CV_64FC1, CV_64FC3, CV_64FC4)

INSTANTIATE_TEST_CASE_P(Filter, Blur, Combine(
                            FILTER_DATATYPES,
                            Values(3, 5, 7),
                            Values(Size(0, 0)), // not used
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, LaplacianTest, Combine(
                            FILTER_DATATYPES,
                            Values(1, 3),
                            Values(Size(0, 0)), // not used
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(1.0, 0.2, 3.0), // scalar
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, Erode, Combine(
                            Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
                            Values(3, 5, 7),
                            Values(Size(0, 0)), // not used
                            Values(0), // not used
                            Values(1.0, 2.0, 3.0),
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, Dilate, Combine(
                            Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
                            Values(3, 5, 7),
                            Values(Size(0, 0)), // not used
                            Values(0), // not used
                            Values(1.0, 2.0, 3.0),
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, SobelTest, Combine(
                            Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
                            Values(3, 5),
                            Values(Size(1, 0), Size(1, 1), Size(2, 0), Size(2, 1)), // dx, dy
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, ScharrTest, Combine(
                            Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
                            Values(1),
                            Values(Size(0, 1), Size(1, 0)), // dx, dy
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(1.0, 0.2), // scalar
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, GaussianBlurTest, Combine(
                            Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC4),
                            Values(3, 5),
                            Values(Size(0, 0)), // not used
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, Filter2D, testing::Combine(
                            FILTER_DATATYPES,
                            Values(3, 15), // TODO 25: CPU implementation has some issues
                            Values(Size(-1, -1), Size(0, 0), Size(2, 1)), // anchor
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, Bilateral, Combine(
                            Values(CV_8UC1, CV_8UC3),
                            Values(5, 9),
                            Values(Size(0, 0)), // not used
                            FILTER_BORDER_SET_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, AdaptiveBilateral, Combine(
                            Values(CV_8UC1, CV_8UC3),
                            Values(5, 9),
                            Values(Size(0, 0)), // not used
                            FILTER_BORDER_SET_NO_WRAP_NO_ISOLATED,
                            Values(0.0), // not used
                            Bool()));

INSTANTIATE_TEST_CASE_P(Filter, MedianFilter, Combine(
                            Values(CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
                            Values(3, 5),
                            Values(Size(0, 0)), // not used
                            Values(0), // not used
                            Values(0.0), // not used
                            Bool()));

#endif // HAVE_OPENCL