test_precomp.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef __OPENCV_TEST_PRECOMP_HPP__
  5. #define __OPENCV_TEST_PRECOMP_HPP__
  6. #include <opencv2/sfm.hpp>
  7. #include <opencv2/core/core_c.h>
  8. #include <opencv2/ts.hpp>
  9. #include <opencv2/core.hpp>
  10. #include "scene.h"
  11. #define OPEN_TESTFILE(FNAME,FS) \
  12. FS.open(FNAME, FileStorage::READ); \
  13. if (!FS.isOpened())\
  14. {\
  15. std::cerr << "Cannot find file: " << FNAME << std::endl;\
  16. return;\
  17. }
  18. namespace opencv_test
  19. {
  20. using namespace cv::sfm;
  21. template<typename T>
  22. inline void
  23. EXPECT_MATRIX_NEAR(const T a, const T b, double tolerance)
  24. {
  25. bool dims_match = (a.rows == b.rows) && (a.cols == b.cols);
  26. EXPECT_EQ((int)a.rows, (int)b.rows);
  27. EXPECT_EQ((int)a.cols, (int)b.cols);
  28. if (dims_match)
  29. {
  30. for (int r = 0; r < a.rows; ++r)
  31. {
  32. for (int c = 0; c < a.cols; ++c)
  33. {
  34. EXPECT_NEAR(a(r, c), b(r, c), tolerance) << "r=" << r << ", c=" << c << ".";
  35. }
  36. }
  37. }
  38. }
  39. template<typename T>
  40. inline void
  41. EXPECT_VECTOR_NEAR(const T a, const T b, double tolerance)
  42. {
  43. bool dims_match = (a.rows == b.rows);
  44. EXPECT_EQ((int)a.rows,(int)b.rows) << "Matrix rows don't match.";
  45. if (dims_match)
  46. {
  47. for (int r = 0; r < a.rows; ++r)
  48. {
  49. EXPECT_NEAR(a(r), b(r), tolerance) << "r=" << r << ".";
  50. }
  51. }
  52. }
  53. template<class T>
  54. inline double
  55. cosinusBetweenMatrices(const T &a, const T &b)
  56. {
  57. double s = cv::sum( a.mul(b) )[0];
  58. return ( s / cv::norm(a) / cv::norm(b) );
  59. }
  60. // Check that sin(angle(a, b)) < tolerance
  61. template<typename T>
  62. inline void
  63. EXPECT_MATRIX_PROP(const T a, const T b, double tolerance)
  64. {
  65. bool dims_match = (a.rows == b.rows) && (a.cols == b.cols);
  66. EXPECT_EQ((int)a.rows, (int)b.rows);
  67. EXPECT_EQ((int)a.cols, (int)b.cols);
  68. if (dims_match)
  69. {
  70. double c = cosinusBetweenMatrices(a, b);
  71. if (c * c < 1)
  72. {
  73. double s = sqrt(1 - c * c);
  74. EXPECT_NEAR(0, s, tolerance);
  75. }
  76. }
  77. }
  78. struct TwoViewDataSet
  79. {
  80. cv::Matx33d K1, K2; // Internal parameters
  81. cv::Matx33d R1, R2; // Rotation
  82. cv::Vec3d t1, t2; // Translation
  83. cv::Matx34d P1, P2; // Projection matrix, P = K(R|t)
  84. cv::Matx33d F; // Fundamental matrix
  85. cv::Mat_<double> X; // 3D points
  86. cv::Mat_<double> x1, x2; // Projected points
  87. };
  88. void
  89. generateTwoViewRandomScene(TwoViewDataSet &data);
  90. /** Check the properties of a fundamental matrix:
  91. *
  92. * 1. The determinant is 0 (rank deficient)
  93. * 2. The condition x'T*F*x = 0 is satisfied to precision.
  94. */
  95. void
  96. expectFundamentalProperties( const cv::Matx33d &F,
  97. const cv::Mat_<double> &ptsA,
  98. const cv::Mat_<double> &ptsB,
  99. double precision = 1e-9 );
  100. /**
  101. * 2D tracked points
  102. * -----------------
  103. *
  104. * The format is:
  105. *
  106. * row1 : x1 y1 x2 y2 ... x36 y36 for track 1
  107. * row2 : x1 y1 x2 y2 ... x36 y36 for track 2
  108. * etc
  109. *
  110. * i.e. a row gives the 2D measured position of a point as it is tracked
  111. * through frames 1 to 36. If there is no match found in a view then x
  112. * and y are -1.
  113. *
  114. * Each row corresponds to a different point.
  115. *
  116. */
  117. void
  118. parser_2D_tracks(const std::string &_filename, std::vector<cv::Mat> &points2d );
  119. } // namespace
  120. #endif