test_adaptive_manifold.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test {
  6. Ptr<AdaptiveManifoldFilter> createAMFilterRefImpl(double sigma_s, double sigma_r, bool adjust_outliers = false);
  7. namespace {
  8. #ifndef SQR
  9. #define SQR(x) ((x)*(x))
  10. #endif
  11. static string getOpenCVExtraDir()
  12. {
  13. return cvtest::TS::ptr()->get_data_path();
  14. }
  15. static void checkSimilarity(InputArray res, InputArray ref, double maxNormInf = 1, double maxNormL2 = 1.0 / 64)
  16. {
  17. double normInf = cvtest::norm(res, ref, NORM_INF);
  18. double normL2 = cvtest::norm(res, ref, NORM_L2) / res.total();
  19. if (maxNormInf >= 0) { EXPECT_LE(normInf, maxNormInf); }
  20. if (maxNormL2 >= 0) { EXPECT_LE(normL2, maxNormL2); }
  21. }
  22. TEST(AdaptiveManifoldTest, SplatSurfaceAccuracy)
  23. {
  24. RNG rnd(0);
  25. for (int i = 0; i < 5; i++)
  26. {
  27. Size sz(rnd.uniform(512, 1024), rnd.uniform(512, 1024));
  28. int guideCn = rnd.uniform(1, 8);
  29. Mat guide(sz, CV_MAKE_TYPE(CV_32F, guideCn));
  30. randu(guide, 0, 1);
  31. Scalar surfaceValue;
  32. int srcCn = rnd.uniform(1, 4);
  33. rnd.fill(surfaceValue, RNG::UNIFORM, 0, 255);
  34. Mat src(sz, CV_MAKE_TYPE(CV_8U, srcCn), surfaceValue);
  35. double sigma_s = rnd.uniform(1.0, 50.0);
  36. double sigma_r = rnd.uniform(0.1, 0.9);
  37. Mat res;
  38. amFilter(guide, src, res, sigma_s, sigma_r, false);
  39. double normInf = cvtest::norm(src, res, NORM_INF);
  40. EXPECT_EQ(normInf, 0);
  41. }
  42. }
  43. TEST(AdaptiveManifoldTest, AuthorsReferenceAccuracy)
  44. {
  45. String srcImgPath = "cv/edgefilter/kodim23.png";
  46. String refPaths[] =
  47. {
  48. "cv/edgefilter/amf/kodim23_amf_ss5_sr0.3_ref.png",
  49. "cv/edgefilter/amf/kodim23_amf_ss30_sr0.1_ref.png",
  50. "cv/edgefilter/amf/kodim23_amf_ss50_sr0.3_ref.png"
  51. };
  52. pair<double, double> refParams[] =
  53. {
  54. make_pair(5.0, 0.3),
  55. make_pair(30.0, 0.1),
  56. make_pair(50.0, 0.3)
  57. };
  58. String refOutliersPaths[] =
  59. {
  60. "cv/edgefilter/amf/kodim23_amf_ss5_sr0.1_outliers_ref.png",
  61. "cv/edgefilter/amf/kodim23_amf_ss15_sr0.3_outliers_ref.png",
  62. "cv/edgefilter/amf/kodim23_amf_ss50_sr0.5_outliers_ref.png"
  63. };
  64. pair<double, double> refOutliersParams[] =
  65. {
  66. make_pair(5.0, 0.1),
  67. make_pair(15.0, 0.3),
  68. make_pair(50.0, 0.5),
  69. };
  70. Mat srcImg = imread(getOpenCVExtraDir() + srcImgPath);
  71. ASSERT_TRUE(!srcImg.empty());
  72. for (int i = 0; i < 3; i++)
  73. {
  74. Mat refRes = imread(getOpenCVExtraDir() + refPaths[i]);
  75. double sigma_s = refParams[i].first;
  76. double sigma_r = refParams[i].second;
  77. ASSERT_TRUE(!refRes.empty());
  78. Mat res;
  79. Ptr<AdaptiveManifoldFilter> amf = createAMFilter(sigma_s, sigma_r, false);
  80. amf->setUseRNG(false);
  81. amf->filter(srcImg, res, srcImg);
  82. amf->collectGarbage();
  83. checkSimilarity(res, refRes);
  84. }
  85. for (int i = 0; i < 3; i++)
  86. {
  87. Mat refRes = imread(getOpenCVExtraDir() + refOutliersPaths[i]);
  88. double sigma_s = refOutliersParams[i].first;
  89. double sigma_r = refOutliersParams[i].second;
  90. ASSERT_TRUE(!refRes.empty());
  91. Mat res;
  92. Ptr<AdaptiveManifoldFilter> amf = createAMFilter(sigma_s, sigma_r, true);
  93. amf->setUseRNG(false);
  94. amf->filter(srcImg, res, srcImg);
  95. amf->collectGarbage();
  96. checkSimilarity(res, refRes);
  97. }
  98. }
  99. typedef tuple<string, string> AMRefTestParams;
  100. typedef TestWithParam<AMRefTestParams> AdaptiveManifoldRefImplTest;
  101. TEST_P(AdaptiveManifoldRefImplTest, RefImplAccuracy)
  102. {
  103. AMRefTestParams params = GetParam();
  104. string guideFileName = get<0>(params);
  105. string srcFileName = get<1>(params);
  106. Mat guide = imread(getOpenCVExtraDir() + guideFileName);
  107. Mat src = imread(getOpenCVExtraDir() + srcFileName);
  108. ASSERT_TRUE(!guide.empty() && !src.empty());
  109. int seed = 10 * (int)guideFileName.length() + (int)srcFileName.length();
  110. RNG rnd(seed);
  111. //inconsistent downsample/upsample operations in reference implementation
  112. Size dstSize((guide.cols + 15) & ~15, (guide.rows + 15) & ~15);
  113. resize(guide, guide, dstSize, 0, 0, INTER_LINEAR_EXACT);
  114. resize(src, src, dstSize, 0, 0, INTER_LINEAR_EXACT);
  115. int nThreads = cv::getNumThreads();
  116. if (nThreads == 1)
  117. throw SkipTestException("Single thread environment");
  118. for (int iter = 0; iter < 4; iter++)
  119. {
  120. double sigma_s = rnd.uniform(1.0, 50.0);
  121. double sigma_r = rnd.uniform(0.1, 0.9);
  122. bool adjust_outliers = (iter % 2 == 0);
  123. cv::setNumThreads(nThreads);
  124. Mat res;
  125. amFilter(guide, src, res, sigma_s, sigma_r, adjust_outliers);
  126. cv::setNumThreads(1);
  127. Mat resRef;
  128. Ptr<AdaptiveManifoldFilter> amf = createAMFilterRefImpl(sigma_s, sigma_r, adjust_outliers);
  129. amf->filter(src, resRef, guide);
  130. //results of reference implementation may differ on small sigma_s into small isolated region
  131. //due to low single-precision floating point numbers accuracy
  132. //therefore the threshold of inf norm was increased
  133. checkSimilarity(res, resRef, 25);
  134. }
  135. }
  136. INSTANTIATE_TEST_CASE_P(TypicalSet, AdaptiveManifoldRefImplTest,
  137. Combine(
  138. Values("cv/edgefilter/kodim23.png", "cv/npr/test4.png"),
  139. Values("cv/edgefilter/kodim23.png", "cv/npr/test4.png")
  140. ));
  141. }} // namespace