test_translation3d_estimator.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. TEST(Calib3d_EstimateTranslation3D, test4Points)
  7. {
  8. Matx13d trans;
  9. cv::randu(trans, Scalar(1), Scalar(3));
  10. // setting points that are no in the same line
  11. Mat fpts(1, 4, CV_32FC3);
  12. Mat tpts(1, 4, CV_32FC3);
  13. RNG& rng = theRNG();
  14. fpts.at<Point3f>(0) = Point3f(rng.uniform(1.0f, 2.0f), rng.uniform(1.0f, 2.0f), rng.uniform(5.0f, 6.0f));
  15. fpts.at<Point3f>(1) = Point3f(rng.uniform(3.0f, 4.0f), rng.uniform(3.0f, 4.0f), rng.uniform(5.0f, 6.0f));
  16. fpts.at<Point3f>(2) = Point3f(rng.uniform(1.0f, 2.0f), rng.uniform(3.0f, 4.0f), rng.uniform(5.0f, 6.0f));
  17. fpts.at<Point3f>(3) = Point3f(rng.uniform(3.0f, 4.0f), rng.uniform(1.0f, 2.0f), rng.uniform(5.0f, 6.0f));
  18. std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + 4, tpts.ptr<Point3f>(),
  19. [&] (const Point3f& p) -> Point3f
  20. {
  21. return Point3f((float)(p.x + trans(0, 0)),
  22. (float)(p.y + trans(0, 1)),
  23. (float)(p.z + trans(0, 2)));
  24. }
  25. );
  26. Matx13d trans_est;
  27. vector<uchar> outliers;
  28. int res = estimateTranslation3D(fpts, tpts, trans_est, outliers);
  29. EXPECT_GT(res, 0);
  30. const double thres = 1e-3;
  31. EXPECT_LE(cvtest::norm(trans_est, trans, NORM_INF), thres)
  32. << "aff est: " << trans_est << endl
  33. << "aff ref: " << trans;
  34. }
  35. TEST(Calib3d_EstimateTranslation3D, testNPoints)
  36. {
  37. Matx13d trans;
  38. cv::randu(trans, Scalar(-2), Scalar(2));
  39. // setting points that are no in the same line
  40. const int n = 100;
  41. const int m = 3*n/5;
  42. const Point3f shift_outl = Point3f(15, 15, 15);
  43. const float noise_level = 20.f;
  44. Mat fpts(1, n, CV_32FC3);
  45. Mat tpts(1, n, CV_32FC3);
  46. randu(fpts, Scalar::all(0), Scalar::all(100));
  47. std::transform(fpts.ptr<Point3f>(), fpts.ptr<Point3f>() + n, tpts.ptr<Point3f>(),
  48. [&] (const Point3f& p) -> Point3f
  49. {
  50. return Point3f((float)(p.x + trans(0, 0)),
  51. (float)(p.y + trans(0, 1)),
  52. (float)(p.z + trans(0, 2)));
  53. }
  54. );
  55. /* adding noise*/
  56. std::transform(tpts.ptr<Point3f>() + m, tpts.ptr<Point3f>() + n, tpts.ptr<Point3f>() + m,
  57. [&] (const Point3f& pt) -> Point3f
  58. {
  59. Point3f p = pt + shift_outl;
  60. RNG& rng = theRNG();
  61. return Point3f(p.x + noise_level * (float)rng,
  62. p.y + noise_level * (float)rng,
  63. p.z + noise_level * (float)rng);
  64. }
  65. );
  66. Matx13d trans_est;
  67. vector<uchar> outl;
  68. int res = estimateTranslation3D(fpts, tpts, trans_est, outl);
  69. EXPECT_GT(res, 0);
  70. const double thres = 1e-4;
  71. EXPECT_LE(cvtest::norm(trans_est, trans, NORM_INF), thres)
  72. << "aff est: " << trans_est << endl
  73. << "aff ref: " << trans;
  74. bool outl_good = count(outl.begin(), outl.end(), 1) == m &&
  75. m == accumulate(outl.begin(), outl.begin() + m, 0);
  76. EXPECT_TRUE(outl_good);
  77. }
  78. }} // namespace