1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "test_precomp.hpp"
45 class AllignedFrameSource : public cv::superres::FrameSource
48 AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
50 void nextFrame(cv::OutputArray frame);
54 cv::Ptr<cv::superres::FrameSource> base_;
59 AllignedFrameSource::AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
60 base_(base), scale_(scale)
62 CV_Assert( !base_.empty() );
65 void AllignedFrameSource::nextFrame(cv::OutputArray frame)
67 base_->nextFrame(origFrame_);
69 if (origFrame_.rows % scale_ == 0 && origFrame_.cols % scale_ == 0)
71 cv::superres::arrCopy(origFrame_, frame);
75 cv::Rect ROI(0, 0, (origFrame_.cols / scale_) * scale_, (origFrame_.rows / scale_) * scale_);
76 cv::superres::arrCopy(origFrame_(ROI), frame);
80 void AllignedFrameSource::reset()
85 class DegradeFrameSource : public cv::superres::FrameSource
88 DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
90 void nextFrame(cv::OutputArray frame);
94 cv::Ptr<cv::superres::FrameSource> base_;
101 DegradeFrameSource::DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
102 base_(base), iscale_(1.0 / scale)
104 CV_Assert( !base_.empty() );
107 void addGaussNoise(cv::Mat& image, double sigma)
109 cv::Mat noise(image.size(), CV_32FC(image.channels()));
110 cvtest::TS::ptr()->get_rng().fill(noise, cv::RNG::NORMAL, 0.0, sigma);
112 cv::addWeighted(image, 1.0, noise, 1.0, 0.0, image, image.depth());
115 void addSpikeNoise(cv::Mat& image, int frequency)
117 cv::Mat_<uchar> mask(image.size(), 0);
119 for (int y = 0; y < mask.rows; ++y)
121 for (int x = 0; x < mask.cols; ++x)
123 if (cvtest::TS::ptr()->get_rng().uniform(0, frequency) < 1)
128 image.setTo(cv::Scalar::all(255), mask);
131 void DegradeFrameSource::nextFrame(cv::OutputArray frame)
133 base_->nextFrame(origFrame_);
135 cv::GaussianBlur(origFrame_, blurred_, cv::Size(5, 5), 0);
136 cv::resize(blurred_, deg_, cv::Size(), iscale_, iscale_, cv::INTER_NEAREST);
138 addGaussNoise(deg_, 10.0);
139 addSpikeNoise(deg_, 500);
141 cv::superres::arrCopy(deg_, frame);
144 void DegradeFrameSource::reset()
149 double MSSIM(const cv::Mat& i1, const cv::Mat& i2)
151 const double C1 = 6.5025;
152 const double C2 = 58.5225;
154 const int depth = CV_32F;
157 i1.convertTo(I1, depth);
158 i2.convertTo(I2, depth);
160 cv::Mat I2_2 = I2.mul(I2); // I2^2
161 cv::Mat I1_2 = I1.mul(I1); // I1^2
162 cv::Mat I1_I2 = I1.mul(I2); // I1 * I2
165 cv::GaussianBlur(I1, mu1, cv::Size(11, 11), 1.5);
166 cv::GaussianBlur(I2, mu2, cv::Size(11, 11), 1.5);
168 cv::Mat mu1_2 = mu1.mul(mu1);
169 cv::Mat mu2_2 = mu2.mul(mu2);
170 cv::Mat mu1_mu2 = mu1.mul(mu2);
172 cv::Mat sigma1_2, sigma2_2, sigma12;
174 cv::GaussianBlur(I1_2, sigma1_2, cv::Size(11, 11), 1.5);
177 cv::GaussianBlur(I2_2, sigma2_2, cv::Size(11, 11), 1.5);
180 cv::GaussianBlur(I1_I2, sigma12, cv::Size(11, 11), 1.5);
187 // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
188 t1 = 2 * mu1_mu2 + C1;
189 t2 = 2 * sigma12 + C2;
190 numerator = t1.mul(t2);
192 // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
193 t1 = mu1_2 + mu2_2 + C1;
194 t2 = sigma1_2 + sigma2_2 + C2;
195 denominator = t1.mul(t2);
197 // ssim_map = numerator./denominator;
199 cv::divide(numerator, denominator, ssim_map);
201 // mssim = average of ssim map
202 cv::Scalar mssim = cv::mean(ssim_map);
204 if (i1.channels() == 1)
207 return (mssim[0] + mssim[1] + mssim[3]) / 3;
210 class SuperResolution : public testing::Test
213 void RunTest(cv::Ptr<cv::superres::SuperResolution> superRes);
216 void SuperResolution::RunTest(cv::Ptr<cv::superres::SuperResolution> superRes)
218 const std::string inputVideoName = cvtest::TS::ptr()->get_data_path() + "car.avi";
220 const int iterations = 100;
221 const int temporalAreaRadius = 2;
223 ASSERT_FALSE( superRes.empty() );
225 const int btvKernelSize = superRes->getInt("btvKernelSize");
227 superRes->set("scale", scale);
228 superRes->set("iterations", iterations);
229 superRes->set("temporalAreaRadius", temporalAreaRadius);
231 cv::Ptr<cv::superres::FrameSource> goldSource(new AllignedFrameSource(cv::superres::createFrameSource_Video(inputVideoName), scale));
232 cv::Ptr<cv::superres::FrameSource> lowResSource(new DegradeFrameSource(new AllignedFrameSource(cv::superres::createFrameSource_Video(inputVideoName), scale), scale));
237 lowResSource->nextFrame(frame);
238 goldSource->nextFrame(frame);
240 cv::Rect inner(btvKernelSize, btvKernelSize, frame.cols - 2 * btvKernelSize, frame.rows - 2 * btvKernelSize);
242 superRes->setInput(lowResSource);
244 double srAvgMSSIM = 0.0;
245 const int count = 10;
247 cv::Mat goldFrame, superResFrame;
248 for (int i = 0; i < count; ++i)
250 goldSource->nextFrame(goldFrame);
251 ASSERT_FALSE( goldFrame.empty() );
253 superRes->nextFrame(superResFrame);
254 ASSERT_FALSE( superResFrame.empty() );
256 const double srMSSIM = MSSIM(goldFrame(inner), superResFrame);
258 srAvgMSSIM += srMSSIM;
263 EXPECT_GE( srAvgMSSIM, 0.5 );
266 TEST_F(SuperResolution, BTVL1)
268 RunTest(cv::superres::createSuperResolution_BTVL1());
271 #if defined(HAVE_OPENCV_GPU) && defined(HAVE_CUDA)
273 TEST_F(SuperResolution, BTVL1_GPU)
275 RunTest(cv::superres::createSuperResolution_BTVL1_GPU());