test_domain_transform.cpp 7.82 KB
Newer Older
vludv's avatar
vludv committed
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
/*
 *  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
 *  (3 - clause BSD License)
 *  
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met :
 *  
 *  *Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  
 *  * 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.
 *  
 *  * 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.
 *  
 *  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 45
using namespace std;
using namespace std::tr1;
using namespace testing;
using namespace perf;
46 47
using namespace cv;
using namespace cv::ximgproc;
vludv's avatar
vludv committed
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

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

CV_ENUM(SupportedTypes, CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4);
CV_ENUM(ModeType, DTF_NC, DTF_IC, DTF_RF)
typedef tuple<Size, ModeType, SupportedTypes, SupportedTypes> DTParams;

Mat convertTypeAndSize(Mat src, int dstType, Size dstSize)
{
    Mat dst;
    CV_Assert(src.channels() == 3);

    int dstChannels = CV_MAT_CN(dstType);

    if (dstChannels == 1)
    {
        cvtColor(src, dst, COLOR_BGR2GRAY);
    }
    else if (dstChannels == 2)
    {
        Mat srcCn[3];
        split(src, srcCn);
        merge(srcCn, 2, dst);
    }
    else if (dstChannels == 3)
    {
        dst = src.clone();
    }
    else if (dstChannels == 4)
    {
        Mat srcCn[4];
        split(src, srcCn);
        srcCn[3] = srcCn[0].clone();
        merge(srcCn, 4, dst);
    }

    dst.convertTo(dst, dstType);
    resize(dst, dst, dstSize);

    return dst;
}

TEST(DomainTransformTest, SplatSurfaceAccuracy)
{
    static int dtModes[] = {DTF_NC, DTF_RF, DTF_IC};
    RNG rnd(0);

    for (int i = 0; i < 15; i++)
    {
        Size sz(rnd.uniform(512, 1024), rnd.uniform(512, 1024));

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

        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, 100.0);
        double sigma_r = rnd.uniform(1.0, 100.0);
        int mode = dtModes[i%3];

        Mat res;
        dtFilter(guide, src, res, sigma_s, sigma_r, mode, 1);

        double normL1 = cvtest::norm(src, res, NORM_L1)/src.total()/src.channels();
        EXPECT_LE(normL1, 1.0/64);
    }
}

typedef TestWithParam<DTParams> DomainTransformTest;
TEST_P(DomainTransformTest, MultiThreadReproducibility)
{
    if (cv::getNumberOfCPUs() == 1)
        return;

    double MAX_DIF = 1.0;
    double MAX_MEAN_DIF = 1.0 / 256.0;
    int loopsCount = 2;
    RNG rng(0);

    DTParams params = GetParam();
    Size size = get<0>(params);
    int mode = get<1>(params);
    int guideType = get<2>(params);
    int srcType = get<3>(params);

    Mat original = imread(getOpenCVExtraDir() + "cv/edgefilter/statue.png");
    Mat guide = convertTypeAndSize(original, guideType, size);
    Mat src = convertTypeAndSize(original, srcType, size);

    for (int iter = 0; iter <= loopsCount; iter++)
    {
        double ss = rng.uniform(0.0, 100.0);
        double sc = rng.uniform(0.0, 100.0);

        cv::setNumThreads(cv::getNumberOfCPUs());
        Mat resMultithread;
        dtFilter(guide, src, resMultithread, ss, sc, mode);

        cv::setNumThreads(1);
        Mat resSingleThread;
        dtFilter(guide, src, resSingleThread, ss, sc, mode);

        EXPECT_LE(cv::norm(resSingleThread, resMultithread, NORM_INF), MAX_DIF);
        EXPECT_LE(cv::norm(resSingleThread, resMultithread, NORM_L1), MAX_MEAN_DIF*src.total());
    }
}

INSTANTIATE_TEST_CASE_P(FullSet, DomainTransformTest,
    Combine(Values(szODD, szQVGA), ModeType::all(), SupportedTypes::all(), SupportedTypes::all())
);

template<typename SrcVec>
Mat getChessMat1px(Size sz, double whiteIntensity = 255)
{
    typedef typename DataType<SrcVec>::channel_type SrcType;

    Mat dst(sz, DataType<SrcVec>::type);

    SrcVec black = SrcVec::all(0);
    SrcVec white = SrcVec::all((SrcType)whiteIntensity);

    for (int i = 0; i < dst.rows; i++)
        for (int j = 0; j < dst.cols; j++)
            dst.at<SrcVec>(i, j) = ((i + j) % 2) ? white : black;

    return dst;
}

TEST(DomainTransformTest, ChessBoard_NC_accuracy)
{
    RNG rng(0);
    double MAX_DIF = 1;
    Size sz = szVGA;
    double ss = 80;
    double sc = 60;

    Mat srcb = randomMat(rng, sz, CV_8UC4, 0, 255, true);
    Mat srcf = randomMat(rng, sz, CV_32FC4, 0, 255, true);
    Mat chessb = getChessMat1px<Vec3b>(sz);

    Mat dstb, dstf;
    dtFilter(chessb, srcb.clone(), dstb, ss, sc, DTF_NC);
    dtFilter(chessb, srcf.clone(), dstf, ss, sc, DTF_NC);

    EXPECT_LE(cv::norm(srcb, dstb, NORM_INF), MAX_DIF);
    EXPECT_LE(cv::norm(srcf, dstf, NORM_INF), MAX_DIF);
}

TEST(DomainTransformTest, BoxFilter_NC_accuracy)
{
    double MAX_DIF = 1;
    int radius = 5;
    double sc = 1.0;
    double ss = 1.01*radius / sqrt(3.0);

    Mat src = imread(getOpenCVExtraDir() + "cv/edgefilter/statue.png");
    ASSERT_TRUE(!src.empty());

    Mat1b guide(src.size(), 200);
    Mat res_dt, res_box;

    blur(src, res_box, Size(2 * radius + 1, 2 * radius + 1));
    dtFilter(guide, src, res_dt, ss, sc, DTF_NC, 1);

    EXPECT_LE(cv::norm(res_dt, res_box, NORM_L2), MAX_DIF*src.total());
}

TEST(DomainTransformTest, AuthorReferenceAccuracy)
{
    string dir = getOpenCVExtraDir() + "cv/edgefilter";
    double ss = 30;
    double sc = 0.2 * 255;

    Mat src = imread(dir + "/statue.png");
    Mat ref_NC = imread(dir + "/dt/authors_statue_NC_ss30_sc0.2.png");
    Mat ref_IC = imread(dir + "/dt/authors_statue_IC_ss30_sc0.2.png");
    Mat ref_RF = imread(dir + "/dt/authors_statue_RF_ss30_sc0.2.png");

    ASSERT_FALSE(src.empty());
    ASSERT_FALSE(ref_NC.empty());
    ASSERT_FALSE(ref_IC.empty());
    ASSERT_FALSE(ref_RF.empty());

    cv::setNumThreads(cv::getNumberOfCPUs());
    Mat res_NC, res_IC, res_RF;
    dtFilter(src, src, res_NC, ss, sc, DTF_NC);
    dtFilter(src, src, res_IC, ss, sc, DTF_IC);
    dtFilter(src, src, res_RF, ss, sc, DTF_RF);

    double totalMaxError = 1.0/64.0*src.total();
    
    EXPECT_LE(cvtest::norm(res_NC, ref_NC, NORM_L2), totalMaxError);
    EXPECT_LE(cvtest::norm(res_NC, ref_NC, NORM_INF), 1);

    EXPECT_LE(cvtest::norm(res_IC, ref_IC, NORM_L2), totalMaxError);
    EXPECT_LE(cvtest::norm(res_IC, ref_IC, NORM_INF), 1);

    EXPECT_LE(cvtest::norm(res_RF, ref_RF, NORM_L2), totalMaxError);
    EXPECT_LE(cvtest::norm(res_IC, ref_IC, NORM_INF), 1);
}

}