Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / superres / test / test_superres.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 class AllignedFrameSource : public cv::superres::FrameSource
46 {
47 public:
48     AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
49
50     void nextFrame(cv::OutputArray frame);
51     void reset();
52
53 private:
54     cv::Ptr<cv::superres::FrameSource> base_;
55     cv::Mat origFrame_;
56     int scale_;
57 };
58
59 AllignedFrameSource::AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
60     base_(base), scale_(scale)
61 {
62     CV_Assert( !base_.empty() );
63 }
64
65 void AllignedFrameSource::nextFrame(cv::OutputArray frame)
66 {
67     base_->nextFrame(origFrame_);
68
69     if (origFrame_.rows % scale_ == 0 && origFrame_.cols % scale_ == 0)
70     {
71         cv::superres::arrCopy(origFrame_, frame);
72     }
73     else
74     {
75         cv::Rect ROI(0, 0, (origFrame_.cols / scale_) * scale_, (origFrame_.rows / scale_) * scale_);
76         cv::superres::arrCopy(origFrame_(ROI), frame);
77     }
78 }
79
80 void AllignedFrameSource::reset()
81 {
82     base_->reset();
83 }
84
85 class DegradeFrameSource : public cv::superres::FrameSource
86 {
87 public:
88     DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
89
90     void nextFrame(cv::OutputArray frame);
91     void reset();
92
93 private:
94     cv::Ptr<cv::superres::FrameSource> base_;
95     cv::Mat origFrame_;
96     cv::Mat blurred_;
97     cv::Mat deg_;
98     double iscale_;
99 };
100
101 DegradeFrameSource::DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
102     base_(base), iscale_(1.0 / scale)
103 {
104     CV_Assert( !base_.empty() );
105 }
106
107 void addGaussNoise(cv::Mat& image, double sigma)
108 {
109     cv::Mat noise(image.size(), CV_32FC(image.channels()));
110     cvtest::TS::ptr()->get_rng().fill(noise, cv::RNG::NORMAL, 0.0, sigma);
111
112     cv::addWeighted(image, 1.0, noise, 1.0, 0.0, image, image.depth());
113 }
114
115 void addSpikeNoise(cv::Mat& image, int frequency)
116 {
117     cv::Mat_<uchar> mask(image.size(), 0);
118
119     for (int y = 0; y < mask.rows; ++y)
120     {
121         for (int x = 0; x < mask.cols; ++x)
122         {
123             if (cvtest::TS::ptr()->get_rng().uniform(0, frequency) < 1)
124                 mask(y, x) = 255;
125         }
126     }
127
128     image.setTo(cv::Scalar::all(255), mask);
129 }
130
131 void DegradeFrameSource::nextFrame(cv::OutputArray frame)
132 {
133     base_->nextFrame(origFrame_);
134
135     cv::GaussianBlur(origFrame_, blurred_, cv::Size(5, 5), 0);
136     cv::resize(blurred_, deg_, cv::Size(), iscale_, iscale_, cv::INTER_NEAREST);
137
138     addGaussNoise(deg_, 10.0);
139     addSpikeNoise(deg_, 500);
140
141     cv::superres::arrCopy(deg_, frame);
142 }
143
144 void DegradeFrameSource::reset()
145 {
146     base_->reset();
147 }
148
149 double MSSIM(const cv::Mat& i1, const cv::Mat& i2)
150 {
151     const double C1 = 6.5025;
152     const double C2 = 58.5225;
153
154     const int depth = CV_32F;
155
156     cv::Mat I1, I2;
157     i1.convertTo(I1, depth);
158     i2.convertTo(I2, depth);
159
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
163
164     cv::Mat mu1, mu2;
165     cv::GaussianBlur(I1, mu1, cv::Size(11, 11), 1.5);
166     cv::GaussianBlur(I2, mu2, cv::Size(11, 11), 1.5);
167
168     cv::Mat mu1_2   = mu1.mul(mu1);
169     cv::Mat mu2_2   = mu2.mul(mu2);
170     cv::Mat mu1_mu2 = mu1.mul(mu2);
171
172     cv::Mat sigma1_2, sigma2_2, sigma12;
173
174     cv::GaussianBlur(I1_2, sigma1_2, cv::Size(11, 11), 1.5);
175     sigma1_2 -= mu1_2;
176
177     cv::GaussianBlur(I2_2, sigma2_2, cv::Size(11, 11), 1.5);
178     sigma2_2 -= mu2_2;
179
180     cv::GaussianBlur(I1_I2, sigma12, cv::Size(11, 11), 1.5);
181     sigma12 -= mu1_mu2;
182
183     cv::Mat t1, t2;
184     cv::Mat numerator;
185     cv::Mat denominator;
186
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);
191
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);
196
197     // ssim_map =  numerator./denominator;
198     cv::Mat ssim_map;
199     cv::divide(numerator, denominator, ssim_map);
200
201     // mssim = average of ssim map
202     cv::Scalar mssim = cv::mean(ssim_map);
203
204     if (i1.channels() == 1)
205         return mssim[0];
206
207     return (mssim[0] + mssim[1] + mssim[3]) / 3;
208 }
209
210 class SuperResolution : public testing::Test
211 {
212 public:
213     void RunTest(cv::Ptr<cv::superres::SuperResolution> superRes);
214 };
215
216 void SuperResolution::RunTest(cv::Ptr<cv::superres::SuperResolution> superRes)
217 {
218     const std::string inputVideoName = cvtest::TS::ptr()->get_data_path() + "car.avi";
219     const int scale = 2;
220     const int iterations = 100;
221     const int temporalAreaRadius = 2;
222
223     ASSERT_FALSE( superRes.empty() );
224
225     const int btvKernelSize = superRes->getInt("btvKernelSize");
226
227     superRes->set("scale", scale);
228     superRes->set("iterations", iterations);
229     superRes->set("temporalAreaRadius", temporalAreaRadius);
230
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));
233
234     // skip first frame
235     cv::Mat frame;
236
237     lowResSource->nextFrame(frame);
238     goldSource->nextFrame(frame);
239
240     cv::Rect inner(btvKernelSize, btvKernelSize, frame.cols - 2 * btvKernelSize, frame.rows - 2 * btvKernelSize);
241
242     superRes->setInput(lowResSource);
243
244     double srAvgMSSIM = 0.0;
245     const int count = 10;
246
247     cv::Mat goldFrame, superResFrame;
248     for (int i = 0; i < count; ++i)
249     {
250         goldSource->nextFrame(goldFrame);
251         ASSERT_FALSE( goldFrame.empty() );
252
253         superRes->nextFrame(superResFrame);
254         ASSERT_FALSE( superResFrame.empty() );
255
256         const double srMSSIM = MSSIM(goldFrame(inner), superResFrame);
257
258         srAvgMSSIM += srMSSIM;
259     }
260
261     srAvgMSSIM /= count;
262
263     EXPECT_GE( srAvgMSSIM, 0.5 );
264 }
265
266 TEST_F(SuperResolution, BTVL1)
267 {
268     RunTest(cv::superres::createSuperResolution_BTVL1());
269 }
270
271 #if defined(HAVE_OPENCV_GPU) && defined(HAVE_CUDA)
272
273 TEST_F(SuperResolution, BTVL1_GPU)
274 {
275     RunTest(cv::superres::createSuperResolution_BTVL1_GPU());
276 }
277 #endif
278 #if defined(HAVE_OPENCV_OCL) && defined(HAVE_OPENCL)
279 TEST_F(SuperResolution, BTVL1_OCL)
280 {
281     std::vector<cv::ocl::Info> infos;
282     cv::ocl::getDevice(infos);
283     RunTest(cv::superres::createSuperResolution_BTVL1_OCL());
284 }
285 #endif