test_affine2d_estimator.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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> EstimateAffine2D;
  45. static float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
  46. TEST_P(EstimateAffine2D, test3Points)
  47. {
  48. // try more transformations
  49. for (size_t i = 0; i < 500; ++i)
  50. {
  51. Mat aff(2, 3, CV_64F);
  52. cv::randu(aff, 1., 3.);
  53. Mat fpts(1, 3, CV_32FC2);
  54. Mat tpts(1, 3, CV_32FC2);
  55. // setting points that are not in the same line
  56. fpts.at<Point2f>(0) = Point2f( rngIn(1,2), rngIn(5,6) );
  57. fpts.at<Point2f>(1) = Point2f( rngIn(3,4), rngIn(3,4) );
  58. fpts.at<Point2f>(2) = Point2f( rngIn(1,2), rngIn(3,4) );
  59. transform(fpts, tpts, aff);
  60. vector<uchar> inliers;
  61. Mat aff_est = estimateAffine2D(fpts, tpts, inliers, GetParam() /* method */);
  62. EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);
  63. // all must be inliers
  64. EXPECT_EQ(countNonZero(inliers), 3);
  65. }
  66. }
  67. TEST_P(EstimateAffine2D, testNPoints)
  68. {
  69. // try more transformations
  70. for (size_t i = 0; i < 500; ++i)
  71. {
  72. Mat aff(2, 3, CV_64F);
  73. cv::randu(aff, -2., 2.);
  74. const int method = GetParam();
  75. const int n = 100;
  76. int m;
  77. // LMEDS can't handle more than 50% outliers (by design)
  78. if (method == LMEDS)
  79. m = 3*n/5;
  80. else
  81. m = 2*n/5;
  82. const float shift_outl = 15.f;
  83. const float noise_level = 20.f;
  84. Mat fpts(1, n, CV_32FC2);
  85. Mat tpts(1, n, CV_32FC2);
  86. randu(fpts, 0., 100.);
  87. transform(fpts, tpts, aff);
  88. /* adding noise to some points */
  89. Mat outliers = tpts.colRange(m, n);
  90. outliers.reshape(1) += shift_outl;
  91. Mat noise (outliers.size(), outliers.type());
  92. randu(noise, 0., noise_level);
  93. outliers += noise;
  94. vector<uchar> inliers;
  95. Mat aff_est = estimateAffine2D(fpts, tpts, inliers, method);
  96. EXPECT_FALSE(aff_est.empty()) << "estimation failed, unable to estimate transform";
  97. EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-4);
  98. bool inliers_good = count(inliers.begin(), inliers.end(), 1) == m &&
  99. m == accumulate(inliers.begin(), inliers.begin() + m, 0);
  100. EXPECT_TRUE(inliers_good);
  101. }
  102. }
  103. // test conversion from other datatypes than float
  104. TEST_P(EstimateAffine2D, testConversion)
  105. {
  106. Mat aff(2, 3, CV_32S);
  107. cv::randu(aff, 1., 3.);
  108. std::vector<Point> fpts(3);
  109. std::vector<Point> tpts(3);
  110. // setting points that are not in the same line
  111. fpts[0] = Point2f( rngIn(1,2), rngIn(5,6) );
  112. fpts[1] = Point2f( rngIn(3,4), rngIn(3,4) );
  113. fpts[2] = Point2f( rngIn(1,2), rngIn(3,4) );
  114. transform(fpts, tpts, aff);
  115. vector<uchar> inliers;
  116. Mat aff_est = estimateAffine2D(fpts, tpts, inliers, GetParam() /* method */);
  117. ASSERT_FALSE(aff_est.empty());
  118. aff.convertTo(aff, CV_64F); // need to convert before compare
  119. EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);
  120. // all must be inliers
  121. EXPECT_EQ(countNonZero(inliers), 3);
  122. }
  123. INSTANTIATE_TEST_CASE_P(Calib3d, EstimateAffine2D, Method::all());
  124. // https://github.com/opencv/opencv/issues/14259
  125. TEST(EstimateAffine2D, issue_14259_dont_change_inputs)
  126. {
  127. /*const static*/ float pts0_[10] = {
  128. 0.0f, 0.0f,
  129. 0.0f, 8.0f,
  130. 4.0f, 0.0f, // outlier
  131. 8.0f, 8.0f,
  132. 8.0f, 0.0f
  133. };
  134. /*const static*/ float pts1_[10] = {
  135. 0.1f, 0.1f,
  136. 0.1f, 8.1f,
  137. 0.0f, 4.0f, // outlier
  138. 8.1f, 8.1f,
  139. 8.1f, 0.1f
  140. };
  141. Mat pts0(Size(1, 5), CV_32FC2, (void*)pts0_);
  142. Mat pts1(Size(1, 5), CV_32FC2, (void*)pts1_);
  143. Mat pts0_copy = pts0.clone();
  144. Mat pts1_copy = pts1.clone();
  145. Mat inliers;
  146. cv::Mat A = cv::estimateAffine2D(pts0, pts1, inliers);
  147. for(int i = 0; i < pts0.rows; ++i)
  148. {
  149. EXPECT_EQ(pts0_copy.at<Vec2f>(i), pts0.at<Vec2f>(i)) << "pts0: i=" << i;
  150. }
  151. for(int i = 0; i < pts1.rows; ++i)
  152. {
  153. EXPECT_EQ(pts1_copy.at<Vec2f>(i), pts1.at<Vec2f>(i)) << "pts1: i=" << i;
  154. }
  155. EXPECT_EQ(0, (int)inliers.at<uchar>(2));
  156. }
  157. }} // namespace