test_adaptive_manifold.cpp 7 KB
Newer Older
vludv's avatar
vludv committed
1 2 3 4
/*
 *  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.
5 6
 *
 *
vludv's avatar
vludv committed
7 8 9
 *  License Agreement
 *  For Open Source Computer Vision Library
 *  (3 - clause BSD License)
10
 *
vludv's avatar
vludv committed
11 12
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met :
13
 *
14
 *  * Redistributions of source code must retain the above copyright notice,
vludv's avatar
vludv committed
15
 *  this list of conditions and the following disclaimer.
16
 *
vludv's avatar
vludv committed
17 18 19
 *  * Redistributions 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.
20
 *
vludv's avatar
vludv committed
21 22 23
 *  * Neither the names of the copyright holders nor the names of the contributors
 *  may be used to endorse or promote products derived from this software
 *  without specific prior written permission.
24
 *
vludv's avatar
vludv committed
25 26 27 28 29 30 31 32 33 34 35 36
 *  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 copyright holders 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.
 */

vludv's avatar
vludv committed
37 38
#include "test_precomp.hpp"

39 40 41
namespace cvtest
{

vludv's avatar
vludv committed
42 43 44
using namespace std;
using namespace std::tr1;
using namespace testing;
45 46
using namespace cv;
using namespace cv::ximgproc;
vludv's avatar
vludv committed
47 48 49 50 51 52 53 54 55 56

#ifndef SQR
#define SQR(x) ((x)*(x))
#endif

static string getOpenCVExtraDir()
{
    return cvtest::TS::ptr()->get_data_path();
}

57
static void checkSimilarity(InputArray res, InputArray ref, double maxNormInf = 1, double maxNormL2 = 1.0 / 64)
vludv's avatar
vludv committed
58 59 60 61
{
    double normInf = cvtest::norm(res, ref, NORM_INF);
    double normL2 = cvtest::norm(res, ref, NORM_L2) / res.total();

62 63
    if (maxNormInf >= 0) EXPECT_LE(normInf, maxNormInf);
    if (maxNormL2 >= 0) EXPECT_LE(normL2, maxNormL2);
vludv's avatar
vludv committed
64 65 66 67 68 69
}

TEST(AdaptiveManifoldTest, SplatSurfaceAccuracy)
{
    RNG rnd(0);

70 71
    cv::setNumThreads(cv::getNumberOfCPUs());

72
    for (int i = 0; i < 5; i++)
vludv's avatar
vludv committed
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
    {
        Size sz(rnd.uniform(512, 1024), rnd.uniform(512, 1024));

        int guideCn = rnd.uniform(1, 8);
        Mat guide(sz, CV_MAKE_TYPE(CV_32F, guideCn));
        randu(guide, 0, 1);

        Scalar surfaceValue;
        int srcCn = rnd.uniform(1, 4);
        rnd.fill(surfaceValue, RNG::UNIFORM, 0, 255);
        Mat src(sz, CV_MAKE_TYPE(CV_8U, srcCn), surfaceValue);

        double sigma_s = rnd.uniform(1.0, 50.0);
        double sigma_r = rnd.uniform(0.1, 0.9);

        Mat res;
        amFilter(guide, src, res, sigma_s, sigma_r, false);

        double normInf = cvtest::norm(src, res, NORM_INF);
        EXPECT_EQ(normInf, 0);
    }
}

TEST(AdaptiveManifoldTest, AuthorsReferenceAccuracy)
{
    String srcImgPath = "cv/edgefilter/kodim23.png";
99

vludv's avatar
vludv committed
100 101 102 103 104 105 106
    String refPaths[] =
    {
        "cv/edgefilter/amf/kodim23_amf_ss5_sr0.3_ref.png",
        "cv/edgefilter/amf/kodim23_amf_ss30_sr0.1_ref.png",
        "cv/edgefilter/amf/kodim23_amf_ss50_sr0.3_ref.png"
    };

107
    pair<double, double> refParams[] =
vludv's avatar
vludv committed
108 109 110 111 112 113
    {
        make_pair(5.0, 0.3),
        make_pair(30.0, 0.1),
        make_pair(50.0, 0.3)
    };

114
    String refOutliersPaths[] =
vludv's avatar
vludv committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    {
        "cv/edgefilter/amf/kodim23_amf_ss5_sr0.1_outliers_ref.png",
        "cv/edgefilter/amf/kodim23_amf_ss15_sr0.3_outliers_ref.png",
        "cv/edgefilter/amf/kodim23_amf_ss50_sr0.5_outliers_ref.png"
    };

    pair<double, double> refOutliersParams[] =
    {
        make_pair(5.0, 0.1),
        make_pair(15.0, 0.3),
        make_pair(50.0, 0.5),
    };

    Mat srcImg = imread(getOpenCVExtraDir() + srcImgPath);
    ASSERT_TRUE(!srcImg.empty());

131 132
    cv::setNumThreads(cv::getNumberOfCPUs());

vludv's avatar
vludv committed
133 134 135 136 137 138 139 140 141
    for (int i = 0; i < 3; i++)
    {
        Mat refRes = imread(getOpenCVExtraDir() + refPaths[i]);
        double sigma_s = refParams[i].first;
        double sigma_r = refParams[i].second;
        ASSERT_TRUE(!refRes.empty());

        Mat res;
        Ptr<AdaptiveManifoldFilter> amf = createAMFilter(sigma_s, sigma_r, false);
142
        amf->setUseRNG(false);
vludv's avatar
vludv committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        amf->filter(srcImg, res, srcImg);
        amf->collectGarbage();

        checkSimilarity(res, refRes);
    }

    for (int i = 0; i < 3; i++)
    {
        Mat refRes = imread(getOpenCVExtraDir() + refOutliersPaths[i]);
        double sigma_s = refOutliersParams[i].first;
        double sigma_r = refOutliersParams[i].second;
        ASSERT_TRUE(!refRes.empty());

        Mat res;
        Ptr<AdaptiveManifoldFilter> amf = createAMFilter(sigma_s, sigma_r, true);
158
        amf->setUseRNG(false);
vludv's avatar
vludv committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        amf->filter(srcImg, res, srcImg);
        amf->collectGarbage();

        checkSimilarity(res, refRes);
    }
}

typedef tuple<string, string> AMRefTestParams;
typedef TestWithParam<AMRefTestParams> AdaptiveManifoldRefImplTest;

Ptr<AdaptiveManifoldFilter> createAMFilterRefImpl(double sigma_s, double sigma_r, bool adjust_outliers = false);

TEST_P(AdaptiveManifoldRefImplTest, RefImplAccuracy)
{
    AMRefTestParams params = GetParam();

    string guideFileName = get<0>(params);
    string srcFileName = get<1>(params);

    Mat guide = imread(getOpenCVExtraDir() + guideFileName);
    Mat src = imread(getOpenCVExtraDir() + srcFileName);
    ASSERT_TRUE(!guide.empty() && !src.empty());

182
    int seed = 10 * (int)guideFileName.length() + (int)srcFileName.length();
vludv's avatar
vludv committed
183 184 185 186 187 188 189 190
    RNG rnd(seed);

    //inconsistent downsample/upsample operations in reference implementation
    Size dstSize((guide.cols + 15) & ~15, (guide.rows + 15) & ~15);

    resize(guide, guide, dstSize);
    resize(src, src, dstSize);

191
    for (int iter = 0; iter < 4; iter++)
vludv's avatar
vludv committed
192 193 194 195 196
    {
        double sigma_s = rnd.uniform(1.0, 50.0);
        double sigma_r = rnd.uniform(0.1, 0.9);
        bool adjust_outliers = (iter % 2 == 0);

197
        cv::setNumThreads(cv::getNumberOfCPUs());
vludv's avatar
vludv committed
198 199 200
        Mat res;
        amFilter(guide, src, res, sigma_s, sigma_r, adjust_outliers);

201
        cv::setNumThreads(1);
vludv's avatar
vludv committed
202 203 204 205
        Mat resRef;
        Ptr<AdaptiveManifoldFilter> amf = createAMFilterRefImpl(sigma_s, sigma_r, adjust_outliers);
        amf->filter(src, resRef, guide);

206 207 208 209
        //results of reference implementation may differ on small sigma_s into small isolated region
        //due to low single-precision floating point numbers accuracy
        //therefore the threshold of inf norm was increased
        checkSimilarity(res, resRef, 25);
vludv's avatar
vludv committed
210 211 212
    }
}

213
INSTANTIATE_TEST_CASE_P(TypicalSet, AdaptiveManifoldRefImplTest,
vludv's avatar
vludv committed
214
    Combine(
215 216
    Values("cv/edgefilter/kodim23.png", "cv/npr/test4.png"),
    Values("cv/edgefilter/kodim23.png", "cv/npr/test4.png")
vludv's avatar
vludv committed
217 218
));

219
}