test_denoising.cpp 4.34 KB
Newer Older
Ilya Lavrenov's avatar
Ilya Lavrenov committed
1 2 3 4 5
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
6 7
// Third party copyrights are property of their respective owners.

Alexander Alekhin's avatar
Alexander Alekhin committed
8
#include "../test_precomp.hpp"
9 10 11 12 13 14 15
#include "opencv2/ts/ocl_test.hpp"

#ifdef HAVE_OPENCL

namespace cvtest {
namespace ocl {

Erik Karlsson's avatar
Erik Karlsson committed
16
PARAM_TEST_CASE(FastNlMeansDenoisingTestBase, Channels, int, bool, bool)
17
{
Erik Karlsson's avatar
Erik Karlsson committed
18
    int cn, normType, templateWindowSize, searchWindowSize;
19
    std::vector<float> h;
Erik Karlsson's avatar
Erik Karlsson committed
20
    bool use_roi, use_image;
21

22 23
    TEST_DECLARE_INPUT_PARAMETER(src);
    TEST_DECLARE_OUTPUT_PARAMETER(dst);
24 25 26 27

    virtual void SetUp()
    {
        cn = GET_PARAM(0);
Erik Karlsson's avatar
Erik Karlsson committed
28 29 30
        normType = GET_PARAM(1);
        use_roi = GET_PARAM(2);
        use_image = GET_PARAM(3);
31 32 33

        templateWindowSize = 7;
        searchWindowSize = 21;
Erik Karlsson's avatar
Erik Karlsson committed
34

35
        h.resize(cn);
Erik Karlsson's avatar
Erik Karlsson committed
36 37
        for (int i=0; i<cn; i++)
            h[i] = 3.0f + 0.5f*i;
38 39
    }

40
    void generateTestData()
41
    {
Erik Karlsson's avatar
Erik Karlsson committed
42
        const int type = CV_8UC(cn);
43
        Mat image;
Erik Karlsson's avatar
Erik Karlsson committed
44 45 46 47

        if (use_image) {
            image = readImage("denoising/lena_noised_gaussian_sigma=10.png",
                                  cn == 1 ? IMREAD_GRAYSCALE : IMREAD_COLOR);
48 49 50
            ASSERT_FALSE(image.empty());
        }

Erik Karlsson's avatar
Erik Karlsson committed
51
        Size roiSize = use_image ? image.size() : randomSize(1, MAX_VALUE);
52 53
        Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
        randomSubMat(src, src_roi, roiSize, srcBorder, type, 0, 255);
Erik Karlsson's avatar
Erik Karlsson committed
54
        if (use_image) {
55
            ASSERT_TRUE(cn > 0 && cn <= 4);
Erik Karlsson's avatar
Erik Karlsson committed
56 57 58 59 60
            if (cn == 2) {
                int from_to[] = { 0,0, 1,1 };
                src_roi.create(roiSize, type);
                mixChannels(&image, 1, &src_roi, 1, from_to, 2);
            }
Erik Karlsson's avatar
Erik Karlsson committed
61 62 63 64 65
            else if (cn == 4) {
                int from_to[] = { 0,0, 1,1, 2,2, 1,3};
                src_roi.create(roiSize, type);
                mixChannels(&image, 1, &src_roi, 1, from_to, 4);
            }
Erik Karlsson's avatar
Erik Karlsson committed
66 67
            else image.copyTo(src_roi);
        }
68 69 70 71

        Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
        randomSubMat(dst, dst_roi, roiSize, dstBorder, type, 0, 255);

72 73
        UMAT_UPLOAD_INPUT_PARAMETER(src);
        UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
74 75 76 77 78 79 80 81 82 83 84
    }
};

typedef FastNlMeansDenoisingTestBase FastNlMeansDenoising;

OCL_TEST_P(FastNlMeansDenoising, Mat)
{
    for (int j = 0; j < test_loop_times; j++)
    {
        generateTestData();

Erik Karlsson's avatar
Erik Karlsson committed
85 86
        OCL_OFF(cv::fastNlMeansDenoising(src_roi, dst_roi, std::vector<float>(1, h[0]), templateWindowSize, searchWindowSize, normType));
        OCL_ON(cv::fastNlMeansDenoising(usrc_roi, udst_roi, std::vector<float>(1, h[0]), templateWindowSize, searchWindowSize, normType));
Erik Karlsson's avatar
Erik Karlsson committed
87 88 89 90 91 92 93 94 95 96 97 98 99

        OCL_EXPECT_MATS_NEAR(dst, 1);
    }
}

typedef FastNlMeansDenoisingTestBase FastNlMeansDenoising_hsep;

OCL_TEST_P(FastNlMeansDenoising_hsep, Mat)
{
    for (int j = 0; j < test_loop_times; j++)
    {
        generateTestData();

Erik Karlsson's avatar
Erik Karlsson committed
100 101
        OCL_OFF(cv::fastNlMeansDenoising(src_roi, dst_roi, h, templateWindowSize, searchWindowSize, normType));
        OCL_ON(cv::fastNlMeansDenoising(usrc_roi, udst_roi, h, templateWindowSize, searchWindowSize, normType));
102

103
        OCL_EXPECT_MATS_NEAR(dst, 1);
104 105 106
    }
}

107
typedef FastNlMeansDenoisingTestBase FastNlMeansDenoisingColored;
108

109
OCL_TEST_P(FastNlMeansDenoisingColored, Mat)
110 111 112 113 114
{
    for (int j = 0; j < test_loop_times; j++)
    {
        generateTestData();

Erik Karlsson's avatar
Erik Karlsson committed
115 116
        OCL_OFF(cv::fastNlMeansDenoisingColored(src_roi, dst_roi, h[0], h[0], templateWindowSize, searchWindowSize));
        OCL_ON(cv::fastNlMeansDenoisingColored(usrc_roi, udst_roi, h[0], h[0], templateWindowSize, searchWindowSize));
117

118
        OCL_EXPECT_MATS_NEAR(dst, 1);
119 120 121
    }
}

Erik Karlsson's avatar
Erik Karlsson committed
122
OCL_INSTANTIATE_TEST_CASE_P(Photo, FastNlMeansDenoising,
Erik Karlsson's avatar
Erik Karlsson committed
123 124
                            Combine(Values(1, 2, 3, 4), Values((int)NORM_L2, (int)NORM_L1),
                                    Bool(), Values(true)));
Erik Karlsson's avatar
Erik Karlsson committed
125
OCL_INSTANTIATE_TEST_CASE_P(Photo, FastNlMeansDenoising_hsep,
Erik Karlsson's avatar
Erik Karlsson committed
126 127
                            Combine(Values(1, 2, 3, 4), Values((int)NORM_L2, (int)NORM_L1),
                                    Bool(), Values(true)));
Erik Karlsson's avatar
Erik Karlsson committed
128
OCL_INSTANTIATE_TEST_CASE_P(Photo, FastNlMeansDenoisingColored,
Erik Karlsson's avatar
Erik Karlsson committed
129
                            Combine(Values(3, 4), Values((int)NORM_L2), Bool(), Values(false)));
130 131 132 133

} } // namespace cvtest::ocl

#endif // HAVE_OPENCL