test_affine3d_estimator.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. class CV_Affine3D_EstTest : public cvtest::BaseTest
  45. {
  46. public:
  47. CV_Affine3D_EstTest();
  48. ~CV_Affine3D_EstTest();
  49. protected:
  50. void run(int);
  51. bool test4Points();
  52. bool testNPoints();
  53. };
  54. CV_Affine3D_EstTest::CV_Affine3D_EstTest()
  55. {
  56. }
  57. CV_Affine3D_EstTest::~CV_Affine3D_EstTest() {}
  58. float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }
  59. struct WrapAff
  60. {
  61. const double *F;
  62. WrapAff(const Mat& aff) : F(aff.ptr<double>()) {}
  63. Point3f operator()(const Point3f& p)
  64. {
  65. return Point3f( (float)(p.x * F[0] + p.y * F[1] + p.z * F[2] + F[3]),
  66. (float)(p.x * F[4] + p.y * F[5] + p.z * F[6] + F[7]),
  67. (float)(p.x * F[8] + p.y * F[9] + p.z * F[10] + F[11]) );
  68. }
  69. };
  70. bool CV_Affine3D_EstTest::test4Points()
  71. {
  72. Mat aff(3, 4, CV_64F);
  73. cv::randu(aff, Scalar(1), Scalar(3));
  74. // setting points that are no in the same line
  75. Mat fpts(1, 4, CV_32FC3);
  76. Mat tpts(1, 4, CV_32FC3);
  77. fpts.ptr<Point3f>()[0] = Point3f( rngIn(1,2), rngIn(1,2), rngIn(5, 6) );
  78. fpts.ptr<Point3f>()[1] = Point3f( rngIn(3,4), rngIn(3,4), rngIn(5, 6) );
  79. fpts.ptr<Point3f>()[2] = Point3f( rngIn(1,2), rngIn(3,4), rngIn(5, 6) );
  80. fpts.ptr<Point3f>()[3] = Point3f( rngIn(3,4), rngIn(1,2), rngIn(5, 6) );
  81. std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(), WrapAff(aff));
  82. Mat aff_est;
  83. vector<uchar> outliers;
  84. estimateAffine3D(fpts, tpts, aff_est, outliers);
  85. const double thres = 1e-3;
  86. if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
  87. {
  88. //cout << cvtest::norm(aff_est, aff, NORM_INF) << endl;
  89. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  90. return false;
  91. }
  92. return true;
  93. }
  94. struct Noise
  95. {
  96. float l;
  97. Noise(float level) : l(level) {}
  98. Point3f operator()(const Point3f& p)
  99. {
  100. RNG& rng = theRNG();
  101. return Point3f( p.x + l * (float)rng, p.y + l * (float)rng, p.z + l * (float)rng);
  102. }
  103. };
  104. bool CV_Affine3D_EstTest::testNPoints()
  105. {
  106. Mat aff(3, 4, CV_64F);
  107. cv::randu(aff, Scalar(-2), Scalar(2));
  108. // setting points that are no in the same line
  109. const int n = 100;
  110. const int m = 3*n/5;
  111. const Point3f shift_outl = Point3f(15, 15, 15);
  112. const float noise_level = 20.f;
  113. Mat fpts(1, n, CV_32FC3);
  114. Mat tpts(1, n, CV_32FC3);
  115. randu(fpts, Scalar::all(0), Scalar::all(100));
  116. std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(), WrapAff(aff));
  117. /* adding noise*/
  118. std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m,
  119. [=] (const Point3f& pt) -> Point3f { return Noise(noise_level)(pt + shift_outl); });
  120. Mat aff_est;
  121. vector<uchar> outl;
  122. int res = estimateAffine3D(fpts, tpts, aff_est, outl);
  123. if (!res)
  124. {
  125. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  126. return false;
  127. }
  128. const double thres = 1e-4;
  129. if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
  130. {
  131. cout << "aff est: " << aff_est << endl;
  132. cout << "aff ref: " << aff << endl;
  133. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  134. return false;
  135. }
  136. bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
  137. m == accumulate(outl.begin(), outl.begin() + m, 0);
  138. if (!outl_good)
  139. {
  140. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  141. return false;
  142. }
  143. return true;
  144. }
  145. void CV_Affine3D_EstTest::run( int /* start_from */)
  146. {
  147. cvtest::DefaultRngAuto dra;
  148. if (!test4Points())
  149. return;
  150. if (!testNPoints())
  151. return;
  152. ts->set_failed_test_info(cvtest::TS::OK);
  153. }
  154. TEST(Calib3d_EstimateAffine3D, accuracy) { CV_Affine3D_EstTest test; test.safe_run(); }
  155. TEST(Calib3d_EstimateAffine3D, regression_16007)
  156. {
  157. std::vector<cv::Point3f> m1, m2;
  158. m1.push_back(Point3f(1.0f, 0.0f, 0.0f)); m2.push_back(Point3f(1.0f, 1.0f, 0.0f));
  159. m1.push_back(Point3f(1.0f, 0.0f, 1.0f)); m2.push_back(Point3f(1.0f, 1.0f, 1.0f));
  160. m1.push_back(Point3f(0.5f, 0.0f, 0.5f)); m2.push_back(Point3f(0.5f, 1.0f, 0.5f));
  161. m1.push_back(Point3f(2.5f, 0.0f, 2.5f)); m2.push_back(Point3f(2.5f, 1.0f, 2.5f));
  162. m1.push_back(Point3f(2.0f, 0.0f, 1.0f)); m2.push_back(Point3f(2.0f, 1.0f, 1.0f));
  163. cv::Mat m3D, inl;
  164. int res = cv::estimateAffine3D(m1, m2, m3D, inl);
  165. EXPECT_EQ(1, res);
  166. }
  167. TEST(Calib3d_EstimateAffine3D, umeyama_3_pt)
  168. {
  169. std::vector<cv::Vec3d> points = {{{0.80549149, 0.8225781, 0.79949521},
  170. {0.28906756, 0.57158557, 0.9864789},
  171. {0.58266182, 0.65474983, 0.25078834}}};
  172. cv::Mat R = (cv::Mat_<double>(3,3) << 0.9689135, -0.0232753, 0.2463025,
  173. 0.0236362, 0.9997195, 0.0014915,
  174. -0.2462682, 0.0043765, 0.9691918);
  175. cv::Vec3d t(1., 2., 3.);
  176. cv::Affine3d transform(R, t);
  177. std::vector<cv::Vec3d> transformed_points(points.size());
  178. std::transform(points.begin(), points.end(), transformed_points.begin(), [transform](const cv::Vec3d v){return transform * v;});
  179. double scale;
  180. cv::Mat trafo_est = estimateAffine3D(points, transformed_points, &scale);
  181. Mat R_est(trafo_est(Rect(0, 0, 3, 3)));
  182. EXPECT_LE(cvtest::norm(R_est, R, NORM_INF), 1e-6);
  183. Vec3d t_est = trafo_est.col(3);
  184. EXPECT_LE(cvtest::norm(t_est, t, NORM_INF), 1e-6);
  185. EXPECT_NEAR(scale, 1.0, 1e-6);
  186. }
  187. }} // namespace