test_sparse_match_interpolator.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "opencv2/ximgproc/sparse_match_interpolator.hpp"
  6. namespace opencv_test { namespace {
  7. static string getDataDir()
  8. {
  9. return cvtest::TS::ptr()->get_data_path();
  10. }
  11. const float FLOW_TAG_FLOAT = 202021.25f;
  12. Mat readOpticalFlow( const String& path )
  13. {
  14. // CV_Assert(sizeof(float) == 4);
  15. //FIXME: ensure right sizes of int and float - here and in writeOpticalFlow()
  16. Mat_<Point2f> flow;
  17. std::ifstream file(path.c_str(), std::ios_base::binary);
  18. if ( !file.good() )
  19. return std::move(flow); // no file - return empty matrix
  20. float tag;
  21. file.read((char*) &tag, sizeof(float));
  22. if ( tag != FLOW_TAG_FLOAT )
  23. return std::move(flow);
  24. int width, height;
  25. file.read((char*) &width, 4);
  26. file.read((char*) &height, 4);
  27. flow.create(height, width);
  28. for ( int i = 0; i < flow.rows; ++i )
  29. {
  30. for ( int j = 0; j < flow.cols; ++j )
  31. {
  32. Point2f u;
  33. file.read((char*) &u.x, sizeof(float));
  34. file.read((char*) &u.y, sizeof(float));
  35. if ( !file.good() )
  36. {
  37. flow.release();
  38. return std::move(flow);
  39. }
  40. flow(i, j) = u;
  41. }
  42. }
  43. file.close();
  44. return std::move(flow);
  45. }
  46. CV_ENUM(GuideTypes, CV_8UC1, CV_8UC3)
  47. typedef tuple<Size, GuideTypes> InterpolatorParams;
  48. typedef TestWithParam<InterpolatorParams> InterpolatorTest;
  49. TEST(InterpolatorTest, ReferenceAccuracy)
  50. {
  51. double MAX_DIF = 1.0;
  52. double MAX_MEAN_DIF = 1.0 / 256.0;
  53. string dir = getDataDir() + "cv/sparse_match_interpolator";
  54. Mat src = imread(getDataDir() + "cv/optflow/RubberWhale1.png",IMREAD_COLOR);
  55. ASSERT_FALSE(src.empty());
  56. Mat ref_flow = readOpticalFlow(dir + "/RubberWhale_reference_result.flo");
  57. ASSERT_FALSE(ref_flow.empty());
  58. std::ifstream file((dir + "/RubberWhale_sparse_matches.txt").c_str());
  59. float from_x,from_y,to_x,to_y;
  60. vector<Point2f> from_points;
  61. vector<Point2f> to_points;
  62. while(file >> from_x >> from_y >> to_x >> to_y)
  63. {
  64. from_points.push_back(Point2f(from_x,from_y));
  65. to_points.push_back(Point2f(to_x,to_y));
  66. }
  67. Mat res_flow;
  68. Ptr<EdgeAwareInterpolator> interpolator = createEdgeAwareInterpolator();
  69. interpolator->setK(128);
  70. interpolator->setSigma(0.05f);
  71. interpolator->setUsePostProcessing(true);
  72. interpolator->setFGSLambda(500.0f);
  73. interpolator->setFGSSigma(1.5f);
  74. interpolator->interpolate(src,from_points,Mat(),to_points,res_flow);
  75. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
  76. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
  77. Mat from_point_mat(from_points);
  78. Mat to_points_mat(to_points);
  79. interpolator->interpolate(src,from_point_mat,Mat(),to_points_mat,res_flow);
  80. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
  81. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
  82. }
  83. TEST(InterpolatorTest, RICReferenceAccuracy)
  84. {
  85. double MAX_DIF = 6.0;
  86. double MAX_MEAN_DIF = 60.0 / 256.0;
  87. string dir = getDataDir() + "cv/sparse_match_interpolator";
  88. Mat src = imread(getDataDir() + "cv/optflow/RubberWhale1.png", IMREAD_COLOR);
  89. ASSERT_FALSE(src.empty());
  90. Mat ref_flow = readOpticalFlow(dir + "/RubberWhale_reference_result.flo");
  91. ASSERT_FALSE(ref_flow.empty());
  92. Mat src1 = imread(getDataDir() + "cv/optflow/RubberWhale2.png", IMREAD_COLOR);
  93. ASSERT_FALSE(src.empty());
  94. std::ifstream file((dir + "/RubberWhale_sparse_matches.txt").c_str());
  95. float from_x, from_y, to_x, to_y;
  96. vector<Point2f> from_points;
  97. vector<Point2f> to_points;
  98. while (file >> from_x >> from_y >> to_x >> to_y)
  99. {
  100. from_points.push_back(Point2f(from_x, from_y));
  101. to_points.push_back(Point2f(to_x, to_y));
  102. }
  103. Mat res_flow;
  104. Ptr<RICInterpolator> interpolator = createRICInterpolator();
  105. interpolator->setK(32);
  106. interpolator->setSuperpixelSize(15);
  107. interpolator->setSuperpixelNNCnt(150);
  108. interpolator->setSuperpixelRuler(15.f);
  109. interpolator->setSuperpixelMode(ximgproc::SLIC);
  110. interpolator->setAlpha(0.7f);
  111. interpolator->setModelIter(4);
  112. interpolator->setRefineModels(true);
  113. interpolator->setMaxFlow(250.f);
  114. interpolator->setUseVariationalRefinement(true);
  115. interpolator->setUseGlobalSmootherFilter(true);
  116. interpolator->setFGSLambda(500.f);
  117. interpolator->setFGSSigma(1.5f);
  118. interpolator->interpolate(src, from_points, src1, to_points, res_flow);
  119. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
  120. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1), MAX_MEAN_DIF*res_flow.total());
  121. Mat from_point_mat(from_points);
  122. Mat to_points_mat(to_points);
  123. interpolator->interpolate(src, from_point_mat, src1, to_points_mat, res_flow);
  124. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_INF), MAX_DIF);
  125. EXPECT_LE(cv::norm(res_flow, ref_flow, NORM_L1) , MAX_MEAN_DIF*res_flow.total());
  126. }
  127. TEST_P(InterpolatorTest, MultiThreadReproducibility)
  128. {
  129. if (cv::getNumberOfCPUs() == 1)
  130. return;
  131. double MAX_DIF = 1.0;
  132. double MAX_MEAN_DIF = 1.0 / 256.0;
  133. int loopsCount = 2;
  134. RNG rng(0);
  135. InterpolatorParams params = GetParam();
  136. Size size = get<0>(params);
  137. int guideType = get<1>(params);
  138. Mat from(size, guideType);
  139. randu(from, 0, 255);
  140. int num_matches = rng.uniform(5,SHRT_MAX-1);
  141. vector<Point2f> from_points;
  142. vector<Point2f> to_points;
  143. for(int i=0;i<num_matches;i++)
  144. {
  145. from_points.push_back(Point2f(rng.uniform(0.01f,(float)size.width-1.01f),rng.uniform(0.01f,(float)size.height-1.01f)));
  146. to_points.push_back(Point2f(rng.uniform(0.01f,(float)size.width-1.01f),rng.uniform(0.01f,(float)size.height-1.01f)));
  147. }
  148. int nThreads = cv::getNumThreads();
  149. if (nThreads == 1)
  150. throw SkipTestException("Single thread environment");
  151. for (int iter = 0; iter <= loopsCount; iter++)
  152. {
  153. int K = rng.uniform(4,512);
  154. float sigma = rng.uniform(0.01f,0.5f);
  155. float FGSlambda = rng.uniform(100.0f, 10000.0f);
  156. float FGSsigma = rng.uniform(0.5f, 100.0f);
  157. Ptr<EdgeAwareInterpolator> interpolator = createEdgeAwareInterpolator();
  158. interpolator->setK(K);
  159. interpolator->setSigma(sigma);
  160. interpolator->setUsePostProcessing(true);
  161. interpolator->setFGSLambda(FGSlambda);
  162. interpolator->setFGSSigma(FGSsigma);
  163. cv::setNumThreads(nThreads);
  164. Mat resMultiThread;
  165. interpolator->interpolate(from,from_points,Mat(),to_points,resMultiThread);
  166. cv::setNumThreads(1);
  167. Mat resSingleThread;
  168. interpolator->interpolate(from,from_points,Mat(),to_points,resSingleThread);
  169. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_INF), MAX_DIF);
  170. EXPECT_LE(cv::norm(resSingleThread, resMultiThread, NORM_L1) , MAX_MEAN_DIF*resMultiThread.total());
  171. }
  172. }
  173. INSTANTIATE_TEST_CASE_P(FullSet,InterpolatorTest, Combine(Values(szODD,szVGA), GuideTypes::all()));
  174. }} // namespace