Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/superres/test/test_superres.cpp
16344 views
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
#include "cvconfig.h"
45
#include "../src/input_array_utility.hpp"
46
#include "opencv2/ts/ocl_test.hpp"
47
48
namespace opencv_test {
49
50
#ifdef HAVE_VIDEO_INPUT
51
52
namespace {
53
54
class AllignedFrameSource : public cv::superres::FrameSource
55
{
56
public:
57
AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
58
59
void nextFrame(cv::OutputArray frame);
60
void reset();
61
62
private:
63
cv::Ptr<cv::superres::FrameSource> base_;
64
65
cv::Mat origFrame_;
66
int scale_;
67
};
68
69
AllignedFrameSource::AllignedFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
70
base_(base), scale_(scale)
71
{
72
CV_Assert( base_ );
73
}
74
75
void AllignedFrameSource::nextFrame(cv::OutputArray frame)
76
{
77
base_->nextFrame(origFrame_);
78
79
if (origFrame_.rows % scale_ == 0 && origFrame_.cols % scale_ == 0)
80
cv::superres::arrCopy(origFrame_, frame);
81
else
82
{
83
cv::Rect ROI(0, 0, (origFrame_.cols / scale_) * scale_, (origFrame_.rows / scale_) * scale_);
84
cv::superres::arrCopy(origFrame_(ROI), frame);
85
}
86
}
87
88
void AllignedFrameSource::reset()
89
{
90
base_->reset();
91
}
92
93
class DegradeFrameSource : public cv::superres::FrameSource
94
{
95
public:
96
DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale);
97
98
void nextFrame(cv::OutputArray frame);
99
void reset();
100
101
private:
102
cv::Ptr<cv::superres::FrameSource> base_;
103
104
cv::Mat origFrame_;
105
cv::Mat blurred_;
106
cv::Mat deg_;
107
double iscale_;
108
};
109
110
DegradeFrameSource::DegradeFrameSource(const cv::Ptr<cv::superres::FrameSource>& base, int scale) :
111
base_(base), iscale_(1.0 / scale)
112
{
113
CV_Assert( base_ );
114
}
115
116
static void addGaussNoise(cv::OutputArray _image, double sigma)
117
{
118
int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
119
cv::Mat noise(_image.size(), CV_32FC(cn));
120
cvtest::TS::ptr()->get_rng().fill(noise, cv::RNG::NORMAL, 0.0, sigma);
121
122
cv::addWeighted(_image, 1.0, noise, 1.0, 0.0, _image, depth);
123
}
124
125
static void addSpikeNoise(cv::OutputArray _image, int frequency)
126
{
127
cv::Mat_<uchar> mask(_image.size(), 0);
128
129
for (int y = 0; y < mask.rows; ++y)
130
for (int x = 0; x < mask.cols; ++x)
131
if (cvtest::TS::ptr()->get_rng().uniform(0, frequency) < 1)
132
mask(y, x) = 255;
133
134
_image.setTo(cv::Scalar::all(255), mask);
135
}
136
137
void DegradeFrameSource::nextFrame(cv::OutputArray frame)
138
{
139
base_->nextFrame(origFrame_);
140
141
cv::GaussianBlur(origFrame_, blurred_, cv::Size(5, 5), 0);
142
cv::resize(blurred_, deg_, cv::Size(), iscale_, iscale_, cv::INTER_NEAREST);
143
144
addGaussNoise(deg_, 10.0);
145
addSpikeNoise(deg_, 500);
146
147
cv::superres::arrCopy(deg_, frame);
148
}
149
150
void DegradeFrameSource::reset()
151
{
152
base_->reset();
153
}
154
155
double MSSIM(cv::InputArray _i1, cv::InputArray _i2)
156
{
157
const double C1 = 6.5025;
158
const double C2 = 58.5225;
159
160
const int depth = CV_32F;
161
162
cv::Mat I1, I2;
163
_i1.getMat().convertTo(I1, depth);
164
_i2.getMat().convertTo(I2, depth);
165
166
cv::Mat I2_2 = I2.mul(I2); // I2^2
167
cv::Mat I1_2 = I1.mul(I1); // I1^2
168
cv::Mat I1_I2 = I1.mul(I2); // I1 * I2
169
170
cv::Mat mu1, mu2;
171
cv::GaussianBlur(I1, mu1, cv::Size(11, 11), 1.5);
172
cv::GaussianBlur(I2, mu2, cv::Size(11, 11), 1.5);
173
174
cv::Mat mu1_2 = mu1.mul(mu1);
175
cv::Mat mu2_2 = mu2.mul(mu2);
176
cv::Mat mu1_mu2 = mu1.mul(mu2);
177
178
cv::Mat sigma1_2, sigma2_2, sigma12;
179
180
cv::GaussianBlur(I1_2, sigma1_2, cv::Size(11, 11), 1.5);
181
sigma1_2 -= mu1_2;
182
183
cv::GaussianBlur(I2_2, sigma2_2, cv::Size(11, 11), 1.5);
184
sigma2_2 -= mu2_2;
185
186
cv::GaussianBlur(I1_I2, sigma12, cv::Size(11, 11), 1.5);
187
sigma12 -= mu1_mu2;
188
189
cv::Mat t1, t2;
190
cv::Mat numerator;
191
cv::Mat denominator;
192
193
// t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
194
t1 = 2 * mu1_mu2 + C1;
195
t2 = 2 * sigma12 + C2;
196
numerator = t1.mul(t2);
197
198
// t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
199
t1 = mu1_2 + mu2_2 + C1;
200
t2 = sigma1_2 + sigma2_2 + C2;
201
denominator = t1.mul(t2);
202
203
// ssim_map = numerator./denominator;
204
cv::Mat ssim_map;
205
cv::divide(numerator, denominator, ssim_map);
206
207
// mssim = average of ssim map
208
cv::Scalar mssim = cv::mean(ssim_map);
209
210
if (_i1.channels() == 1)
211
return mssim[0];
212
213
return (mssim[0] + mssim[1] + mssim[3]) / 3;
214
}
215
216
class SuperResolution : public testing::Test
217
{
218
public:
219
template <typename T>
220
void RunTest(cv::Ptr<cv::superres::SuperResolution> superRes);
221
};
222
223
template <typename T>
224
void SuperResolution::RunTest(cv::Ptr<cv::superres::SuperResolution> superRes)
225
{
226
const std::string inputVideoName = cvtest::TS::ptr()->get_data_path() + "car.avi";
227
const int scale = 2;
228
const int iterations = 100;
229
const int temporalAreaRadius = 2;
230
231
ASSERT_FALSE( superRes.empty() );
232
233
const int btvKernelSize = superRes->getKernelSize();
234
235
superRes->setScale(scale);
236
superRes->setIterations(iterations);
237
superRes->setTemporalAreaRadius(temporalAreaRadius);
238
239
cv::Ptr<cv::superres::FrameSource> goldSource(new AllignedFrameSource(cv::superres::createFrameSource_Video(inputVideoName), scale));
240
cv::Ptr<cv::superres::FrameSource> lowResSource(new DegradeFrameSource(
241
cv::makePtr<AllignedFrameSource>(cv::superres::createFrameSource_Video(inputVideoName), scale), scale));
242
243
// skip first frame
244
cv::Mat frame;
245
246
lowResSource->nextFrame(frame);
247
goldSource->nextFrame(frame);
248
249
cv::Rect inner(btvKernelSize, btvKernelSize, frame.cols - 2 * btvKernelSize, frame.rows - 2 * btvKernelSize);
250
251
superRes->setInput(lowResSource);
252
253
double srAvgMSSIM = 0.0;
254
const int count = 10;
255
256
cv::Mat goldFrame;
257
T superResFrame;
258
for (int i = 0; i < count; ++i)
259
{
260
goldSource->nextFrame(goldFrame);
261
ASSERT_FALSE( goldFrame.empty() );
262
263
superRes->nextFrame(superResFrame);
264
ASSERT_FALSE( superResFrame.empty() );
265
266
const double srMSSIM = MSSIM(goldFrame(inner), superResFrame);
267
268
srAvgMSSIM += srMSSIM;
269
}
270
271
srAvgMSSIM /= count;
272
273
EXPECT_GE( srAvgMSSIM, 0.5 );
274
}
275
276
TEST_F(SuperResolution, BTVL1)
277
{
278
RunTest<cv::Mat>(cv::superres::createSuperResolution_BTVL1());
279
}
280
281
#if defined(HAVE_CUDA) && defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING) && defined(HAVE_OPENCV_CUDAFILTERS)
282
283
TEST_F(SuperResolution, BTVL1_CUDA)
284
{
285
RunTest<cv::Mat>(cv::superres::createSuperResolution_BTVL1_CUDA());
286
}
287
288
#endif
289
290
} // namespace
291
292
#ifdef HAVE_OPENCL
293
294
namespace ocl {
295
296
OCL_TEST_F(SuperResolution, BTVL1)
297
{
298
RunTest<cv::UMat>(cv::superres::createSuperResolution_BTVL1());
299
}
300
301
} // namespace opencv_test::ocl
302
303
#endif
304
305
#endif // HAVE_VIDEO_INPUT
306
307
} // namespace
308
309