test_denoise_bm3d.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. //#define DUMP_RESULTS
  44. //#define TEST_TRANSFORMS
  45. #ifdef TEST_TRANSFORMS
  46. #include "..\..\xphoto\src\bm3d_denoising_invoker_commons.hpp"
  47. #include "..\..\xphoto\src\bm3d_denoising_transforms.hpp"
  48. #include "..\..\xphoto\src\kaiser_window.hpp"
  49. using namespace cv::xphoto;
  50. #endif
  51. #ifdef DUMP_RESULTS
  52. # define DUMP(image, path) imwrite(path, image)
  53. #else
  54. # define DUMP(image, path)
  55. #endif
  56. #ifdef OPENCV_ENABLE_NONFREE
  57. namespace opencv_test { namespace {
  58. TEST(xphoto_DenoisingBm3dGrayscale, regression_L2)
  59. {
  60. std::string folder = std::string(cvtest::TS::ptr()->get_data_path()) + "cv/xphoto/bm3d_image_denoising/";
  61. std::string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  62. std::string expected_path = folder + "lena_noised_denoised_bm3d_wiener_grayscale_l2_tw=4_sw=16_h=10_bm=400.png";
  63. cv::Mat original = cv::imread(original_path, cv::IMREAD_GRAYSCALE);
  64. cv::Mat expected = cv::imread(expected_path, cv::IMREAD_GRAYSCALE);
  65. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  66. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  67. // BM3D: two different calls doing exactly the same thing
  68. cv::Mat result, resultSec;
  69. cv::xphoto::bm3dDenoising(original, noArray(), resultSec, 10, 4, 16, 2500, 400, 8, 1, 0.0f, cv::NORM_L2, cv::xphoto::BM3D_STEPALL);
  70. cv::xphoto::bm3dDenoising(original, result, 10, 4, 16, 2500, 400, 8, 1, 0.0f, cv::NORM_L2, cv::xphoto::BM3D_STEPALL);
  71. DUMP(result, expected_path + ".res.png");
  72. ASSERT_EQ(cvtest::norm(result, resultSec, cv::NORM_L2), 0);
  73. ASSERT_LT(cvtest::norm(result, expected, cv::NORM_L2), 200);
  74. }
  75. TEST(xphoto_DenoisingBm3dGrayscale, regression_L2_separate)
  76. {
  77. std::string folder = std::string(cvtest::TS::ptr()->get_data_path()) + "cv/xphoto/bm3d_image_denoising/";
  78. std::string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  79. std::string expected_basic_path = folder + "lena_noised_denoised_bm3d_grayscale_l2_tw=4_sw=16_h=10_bm=2500.png";
  80. std::string expected_path = folder + "lena_noised_denoised_bm3d_wiener_grayscale_l2_tw=4_sw=16_h=10_bm=400.png";
  81. cv::Mat original = cv::imread(original_path, cv::IMREAD_GRAYSCALE);
  82. cv::Mat expected_basic = cv::imread(expected_basic_path, cv::IMREAD_GRAYSCALE);
  83. cv::Mat expected = cv::imread(expected_path, cv::IMREAD_GRAYSCALE);
  84. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  85. ASSERT_FALSE(expected_basic.empty()) << "Could not load reference image " << expected_basic_path;
  86. ASSERT_FALSE(expected.empty()) << "Could not load input image " << expected_path;
  87. cv::Mat basic, result;
  88. // BM3D step 1
  89. cv::xphoto::bm3dDenoising(original, basic, 10, 4, 16, 2500, -1, 8, 1, 0.0f, cv::NORM_L2, cv::xphoto::BM3D_STEP1);
  90. ASSERT_LT(cvtest::norm(basic, expected_basic, cv::NORM_L2), 200);
  91. DUMP(basic, expected_basic_path + ".res.basic.png");
  92. // BM3D step 2
  93. cv::xphoto::bm3dDenoising(original, basic, result, 10, 4, 16, 2500, 400, 8, 1, 0.0f, cv::NORM_L2, cv::xphoto::BM3D_STEP2);
  94. ASSERT_LT(cvtest::norm(basic, expected_basic, cv::NORM_L2), 200);
  95. DUMP(basic, expected_basic_path + ".res.basic2.png");
  96. DUMP(result, expected_path + ".res.png");
  97. ASSERT_LT(cvtest::norm(result, expected, cv::NORM_L2), 200);
  98. }
  99. TEST(xphoto_DenoisingBm3dGrayscale, regression_L1)
  100. {
  101. std::string folder = std::string(cvtest::TS::ptr()->get_data_path()) + "cv/xphoto/bm3d_image_denoising/";
  102. std::string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  103. std::string expected_path = folder + "lena_noised_denoised_bm3d_grayscale_l1_tw=4_sw=16_h=10_bm=2500.png";
  104. cv::Mat original = cv::imread(original_path, cv::IMREAD_GRAYSCALE);
  105. cv::Mat expected = cv::imread(expected_path, cv::IMREAD_GRAYSCALE);
  106. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  107. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  108. cv::Mat result;
  109. cv::xphoto::bm3dDenoising(original, result, 10, 4, 16, 2500, -1, 8, 1, 0.0f, cv::NORM_L1, cv::xphoto::BM3D_STEP1);
  110. DUMP(result, expected_path + ".res.png");
  111. ASSERT_LT(cvtest::norm(result, expected, cv::NORM_L2), 200);
  112. }
  113. TEST(xphoto_DenoisingBm3dGrayscale, regression_L2_8x8)
  114. {
  115. std::string folder = std::string(cvtest::TS::ptr()->get_data_path()) + "cv/xphoto/bm3d_image_denoising/";
  116. std::string original_path = folder + "lena_noised_gaussian_sigma=10.png";
  117. std::string expected_path = folder + "lena_noised_denoised_bm3d_grayscale_l2_tw=8_sw=16_h=10_bm=2500.png";
  118. cv::Mat original = cv::imread(original_path, cv::IMREAD_GRAYSCALE);
  119. cv::Mat expected = cv::imread(expected_path, cv::IMREAD_GRAYSCALE);
  120. ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
  121. ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
  122. cv::Mat result;
  123. cv::xphoto::bm3dDenoising(original, result, 10, 8, 16, 2500, -1, 8, 1, 0.0f, cv::NORM_L2, cv::xphoto::BM3D_STEP1);
  124. DUMP(result, expected_path + ".res.png");
  125. ASSERT_LT(cvtest::norm(result, expected, cv::NORM_L2), 200);
  126. }
  127. #ifdef TEST_TRANSFORMS
  128. TEST(xphoto_DenoisingBm3dKaiserWindow, regression_4)
  129. {
  130. float beta = 2.0f;
  131. int N = 4;
  132. cv::Mat kaiserWindow;
  133. calcKaiserWindow1D(kaiserWindow, N, beta);
  134. float kaiser4[] = {
  135. 0.43869004f,
  136. 0.92432547f,
  137. 0.92432547f,
  138. 0.43869004f
  139. };
  140. for (int i = 0; i < N; ++i)
  141. ASSERT_FLOAT_EQ(kaiser4[i], kaiserWindow.at<float>(i));
  142. }
  143. TEST(xphoto_DenoisingBm3dKaiserWindow, regression_8)
  144. {
  145. float beta = 2.0f;
  146. int N = 8;
  147. cv::Mat kaiserWindow;
  148. calcKaiserWindow1D(kaiserWindow, N, beta);
  149. float kaiser8[] = {
  150. 0.43869004f,
  151. 0.68134475f,
  152. 0.87685609f,
  153. 0.98582518f,
  154. 0.98582518f,
  155. 0.87685609f,
  156. 0.68134463f,
  157. 0.43869004f
  158. };
  159. for (int i = 0; i < N; ++i)
  160. ASSERT_FLOAT_EQ(kaiser8[i], kaiserWindow.at<float>(i));
  161. }
  162. TEST(xphoto_DenoisingBm3dTransforms, regression_2D_generic)
  163. {
  164. const int templateWindowSize = 8;
  165. const int templateWindowSizeSq = templateWindowSize * templateWindowSize;
  166. uchar src[templateWindowSizeSq];
  167. short dst[templateWindowSizeSq];
  168. short dstSec[templateWindowSizeSq];
  169. // Initialize array
  170. for (uchar i = 0; i < templateWindowSizeSq; ++i)
  171. src[i] = (i % 10) * 10;
  172. // Use tailored transforms
  173. HaarTransform<uchar, short>::RegisterTransforms2D(templateWindowSize);
  174. HaarTransform<uchar, short>::forwardTransform2D(src, dst, templateWindowSize, templateWindowSize);
  175. HaarTransform<uchar, short>::inverseTransform2D(dst, templateWindowSize);
  176. // Use generic transforms
  177. HaarTransform2D::ForwardTransformXxX<uchar, short, templateWindowSize>(src, dstSec, templateWindowSize, templateWindowSize);
  178. HaarTransform2D::InverseTransformXxX<short, templateWindowSize>(dstSec, templateWindowSize);
  179. for (unsigned i = 0; i < templateWindowSizeSq; ++i)
  180. ASSERT_EQ(dst[i], dstSec[i]);
  181. }
  182. TEST(xphoto_DenoisingBm3dTransforms, regression_2D_4x4)
  183. {
  184. const int templateWindowSize = 4;
  185. const int templateWindowSizeSq = templateWindowSize * templateWindowSize;
  186. uchar src[templateWindowSizeSq];
  187. short dst[templateWindowSizeSq];
  188. // Initialize array
  189. for (uchar i = 0; i < templateWindowSizeSq; ++i)
  190. {
  191. src[i] = i;
  192. }
  193. HaarTransform2D::ForwardTransform4x4(src, dst, templateWindowSize, templateWindowSize);
  194. HaarTransform2D::InverseTransform4x4(dst, templateWindowSize);
  195. for (uchar i = 0; i < templateWindowSizeSq; ++i)
  196. ASSERT_EQ(static_cast<short>(src[i]), dst[i]);
  197. }
  198. TEST(xphoto_DenoisingBm3dTransforms, regression_2D_8x8)
  199. {
  200. const int templateWindowSize = 8;
  201. const int templateWindowSizeSq = templateWindowSize * templateWindowSize;
  202. uchar src[templateWindowSizeSq];
  203. short dst[templateWindowSizeSq];
  204. // Initialize array
  205. for (uchar i = 0; i < templateWindowSizeSq; ++i)
  206. {
  207. src[i] = i;
  208. }
  209. HaarTransform2D::ForwardTransform8x8(src, dst, templateWindowSize, templateWindowSize);
  210. HaarTransform2D::InverseTransform8x8(dst, templateWindowSize);
  211. for (uchar i = 0; i < templateWindowSizeSq; ++i)
  212. ASSERT_EQ(static_cast<short>(src[i]), dst[i]);
  213. }
  214. template <typename T, typename DT, typename CT>
  215. static void Test1dTransform(
  216. T *thrMap,
  217. int groupSize,
  218. int templateWindowSizeSq,
  219. BlockMatch<T, DT, CT> *bm,
  220. BlockMatch<T, DT, CT> *bmOrig,
  221. int expectedNonZeroCount = -1)
  222. {
  223. if (expectedNonZeroCount < 0)
  224. expectedNonZeroCount = groupSize * templateWindowSizeSq;
  225. // Test group size
  226. short sumNonZero = 0;
  227. T *thrMapPtr1D = thrMap + (groupSize - 1) * templateWindowSizeSq;
  228. for (int n = 0; n < templateWindowSizeSq; n++)
  229. {
  230. switch (groupSize)
  231. {
  232. case 16:
  233. HaarTransform1D::ForwardTransform16(bm, n);
  234. sumNonZero += HardThreshold<16>(bm, n, thrMapPtr1D);
  235. HaarTransform1D::InverseTransform16(bm, n);
  236. break;
  237. case 8:
  238. HaarTransform1D::ForwardTransform8(bm, n);
  239. sumNonZero += HardThreshold<8>(bm, n, thrMapPtr1D);
  240. HaarTransform1D::InverseTransform8(bm, n);
  241. break;
  242. case 4:
  243. HaarTransform1D::ForwardTransform4(bm, n);
  244. sumNonZero += HardThreshold<4>(bm, n, thrMapPtr1D);
  245. HaarTransform1D::InverseTransform4(bm, n);
  246. break;
  247. case 2:
  248. HaarTransform1D::ForwardTransform2(bm, n);
  249. sumNonZero += HardThreshold<2>(bm, n, thrMapPtr1D);
  250. HaarTransform1D::InverseTransform2(bm, n);
  251. break;
  252. default:
  253. HaarTransform1D::ForwardTransformN(bm, n, groupSize);
  254. sumNonZero += HardThreshold(bm, n, thrMapPtr1D, groupSize);
  255. HaarTransform1D::InverseTransformN(bm, n, groupSize);
  256. }
  257. }
  258. // Assert transform
  259. if (expectedNonZeroCount == groupSize * templateWindowSizeSq)
  260. {
  261. for (int i = 0; i < groupSize; ++i)
  262. for (int j = 0; j < templateWindowSizeSq; ++j)
  263. ASSERT_EQ(bm[i][j], bmOrig[i][j]);
  264. }
  265. // Assert shrinkage
  266. ASSERT_EQ(sumNonZero, expectedNonZeroCount);
  267. }
  268. TEST(xphoto_DenoisingBm3dTransforms, regression_1D_transform)
  269. {
  270. const int templateWindowSize = 4;
  271. const int templateWindowSizeSq = templateWindowSize * templateWindowSize;
  272. const int searchWindowSize = 16;
  273. const int searchWindowSizeSq = searchWindowSize * searchWindowSize;
  274. const float h = 10;
  275. int maxGroupSize = 64;
  276. // Precompute separate maps for transform and shrinkage verification
  277. short *thrMapTransform = NULL;
  278. short *thrMapShrinkage = NULL;
  279. HaarTransform<short, short>::calcThresholdMap3D(thrMapTransform, 0, templateWindowSize, maxGroupSize);
  280. HaarTransform<short, short>::calcThresholdMap3D(thrMapShrinkage, h, templateWindowSize, maxGroupSize);
  281. // Generate some data
  282. BlockMatch<short, int, short> *bm = new BlockMatch<short, int, short>[maxGroupSize];
  283. BlockMatch<short, int, short> *bmOrig = new BlockMatch<short, int, short>[maxGroupSize];
  284. for (int i = 0; i < maxGroupSize; ++i)
  285. {
  286. bm[i].init(templateWindowSizeSq);
  287. bmOrig[i].init(templateWindowSizeSq);
  288. }
  289. for (short i = 0; i < maxGroupSize; ++i)
  290. {
  291. for (short j = 0; j < templateWindowSizeSq; ++j)
  292. {
  293. bm[i][j] = (j + 1);
  294. bmOrig[i][j] = bm[i][j];
  295. }
  296. }
  297. // Verify transforms
  298. Test1dTransform<short, int, short>(thrMapTransform, 2, templateWindowSizeSq, bm, bmOrig);
  299. Test1dTransform<short, int, short>(thrMapTransform, 4, templateWindowSizeSq, bm, bmOrig);
  300. Test1dTransform<short, int, short>(thrMapTransform, 8, templateWindowSizeSq, bm, bmOrig);
  301. Test1dTransform<short, int, short>(thrMapTransform, 16, templateWindowSizeSq, bm, bmOrig);
  302. Test1dTransform<short, int, short>(thrMapTransform, 32, templateWindowSizeSq, bm, bmOrig);
  303. Test1dTransform<short, int, short>(thrMapTransform, 64, templateWindowSizeSq, bm, bmOrig);
  304. // Verify shrinkage
  305. Test1dTransform<short, int, short>(thrMapShrinkage, 2, templateWindowSizeSq, bm, bmOrig, 6);
  306. Test1dTransform<short, int, short>(thrMapShrinkage, 4, templateWindowSizeSq, bm, bmOrig, 6);
  307. Test1dTransform<short, int, short>(thrMapShrinkage, 8, templateWindowSizeSq, bm, bmOrig, 6);
  308. Test1dTransform<short, int, short>(thrMapShrinkage, 16, templateWindowSizeSq, bm, bmOrig, 6);
  309. Test1dTransform<short, int, short>(thrMapShrinkage, 32, templateWindowSizeSq, bm, bmOrig, 6);
  310. Test1dTransform<short, int, short>(thrMapShrinkage, 64, templateWindowSizeSq, bm, bmOrig, 14);
  311. }
  312. const float sqrt2 = std::sqrt(2.0f);
  313. TEST(xphoto_DenoisingBm3dTransforms, regression_1D_generate)
  314. {
  315. const int numberOfElements = 8;
  316. const int arrSize = (numberOfElements << 1) - 1;
  317. float *thrMap1D = NULL;
  318. HaarTransform<short, short>::calcThresholdMap1D(thrMap1D, numberOfElements);
  319. // Expected array
  320. const float kThrMap1D[arrSize] = {
  321. 1.0f, // 1 element
  322. sqrt2 / 2.0f, sqrt2, // 2 elements
  323. 0.5f, 1.0f, sqrt2, sqrt2, // 4 elements
  324. sqrt2 / 4.0f, sqrt2 / 2.0f, 1.0f, 1.0f, sqrt2, sqrt2, sqrt2, sqrt2 // 8 elements
  325. };
  326. for (int j = 0; j < arrSize; ++j)
  327. ASSERT_EQ(thrMap1D[j], kThrMap1D[j]);
  328. delete[] thrMap1D;
  329. }
  330. TEST(xphoto_DenoisingBm3dTransforms, regression_2D_generate_4x4)
  331. {
  332. const int templateWindowSize = 4;
  333. float *thrMap2D = NULL;
  334. HaarTransform<short, short>::calcThresholdMap2D(thrMap2D, templateWindowSize);
  335. // Expected array
  336. const float kThrMap4x4[templateWindowSize * templateWindowSize] = {
  337. 0.25f, 0.5f, sqrt2 / 2.0f, sqrt2 / 2.0f,
  338. 0.5f, 1.0f, sqrt2, sqrt2,
  339. sqrt2 / 2.0f, sqrt2, 2.0f, 2.0f,
  340. sqrt2 / 2.0f, sqrt2, 2.0f, 2.0f
  341. };
  342. for (int j = 0; j < templateWindowSize * templateWindowSize; ++j)
  343. ASSERT_EQ(thrMap2D[j], kThrMap4x4[j]);
  344. delete[] thrMap2D;
  345. }
  346. TEST(xphoto_DenoisingBm3dTransforms, regression_2D_generate_8x8)
  347. {
  348. const int templateWindowSize = 8;
  349. float *thrMap2D = NULL;
  350. HaarTransform<short, short>::calcThresholdMap2D(thrMap2D, templateWindowSize);
  351. // Expected array
  352. const float kThrMap8x8[templateWindowSize * templateWindowSize] = {
  353. 0.125f, 0.25f, sqrt2 / 4.0f, sqrt2 / 4.0f, 0.5f, 0.5f, 0.5f, 0.5f,
  354. 0.25f, 0.5f, sqrt2 / 2.0f, sqrt2 / 2.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  355. sqrt2 / 4.0f, sqrt2 / 2.0f, 1.0f, 1.0f, sqrt2, sqrt2, sqrt2, sqrt2,
  356. sqrt2 / 4.0f, sqrt2 / 2.0f, 1.0f, 1.0f, sqrt2, sqrt2, sqrt2, sqrt2,
  357. 0.5f, 1.0f, sqrt2, sqrt2, 2.0f, 2.0f, 2.0f, 2.0f,
  358. 0.5f, 1.0f, sqrt2, sqrt2, 2.0f, 2.0f, 2.0f, 2.0f,
  359. 0.5f, 1.0f, sqrt2, sqrt2, 2.0f, 2.0f, 2.0f, 2.0f,
  360. 0.5f, 1.0f, sqrt2, sqrt2, 2.0f, 2.0f, 2.0f, 2.0f
  361. };
  362. for (int j = 0; j < templateWindowSize * templateWindowSize; ++j)
  363. ASSERT_EQ(thrMap2D[j], kThrMap8x8[j]);
  364. delete[] thrMap2D;
  365. }
  366. TEST(xphoto_Bm3dDenoising, powerOf2)
  367. {
  368. ASSERT_EQ(8, getLargestPowerOf2SmallerThan(9));
  369. ASSERT_EQ(16, getLargestPowerOf2SmallerThan(21));
  370. ASSERT_EQ(4, getLargestPowerOf2SmallerThan(7));
  371. ASSERT_EQ(8, getLargestPowerOf2SmallerThan(8));
  372. ASSERT_EQ(4, getLargestPowerOf2SmallerThan(5));
  373. ASSERT_EQ(4, getLargestPowerOf2SmallerThan(4));
  374. ASSERT_EQ(2, getLargestPowerOf2SmallerThan(3));
  375. ASSERT_EQ(1, getLargestPowerOf2SmallerThan(1));
  376. ASSERT_EQ(0, getLargestPowerOf2SmallerThan(0));
  377. }
  378. #endif // TEST_TRANSFORMS
  379. }} // namespace
  380. #endif // OPENCV_ENABLE_NONFREE