test_superres.cpp 8.51 KB
Newer Older
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
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// 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*/

43
#include "test_precomp.hpp"
Ilya Lavrenov's avatar
Ilya Lavrenov committed
44
#include "opencv2/ts/ocl_test.hpp"
45

46
#ifdef HAVE_VIDEO_INPUT
47

48 49 50 51 52 53 54 55 56 57
class AllignedFrameSource : public cv::superres::FrameSource
{
public:
    AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);

    void nextFrame(cv::OutputArray frame);
    void reset();

private:
    cv::Ptr<cv::superres::FrameSource> base_;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
58

59 60 61 62 63 64 65
    cv::Mat origFrame_;
    int scale_;
};

AllignedFrameSource::AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
    base_(base), scale_(scale)
{
66
    CV_Assert( base_ );
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
}

void AllignedFrameSource::nextFrame(cv::OutputArray frame)
{
    base_->nextFrame(origFrame_);

    if (origFrame_.rows % scale_ == 0 && origFrame_.cols % scale_ == 0)
        cv::superres::arrCopy(origFrame_, frame);
    else
    {
        cv::Rect ROI(0, 0, (origFrame_.cols / scale_) * scale_, (origFrame_.rows / scale_) * scale_);
        cv::superres::arrCopy(origFrame_(ROI), frame);
    }
}

void AllignedFrameSource::reset()
{
    base_->reset();
}

class DegradeFrameSource : public cv::superres::FrameSource
{
public:
    DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);

    void nextFrame(cv::OutputArray frame);
    void reset();

private:
    cv::Ptr<cv::superres::FrameSource> base_;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
97

98 99 100 101 102 103 104 105 106
    cv::Mat origFrame_;
    cv::Mat blurred_;
    cv::Mat deg_;
    double iscale_;
};

DegradeFrameSource::DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
    base_(base), iscale_(1.0 / scale)
{
107
    CV_Assert( base_ );
108 109
}

Ilya Lavrenov's avatar
Ilya Lavrenov committed
110
static void addGaussNoise(cv::OutputArray _image, double sigma)
111
{
Ilya Lavrenov's avatar
Ilya Lavrenov committed
112 113
    int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
    cv::Mat noise(_image.size(), CV_32FC(cn));
114 115
    cvtest::TS::ptr()->get_rng().fill(noise, cv::RNG::NORMAL, 0.0, sigma);

Ilya Lavrenov's avatar
Ilya Lavrenov committed
116
    cv::addWeighted(_image, 1.0, noise, 1.0, 0.0, _image, depth);
117 118
}

Ilya Lavrenov's avatar
Ilya Lavrenov committed
119
static void addSpikeNoise(cv::OutputArray _image, int frequency)
120
{
Ilya Lavrenov's avatar
Ilya Lavrenov committed
121
    cv::Mat_<uchar> mask(_image.size(), 0);
122 123 124 125 126 127

    for (int y = 0; y < mask.rows; ++y)
        for (int x = 0; x < mask.cols; ++x)
            if (cvtest::TS::ptr()->get_rng().uniform(0, frequency) < 1)
                mask(y, x) = 255;

Ilya Lavrenov's avatar
Ilya Lavrenov committed
128
    _image.setTo(cv::Scalar::all(255), mask);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

void DegradeFrameSource::nextFrame(cv::OutputArray frame)
{
    base_->nextFrame(origFrame_);

    cv::GaussianBlur(origFrame_, blurred_, cv::Size(5, 5), 0);
    cv::resize(blurred_, deg_, cv::Size(), iscale_, iscale_, cv::INTER_NEAREST);

    addGaussNoise(deg_, 10.0);
    addSpikeNoise(deg_, 500);

    cv::superres::arrCopy(deg_, frame);
}

void DegradeFrameSource::reset()
{
    base_->reset();
}

Ilya Lavrenov's avatar
Ilya Lavrenov committed
149
double MSSIM(cv::InputArray _i1, cv::InputArray _i2)
150 151 152 153 154 155 156
{
    const double C1 = 6.5025;
    const double C2 = 58.5225;

    const int depth = CV_32F;

    cv::Mat I1, I2;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
157 158
    _i1.getMat().convertTo(I1, depth);
    _i2.getMat().convertTo(I2, depth);
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

    cv::Mat I2_2  = I2.mul(I2); // I2^2
    cv::Mat I1_2  = I1.mul(I1); // I1^2
    cv::Mat I1_I2 = I1.mul(I2); // I1 * I2

    cv::Mat mu1, mu2;
    cv::GaussianBlur(I1, mu1, cv::Size(11, 11), 1.5);
    cv::GaussianBlur(I2, mu2, cv::Size(11, 11), 1.5);

    cv::Mat mu1_2   = mu1.mul(mu1);
    cv::Mat mu2_2   = mu2.mul(mu2);
    cv::Mat mu1_mu2 = mu1.mul(mu2);

    cv::Mat sigma1_2, sigma2_2, sigma12;

    cv::GaussianBlur(I1_2, sigma1_2, cv::Size(11, 11), 1.5);
    sigma1_2 -= mu1_2;

    cv::GaussianBlur(I2_2, sigma2_2, cv::Size(11, 11), 1.5);
    sigma2_2 -= mu2_2;

    cv::GaussianBlur(I1_I2, sigma12, cv::Size(11, 11), 1.5);
    sigma12 -= mu1_mu2;

    cv::Mat t1, t2;
    cv::Mat numerator;
    cv::Mat denominator;

    // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
    t1 = 2 * mu1_mu2 + C1;
    t2 = 2 * sigma12 + C2;
    numerator = t1.mul(t2);

    // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
    t1 = mu1_2 + mu2_2 + C1;
    t2 = sigma1_2 + sigma2_2 + C2;
    denominator = t1.mul(t2);

    // ssim_map =  numerator./denominator;
    cv::Mat ssim_map;
    cv::divide(numerator, denominator, ssim_map);

    // mssim = average of ssim map
    cv::Scalar mssim = cv::mean(ssim_map);

Ilya Lavrenov's avatar
Ilya Lavrenov committed
204
    if (_i1.channels() == 1)
205 206 207 208 209 210 211 212
        return mssim[0];

    return (mssim[0] + mssim[1] + mssim[3]) / 3;
}

class SuperResolution : public testing::Test
{
public:
Ilya Lavrenov's avatar
Ilya Lavrenov committed
213
    template <typename T>
214 215 216
    void RunTest(cv::Ptr<cv::superres::SuperResolution> superRes);
};

Ilya Lavrenov's avatar
Ilya Lavrenov committed
217
template <typename T>
218 219 220 221 222 223 224 225 226
void SuperResolution::RunTest(cv::Ptr<cv::superres::SuperResolution> superRes)
{
    const std::string inputVideoName = cvtest::TS::ptr()->get_data_path() + "car.avi";
    const int scale = 2;
    const int iterations = 100;
    const int temporalAreaRadius = 2;

    ASSERT_FALSE( superRes.empty() );

227
    const int btvKernelSize = superRes->getKernelSize();
228

229 230 231
    superRes->setScale(scale);
    superRes->setIterations(iterations);
    superRes->setTemporalAreaRadius(temporalAreaRadius);
232 233

    cv::Ptr<cv::superres::FrameSource> goldSource(new AllignedFrameSource(cv::superres::createFrameSource_Video(inputVideoName), scale));
234 235
    cv::Ptr<cv::superres::FrameSource> lowResSource(new DegradeFrameSource(
        cv::makePtr<AllignedFrameSource>(cv::superres::createFrameSource_Video(inputVideoName), scale), scale));
236 237 238 239 240 241 242 243 244 245 246 247 248 249

    // skip first frame
    cv::Mat frame;

    lowResSource->nextFrame(frame);
    goldSource->nextFrame(frame);

    cv::Rect inner(btvKernelSize, btvKernelSize, frame.cols - 2 * btvKernelSize, frame.rows - 2 * btvKernelSize);

    superRes->setInput(lowResSource);

    double srAvgMSSIM = 0.0;
    const int count = 10;

Ilya Lavrenov's avatar
Ilya Lavrenov committed
250 251
    cv::Mat goldFrame;
    T superResFrame;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    for (int i = 0; i < count; ++i)
    {
        goldSource->nextFrame(goldFrame);
        ASSERT_FALSE( goldFrame.empty() );

        superRes->nextFrame(superResFrame);
        ASSERT_FALSE( superResFrame.empty() );

        const double srMSSIM = MSSIM(goldFrame(inner), superResFrame);

        srAvgMSSIM += srMSSIM;
    }

    srAvgMSSIM /= count;

    EXPECT_GE( srAvgMSSIM, 0.5 );
}

TEST_F(SuperResolution, BTVL1)
{
Ilya Lavrenov's avatar
Ilya Lavrenov committed
272
    RunTest<cv::Mat>(cv::superres::createSuperResolution_BTVL1());
273 274
}

275
#if defined(HAVE_CUDA) && defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING) && defined(HAVE_OPENCV_CUDAFILTERS)
276

277
TEST_F(SuperResolution, BTVL1_CUDA)
278
{
Ilya Lavrenov's avatar
Ilya Lavrenov committed
279
    RunTest<cv::Mat>(cv::superres::createSuperResolution_BTVL1_CUDA());
280
}
281

282
#endif
283

Ilya Lavrenov's avatar
Ilya Lavrenov committed
284
#ifdef HAVE_OPENCL
285

Ilya Lavrenov's avatar
Ilya Lavrenov committed
286 287 288 289
namespace cvtest {
namespace ocl {

OCL_TEST_F(SuperResolution, BTVL1)
290
{
Ilya Lavrenov's avatar
Ilya Lavrenov committed
291
    RunTest<cv::UMat>(cv::superres::createSuperResolution_BTVL1());
292
}
293

Ilya Lavrenov's avatar
Ilya Lavrenov committed
294 295
} } // namespace cvtest::ocl

296
#endif
297

298
#endif // HAVE_VIDEO_INPUT