test_translation_2d_estimator.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. CV_ENUM(Method, RANSAC, LMEDS)
  44. typedef TestWithParam<Method> EstimateTranslation2D;
  45. static float rngIn(float from, float to) { return from + (to - from) * (float)theRNG(); }
  46. // build a pure translation 2x3 matrix
  47. static cv::Mat rngTranslationMat()
  48. {
  49. double tx = rngIn(-20.f, 20.f);
  50. double ty = rngIn(-20.f, 20.f);
  51. double t[2*3] = { 1.0, 0.0, tx,
  52. 0.0, 1.0, ty };
  53. return cv::Mat(2, 3, CV_64F, t).clone();
  54. }
  55. static inline cv::Vec2d getTxTy(const cv::Mat& T)
  56. {
  57. CV_Assert(T.rows == 2 && T.cols == 3 && T.type() == CV_64F);
  58. return cv::Vec2d(T.at<double>(0,2), T.at<double>(1,2));
  59. }
  60. TEST_P(EstimateTranslation2D, test1Point)
  61. {
  62. // minimal sample is 1 point
  63. for (size_t i = 0; i < 500; ++i)
  64. {
  65. cv::Mat T = rngTranslationMat();
  66. cv::Vec2d T_ref = getTxTy(T);
  67. cv::Mat fpts(1, 1, CV_32FC2);
  68. cv::Mat tpts(1, 1, CV_32FC2);
  69. fpts.at<cv::Point2f>(0) = cv::Point2f(rngIn(1,2), rngIn(5,6));
  70. transform(fpts, tpts, T);
  71. std::vector<uchar> inliers;
  72. cv::Vec2d T_est = estimateTranslation2D(fpts, tpts, inliers, GetParam() /* method */);
  73. EXPECT_NEAR(T_est[0], T_ref[0], 1e-6);
  74. EXPECT_NEAR(T_est[1], T_ref[1], 1e-6);
  75. EXPECT_EQ((int)inliers.size(), 1);
  76. EXPECT_EQ((int)inliers[0], 1);
  77. }
  78. }
  79. TEST_P(EstimateTranslation2D, testNPoints)
  80. {
  81. for (size_t i = 0; i < 500; ++i)
  82. {
  83. cv::Mat T = rngTranslationMat();
  84. cv::Vec2d T_ref = getTxTy(T);
  85. const int method = GetParam();
  86. const int n = 100;
  87. int m;
  88. // LMEDS can't handle more than 50% outliers (by design)
  89. if (method == LMEDS)
  90. m = 3*n/5;
  91. else
  92. m = 2*n/5;
  93. const float shift_outl = 15.f;
  94. const float noise_level = 20.f;
  95. cv::Mat fpts(1, n, CV_32FC2);
  96. cv::Mat tpts(1, n, CV_32FC2);
  97. randu(fpts, 0.f, 100.f);
  98. transform(fpts, tpts, T);
  99. /* adding noise to some points (make last n-m points outliers) */
  100. cv::Mat outliers = tpts.colRange(m, n);
  101. outliers.reshape(1) += shift_outl;
  102. cv::Mat noise(outliers.size(), outliers.type());
  103. randu(noise, 0.f, noise_level);
  104. outliers += noise;
  105. std::vector<uchar> inliers;
  106. cv::Vec2d T_est = estimateTranslation2D(fpts, tpts, inliers, method);
  107. // Check estimation produced finite values
  108. ASSERT_TRUE(std::isfinite(T_est[0]) && std::isfinite(T_est[1]));
  109. EXPECT_NEAR(T_est[0], T_ref[0], 1e-4);
  110. EXPECT_NEAR(T_est[1], T_ref[1], 1e-4);
  111. bool inliers_good = std::count(inliers.begin(), inliers.end(), 1) == m &&
  112. m == std::accumulate(inliers.begin(), inliers.begin() + m, 0);
  113. EXPECT_TRUE(inliers_good);
  114. }
  115. }
  116. // test conversion from other datatypes than float
  117. TEST_P(EstimateTranslation2D, testConversion)
  118. {
  119. cv::Mat T = rngTranslationMat();
  120. T.convertTo(T, CV_32S); // convert to int to transform ints properly
  121. std::vector<cv::Point> fpts(3);
  122. std::vector<cv::Point> tpts(3);
  123. fpts[0] = cv::Point2f(rngIn(1,2), rngIn(5,6));
  124. fpts[1] = cv::Point2f(rngIn(3,4), rngIn(3,4));
  125. fpts[2] = cv::Point2f(rngIn(1,2), rngIn(3,4));
  126. transform(fpts, tpts, T);
  127. std::vector<uchar> inliers;
  128. cv::Vec2d T_est = estimateTranslation2D(fpts, tpts, inliers, GetParam() /* method */);
  129. ASSERT_TRUE(std::isfinite(T_est[0]) && std::isfinite(T_est[1]));
  130. T.convertTo(T, CV_64F); // convert back for reference extraction
  131. cv::Vec2d T_ref = getTxTy(T);
  132. EXPECT_NEAR(T_est[0], T_ref[0], 1e-3);
  133. EXPECT_NEAR(T_est[1], T_ref[1], 1e-3);
  134. // all must be inliers
  135. EXPECT_EQ(countNonZero(inliers), 3);
  136. }
  137. INSTANTIATE_TEST_CASE_P(Calib3d, EstimateTranslation2D, Method::all());
  138. // "don't change inputs" regression, mirroring affine partial test
  139. TEST(EstimateTranslation2D, dont_change_inputs)
  140. {
  141. /*const static*/ float pts0_[10] = {
  142. 0.0f, 0.0f,
  143. 0.0f, 8.0f,
  144. 4.0f, 0.0f, // outlier
  145. 8.0f, 8.0f,
  146. 8.0f, 0.0f
  147. };
  148. /*const static*/ float pts1_[10] = {
  149. 0.1f, 0.1f,
  150. 0.1f, 8.1f,
  151. 0.0f, 4.0f, // outlier
  152. 8.1f, 8.1f,
  153. 8.1f, 0.1f
  154. };
  155. cv::Mat pts0(cv::Size(1, 5), CV_32FC2, (void*)pts0_);
  156. cv::Mat pts1(cv::Size(1, 5), CV_32FC2, (void*)pts1_);
  157. cv::Mat pts0_copy = pts0.clone();
  158. cv::Mat pts1_copy = pts1.clone();
  159. cv::Mat inliers;
  160. cv::Vec2d T = cv::estimateTranslation2D(pts0, pts1, inliers);
  161. for (int i = 0; i < pts0.rows; ++i)
  162. EXPECT_EQ(pts0_copy.at<cv::Vec2f>(i), pts0.at<cv::Vec2f>(i)) << "pts0: i=" << i;
  163. for (int i = 0; i < pts1.rows; ++i)
  164. EXPECT_EQ(pts1_copy.at<cv::Vec2f>(i), pts1.at<cv::Vec2f>(i)) << "pts1: i=" << i;
  165. EXPECT_EQ(0, (int)inliers.at<uchar>(2));
  166. // sanity: estimated translation should be finite
  167. EXPECT_TRUE(std::isfinite(T[0]) && std::isfinite(T[1]));
  168. }
  169. }} // namespace