test_affine_partial2d_estimator.cpp 6.6 KB

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