test_denoise_tvl1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the OpenCV Foundation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng)
  44. {
  45. noisy.create(img.size(), img.type());
  46. cv::Mat noise(img.size(), img.type()), mask(img.size(), CV_8U);
  47. rng.fill(noise,cv::RNG::NORMAL,128.0,sigma);
  48. cv::addWeighted(img, 1, noise, 1, -128, noisy);
  49. cv::randn(noise, cv::Scalar::all(0), cv::Scalar::all(2));
  50. noise *= 255;
  51. cv::randu(mask, 0, cvRound(1./pepper_salt_ratio));
  52. cv::Mat half = mask.colRange(0, img.cols/2);
  53. half = cv::Scalar::all(1);
  54. noise.setTo(128, mask);
  55. cv::addWeighted(noisy, 1, noise, 1, -128, noisy);
  56. }
  57. #if 0
  58. void make_spotty(cv::Mat& img,cv::RNG& rng, int r=3,int n=1000)
  59. {
  60. for(int i=0;i<n;i++)
  61. {
  62. int x=rng(img.cols-r),y=rng(img.rows-r);
  63. if(rng(2)==0)
  64. img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)0;
  65. else
  66. img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)255;
  67. }
  68. }
  69. #endif
  70. bool validate_pixel(const cv::Mat& image,int x,int y,uchar val)
  71. {
  72. bool ok = std::abs(image.at<uchar>(x,y) - val) < 10;
  73. printf("test: image(%d,%d)=%d vs %d - %s\n",x,y,(int)image.at<uchar>(x,y),val,ok?"ok":"bad");
  74. return ok;
  75. }
  76. TEST(Optim_denoise_tvl1, regression_basic)
  77. {
  78. cv::RNG rng(42);
  79. cv::Mat img = cv::imread(cvtest::TS::ptr()->get_data_path() + "shared/lena.png", 0), noisy, res;
  80. ASSERT_FALSE(img.empty()) << "Error: can't open 'lena.png'";
  81. const int obs_num=5;
  82. std::vector<cv::Mat> images(obs_num, cv::Mat());
  83. for(int i=0;i<(int)images.size();i++)
  84. {
  85. make_noisy(img,images[i], 20, 0.02,rng);
  86. //make_spotty(images[i],rng);
  87. }
  88. //cv::imshow("test", images[0]);
  89. cv::denoise_TVL1(images, res);
  90. //cv::imshow("denoised", res);
  91. //cv::waitKey();
  92. #if 0
  93. ASSERT_TRUE(validate_pixel(res,248,334,179));
  94. ASSERT_TRUE(validate_pixel(res,489,333,172));
  95. ASSERT_TRUE(validate_pixel(res,425,507,104));
  96. ASSERT_TRUE(validate_pixel(res,489,486,105));
  97. ASSERT_TRUE(validate_pixel(res,223,208,64));
  98. ASSERT_TRUE(validate_pixel(res,418,3,78));
  99. ASSERT_TRUE(validate_pixel(res,63,76,97));
  100. ASSERT_TRUE(validate_pixel(res,29,134,126));
  101. ASSERT_TRUE(validate_pixel(res,219,291,174));
  102. ASSERT_TRUE(validate_pixel(res,384,124,76));
  103. #endif
  104. #if 1
  105. ASSERT_TRUE(validate_pixel(res,248,334,194));
  106. ASSERT_TRUE(validate_pixel(res,489,333,171));
  107. ASSERT_TRUE(validate_pixel(res,425,507,103));
  108. ASSERT_TRUE(validate_pixel(res,489,486,109));
  109. ASSERT_TRUE(validate_pixel(res,223,208,72));
  110. ASSERT_TRUE(validate_pixel(res,418,3,58));
  111. ASSERT_TRUE(validate_pixel(res,63,76,93));
  112. ASSERT_TRUE(validate_pixel(res,29,134,127));
  113. ASSERT_TRUE(validate_pixel(res,219,291,180));
  114. ASSERT_TRUE(validate_pixel(res,384,124,80));
  115. #endif
  116. }
  117. }} // namespace