test_denoising.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. namespace opencv_test { namespace {
  44. //#define DUMP_RESULTS
  45. #ifdef DUMP_RESULTS
  46. # define DUMP(image, path) imwrite(path, image)
  47. #else
  48. # define DUMP(image, path)
  49. #endif
  50. TEST(Photo_DenoisingGrayscale, regression)
  51. {
  52. string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
  53. string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  54. string expected_path = folder + "lena_noised_denoised_grayscale_tw=7_sw=21_h=10.png";
  55. Mat original = imread(original_path, IMREAD_GRAYSCALE);
  56. Mat expected = imread(expected_path, IMREAD_GRAYSCALE);
  57. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  58. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  59. Mat result;
  60. fastNlMeansDenoising(original, result, 10);
  61. DUMP(result, expected_path + ".res.png");
  62. ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
  63. }
  64. TEST(Photo_DenoisingColored, regression)
  65. {
  66. string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
  67. string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  68. string expected_path = folder + "lena_noised_denoised_lab12_tw=7_sw=21_h=10_h2=10.png";
  69. Mat original = imread(original_path, IMREAD_COLOR);
  70. Mat expected = imread(expected_path, IMREAD_COLOR);
  71. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  72. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  73. Mat result;
  74. fastNlMeansDenoisingColored(original, result, 10, 10);
  75. DUMP(result, expected_path + ".res.png");
  76. ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
  77. }
  78. TEST(Photo_DenoisingGrayscaleMulti, regression)
  79. {
  80. const int imgs_count = 3;
  81. string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
  82. string expected_path = folder + "lena_noised_denoised_multi_tw=7_sw=21_h=15.png";
  83. Mat expected = imread(expected_path, IMREAD_GRAYSCALE);
  84. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  85. vector<Mat> original(imgs_count);
  86. for (int i = 0; i < imgs_count; i++)
  87. {
  88. string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);
  89. original[i] = imread(original_path, IMREAD_GRAYSCALE);
  90. ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;
  91. }
  92. Mat result;
  93. fastNlMeansDenoisingMulti(original, result, imgs_count / 2, imgs_count, 15);
  94. DUMP(result, expected_path + ".res.png");
  95. ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
  96. }
  97. TEST(Photo_DenoisingColoredMulti, regression)
  98. {
  99. const int imgs_count = 3;
  100. string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
  101. string expected_path = folder + "lena_noised_denoised_multi_lab12_tw=7_sw=21_h=10_h2=15.png";
  102. Mat expected = imread(expected_path, IMREAD_COLOR);
  103. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  104. vector<Mat> original(imgs_count);
  105. for (int i = 0; i < imgs_count; i++)
  106. {
  107. string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);
  108. original[i] = imread(original_path, IMREAD_COLOR);
  109. ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;
  110. }
  111. Mat result;
  112. fastNlMeansDenoisingColoredMulti(original, result, imgs_count / 2, imgs_count, 10, 15);
  113. DUMP(result, expected_path + ".res.png");
  114. ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
  115. }
  116. TEST(Photo_White, issue_2646)
  117. {
  118. cv::Mat img(50, 50, CV_8UC1, cv::Scalar::all(255));
  119. cv::Mat filtered;
  120. cv::fastNlMeansDenoising(img, filtered);
  121. int nonWhitePixelsCount = (int)img.total() - cv::countNonZero(filtered == img);
  122. ASSERT_EQ(0, nonWhitePixelsCount);
  123. }
  124. TEST(Photo_Denoising, speed)
  125. {
  126. string imgname = string(cvtest::TS::ptr()->get_data_path()) + "shared/5MP.png";
  127. Mat src = imread(imgname, 0), dst;
  128. double t = (double)getTickCount();
  129. fastNlMeansDenoising(src, dst, 5, 7, 21);
  130. t = (double)getTickCount() - t;
  131. printf("execution time: %gms\n", t*1000./getTickFrequency());
  132. }
  133. }} // namespace