perf_translation2d.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // By downloading, copying, installing or using the software you agree to this license.
  4. // If you do not agree to this license, do not download, install,
  5. // copy or use the software.
  6. //
  7. //
  8. // License Agreement
  9. // For Open Source Computer Vision Library
  10. // (3-clause BSD License)
  11. //
  12. // Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
  13. // Third party copyrights are property of their respective owners.
  14. //
  15. // Redistribution and use in source and binary forms, with or without modification,
  16. // are permitted provided that the following conditions are met:
  17. //
  18. // * Redistributions of source code must retain the above copyright notice,
  19. // this list of conditions and the following disclaimer.
  20. //
  21. // * Redistributions in binary form must reproduce the above copyright notice,
  22. // this list of conditions and the following disclaimer in the documentation
  23. // and/or other materials provided with the distribution.
  24. //
  25. // * Neither the names of the copyright holders nor the names of the contributors
  26. // may be used to endorse or promote products derived from this software
  27. // without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall copyright holders or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "perf_precomp.hpp"
  42. #include <algorithm>
  43. #include <functional>
  44. namespace opencv_test
  45. {
  46. using namespace perf;
  47. CV_ENUM(Method, RANSAC, LMEDS)
  48. typedef tuple<int, double, Method, size_t> TranslationParams;
  49. typedef TestBaseWithParam<TranslationParams> EstimateTranslation2DPerf;
  50. #define ESTIMATE_PARAMS Combine(Values(1000), Values(0.95), Method::all(), Values(10, 0))
  51. static float rngIn(float from, float to) { return from + (to - from) * (float)theRNG(); }
  52. static cv::Mat rngTranslationMat()
  53. {
  54. double tx = rngIn(-2.f, 2.f);
  55. double ty = rngIn(-2.f, 2.f);
  56. double t[2*3] = { 1.0, 0.0, tx,
  57. 0.0, 1.0, ty };
  58. return cv::Mat(2, 3, CV_64F, t).clone();
  59. }
  60. PERF_TEST_P(EstimateTranslation2DPerf, EstimateTranslation2D, ESTIMATE_PARAMS)
  61. {
  62. TranslationParams params = GetParam();
  63. const int n = get<0>(params);
  64. const double confidence = get<1>(params);
  65. const int method = get<2>(params);
  66. const size_t refining = get<3>(params);
  67. //fixed seed so the generated data are deterministic
  68. cv::theRNG().state = 0x12345678;
  69. // ground-truth pure translation
  70. cv::Mat T = rngTranslationMat();
  71. // LMEDS can't handle more than 50% outliers (by design)
  72. int m;
  73. if (method == LMEDS)
  74. m = 3*n/5;
  75. else
  76. m = 2*n/5;
  77. const float shift_outl = 15.f;
  78. const float noise_level = 20.f;
  79. cv::Mat fpts(1, n, CV_32FC2);
  80. cv::Mat tpts(1, n, CV_32FC2);
  81. randu(fpts, 0.f, 100.f);
  82. transform(fpts, tpts, T);
  83. // add outliers to the tail [m, n)
  84. cv::Mat outliers = tpts.colRange(m, n);
  85. outliers.reshape(1) += shift_outl;
  86. cv::Mat noise(outliers.size(), outliers.type());
  87. randu(noise, 0.f, noise_level);
  88. outliers += noise;
  89. cv::Vec2d T_est;
  90. std::vector<uchar> inliers(n);
  91. warmup(inliers, WARMUP_WRITE);
  92. warmup(fpts, WARMUP_READ);
  93. warmup(tpts, WARMUP_READ);
  94. TEST_CYCLE()
  95. {
  96. T_est = estimateTranslation2D(fpts, tpts, inliers, method,
  97. /*ransacReprojThreshold=*/3.0,
  98. /*maxIters=*/2000,
  99. /*confidence=*/confidence,
  100. /*refineIters=*/refining);
  101. }
  102. // Convert to Mat for SANITY_CHECK consistency
  103. cv::Mat T_est_mat = (cv::Mat_<double>(2,1) << T_est[0], T_est[1]);
  104. SANITY_CHECK(T_est_mat, 1e-6);
  105. }
  106. } // namespace opencv_test