perf_stich.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "perf_precomp.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/opencv_modules.hpp"
  4. #include "opencv2/core/ocl.hpp"
  5. namespace opencv_test
  6. {
  7. using namespace perf;
  8. #define SURF_MATCH_CONFIDENCE 0.65f
  9. #define ORB_MATCH_CONFIDENCE 0.3f
  10. #define WORK_MEGAPIX 0.6
  11. typedef TestBaseWithParam<string> stitch;
  12. typedef TestBaseWithParam<int> stitchExposureCompensation;
  13. typedef TestBaseWithParam<tuple<string, string> > stitchDatasets;
  14. typedef TestBaseWithParam<tuple<string, int>> stitchExposureCompMultiFeed;
  15. #if defined(HAVE_OPENCV_XFEATURES2D) && defined(OPENCV_ENABLE_NONFREE)
  16. #define TEST_DETECTORS testing::Values("surf", "orb", "akaze")
  17. #else
  18. #define TEST_DETECTORS testing::Values("orb", "akaze")
  19. #endif
  20. #define TEST_EXP_COMP_BS testing::Values(32, 16, 12, 10, 8)
  21. #define TEST_EXP_COMP_NR_FEED testing::Values(1, 2, 3, 4, 5)
  22. #define TEST_EXP_COMP_MODE testing::Values("gain", "channels", "blocks_gain", "blocks_channels")
  23. #define AFFINE_DATASETS testing::Values("s", "budapest", "newspaper", "prague")
  24. PERF_TEST_P(stitch, a123, TEST_DETECTORS)
  25. {
  26. Mat pano;
  27. vector<Mat> imgs;
  28. imgs.push_back( imread( getDataPath("stitching/a1.png") ) );
  29. imgs.push_back( imread( getDataPath("stitching/a2.png") ) );
  30. imgs.push_back( imread( getDataPath("stitching/a3.png") ) );
  31. Ptr<Feature2D> featuresFinder = getFeatureFinder(GetParam());
  32. Ptr<detail::FeaturesMatcher> featuresMatcher = GetParam() == "orb"
  33. ? makePtr<detail::BestOf2NearestMatcher>(false, ORB_MATCH_CONFIDENCE)
  34. : makePtr<detail::BestOf2NearestMatcher>(false, SURF_MATCH_CONFIDENCE);
  35. declare.time(30 * 20).iterations(20);
  36. while(next())
  37. {
  38. Ptr<Stitcher> stitcher = Stitcher::create();
  39. stitcher->setFeaturesFinder(featuresFinder);
  40. stitcher->setFeaturesMatcher(featuresMatcher);
  41. stitcher->setWarper(makePtr<SphericalWarper>());
  42. stitcher->setRegistrationResol(WORK_MEGAPIX);
  43. startTimer();
  44. stitcher->stitch(imgs, pano);
  45. stopTimer();
  46. }
  47. EXPECT_NEAR(pano.size().width, 1182, 50);
  48. EXPECT_NEAR(pano.size().height, 682, 30);
  49. SANITY_CHECK_NOTHING();
  50. }
  51. PERF_TEST_P(stitchExposureCompensation, a123, TEST_EXP_COMP_BS)
  52. {
  53. Mat pano;
  54. vector<Mat> imgs;
  55. imgs.push_back( imread( getDataPath("stitching/a1.png") ) );
  56. imgs.push_back( imread( getDataPath("stitching/a2.png") ) );
  57. imgs.push_back( imread( getDataPath("stitching/a3.png") ) );
  58. int bs = GetParam();
  59. declare.time(30 * 10).iterations(10);
  60. while(next())
  61. {
  62. Ptr<Stitcher> stitcher = Stitcher::create();
  63. stitcher->setWarper(makePtr<SphericalWarper>());
  64. stitcher->setRegistrationResol(WORK_MEGAPIX);
  65. stitcher->setExposureCompensator(
  66. makePtr<detail::BlocksGainCompensator>(bs, bs));
  67. startTimer();
  68. stitcher->stitch(imgs, pano);
  69. stopTimer();
  70. }
  71. EXPECT_NEAR(pano.size().width, 1182, 50);
  72. EXPECT_NEAR(pano.size().height, 682, 30);
  73. SANITY_CHECK_NOTHING();
  74. }
  75. PERF_TEST_P(stitchExposureCompMultiFeed, a123, testing::Combine(TEST_EXP_COMP_MODE, TEST_EXP_COMP_NR_FEED))
  76. {
  77. const int block_size = 32;
  78. Mat pano;
  79. vector<Mat> imgs;
  80. imgs.push_back( imread( getDataPath("stitching/a1.png") ) );
  81. imgs.push_back( imread( getDataPath("stitching/a2.png") ) );
  82. imgs.push_back( imread( getDataPath("stitching/a3.png") ) );
  83. string mode = get<0>(GetParam());
  84. int nr_feeds = get<1>(GetParam());
  85. declare.time(30 * 10).iterations(10);
  86. Ptr<detail::ExposureCompensator> exp_comp;
  87. if (mode == "gain")
  88. exp_comp = makePtr<detail::GainCompensator>(nr_feeds);
  89. else if (mode == "channels")
  90. exp_comp = makePtr<detail::ChannelsCompensator>(nr_feeds);
  91. else if (mode == "blocks_gain")
  92. exp_comp = makePtr<detail::BlocksGainCompensator>(block_size, block_size, nr_feeds);
  93. else if (mode == "blocks_channels")
  94. exp_comp = makePtr<detail::BlocksChannelsCompensator>(block_size, block_size, nr_feeds);
  95. while(next())
  96. {
  97. Ptr<Stitcher> stitcher = Stitcher::create();
  98. stitcher->setWarper(makePtr<SphericalWarper>());
  99. stitcher->setRegistrationResol(WORK_MEGAPIX);
  100. stitcher->setExposureCompensator(exp_comp);
  101. startTimer();
  102. stitcher->stitch(imgs, pano);
  103. stopTimer();
  104. }
  105. EXPECT_NEAR(pano.size().width, 1182, 50);
  106. EXPECT_NEAR(pano.size().height, 682, 30);
  107. SANITY_CHECK_NOTHING();
  108. }
  109. PERF_TEST_P(stitch, b12, TEST_DETECTORS)
  110. {
  111. Mat pano;
  112. vector<Mat> imgs;
  113. imgs.push_back( imread( getDataPath("stitching/b1.png") ) );
  114. imgs.push_back( imread( getDataPath("stitching/b2.png") ) );
  115. Ptr<Feature2D> featuresFinder = getFeatureFinder(GetParam());
  116. Ptr<detail::FeaturesMatcher> featuresMatcher = GetParam() == "orb"
  117. ? makePtr<detail::BestOf2NearestMatcher>(false, ORB_MATCH_CONFIDENCE)
  118. : makePtr<detail::BestOf2NearestMatcher>(false, SURF_MATCH_CONFIDENCE);
  119. declare.time(30 * 20).iterations(20);
  120. while(next())
  121. {
  122. Ptr<Stitcher> stitcher = Stitcher::create();
  123. stitcher->setFeaturesFinder(featuresFinder);
  124. stitcher->setFeaturesMatcher(featuresMatcher);
  125. stitcher->setWarper(makePtr<SphericalWarper>());
  126. stitcher->setRegistrationResol(WORK_MEGAPIX);
  127. startTimer();
  128. stitcher->stitch(imgs, pano);
  129. stopTimer();
  130. }
  131. EXPECT_NEAR(pano.size().width, 1117, GetParam() == "surf" ? 100 : 50);
  132. EXPECT_NEAR(pano.size().height, 642, GetParam() == "surf" ? 60 : 30);
  133. SANITY_CHECK_NOTHING();
  134. }
  135. PERF_TEST_P(stitchDatasets, affine, testing::Combine(AFFINE_DATASETS, TEST_DETECTORS))
  136. {
  137. string dataset = get<0>(GetParam());
  138. string detector = get<1>(GetParam());
  139. Mat pano;
  140. vector<Mat> imgs;
  141. int width, height, allowed_diff = 20;
  142. Ptr<Feature2D> featuresFinder = getFeatureFinder(detector);
  143. if(dataset == "budapest")
  144. {
  145. imgs.push_back(imread(getDataPath("stitching/budapest1.jpg")));
  146. imgs.push_back(imread(getDataPath("stitching/budapest2.jpg")));
  147. imgs.push_back(imread(getDataPath("stitching/budapest3.jpg")));
  148. imgs.push_back(imread(getDataPath("stitching/budapest4.jpg")));
  149. imgs.push_back(imread(getDataPath("stitching/budapest5.jpg")));
  150. imgs.push_back(imread(getDataPath("stitching/budapest6.jpg")));
  151. width = 2313;
  152. height = 1158;
  153. // this dataset is big, the results between surf and orb differ slightly,
  154. // but both are still good
  155. allowed_diff = 50;
  156. // we need to boost ORB number of features to be able to stitch this dataset
  157. // SURF works just fine with default settings
  158. if(detector == "orb")
  159. featuresFinder = ORB::create(1500);
  160. }
  161. else if (dataset == "newspaper")
  162. {
  163. imgs.push_back(imread(getDataPath("stitching/newspaper1.jpg")));
  164. imgs.push_back(imread(getDataPath("stitching/newspaper2.jpg")));
  165. imgs.push_back(imread(getDataPath("stitching/newspaper3.jpg")));
  166. imgs.push_back(imread(getDataPath("stitching/newspaper4.jpg")));
  167. width = 1791;
  168. height = 1136;
  169. // we need to boost ORB number of features to be able to stitch this dataset
  170. // SURF works just fine with default settings
  171. if(detector == "orb")
  172. featuresFinder = ORB::create(3000);
  173. }
  174. else if (dataset == "prague")
  175. {
  176. imgs.push_back(imread(getDataPath("stitching/prague1.jpg")));
  177. imgs.push_back(imread(getDataPath("stitching/prague2.jpg")));
  178. width = 983;
  179. height = 1759;
  180. }
  181. else // dataset == "s"
  182. {
  183. imgs.push_back(imread(getDataPath("stitching/s1.jpg")));
  184. imgs.push_back(imread(getDataPath("stitching/s2.jpg")));
  185. width = 1815;
  186. height = 700;
  187. }
  188. declare.time(30 * 20).iterations(20);
  189. while(next())
  190. {
  191. Ptr<Stitcher> stitcher = Stitcher::create(Stitcher::SCANS);
  192. stitcher->setFeaturesFinder(featuresFinder);
  193. if (cv::ocl::useOpenCL())
  194. cv::theRNG() = cv::RNG(12345); // prevent fails of Windows OpenCL builds (see #8294)
  195. startTimer();
  196. stitcher->stitch(imgs, pano);
  197. stopTimer();
  198. }
  199. EXPECT_NEAR(pano.size().width, width, allowed_diff);
  200. EXPECT_NEAR(pano.size().height, height, allowed_diff);
  201. SANITY_CHECK_NOTHING();
  202. }
  203. } // namespace