test_superres.cpp 8.6 KB

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