test_common.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 {
  6. void generateTwoViewRandomScene( TwoViewDataSet &data )
  7. {
  8. vector<Mat_<double> > points2d;
  9. vector<cv::Matx33d> Rs;
  10. vector<cv::Vec3d> ts;
  11. vector<cv::Matx34d> Ps;
  12. Matx33d K;
  13. Mat_<double> points3d;
  14. int nviews = 2;
  15. int npoints = 30;
  16. bool is_projective = true;
  17. generateScene(nviews, npoints, is_projective, K, Rs, ts, Ps, points3d, points2d);
  18. // Internal parameters (same K)
  19. data.K1 = K;
  20. data.K2 = K;
  21. // Rotation
  22. data.R1 = Rs[0];
  23. data.R2 = Rs[1];
  24. // Translation
  25. data.t1 = ts[0];
  26. data.t2 = ts[1];
  27. // Projection matrix, P = K(R|t)
  28. data.P1 = Ps[0];
  29. data.P2 = Ps[1];
  30. // Fundamental matrix
  31. fundamentalFromProjections( data.P1, data.P2, data.F );
  32. // 3D points
  33. data.X = points3d;
  34. // Projected points
  35. data.x1 = points2d[0];
  36. data.x2 = points2d[1];
  37. }
  38. /** Check the properties of a fundamental matrix:
  39. *
  40. * 1. The determinant is 0 (rank deficient)
  41. * 2. The condition x'T*F*x = 0 is satisfied to precision.
  42. */
  43. void
  44. expectFundamentalProperties( const cv::Matx33d &F,
  45. const cv::Mat_<double> &ptsA,
  46. const cv::Mat_<double> &ptsB,
  47. double precision )
  48. {
  49. EXPECT_NEAR( 0, determinant(F), precision );
  50. int n = ptsA.cols;
  51. EXPECT_EQ( n, ptsB.cols );
  52. cv::Mat_<double> x1, x2;
  53. euclideanToHomogeneous( ptsA, x1 );
  54. euclideanToHomogeneous( ptsB, x2 );
  55. for( int i = 0; i < n; ++i )
  56. {
  57. double residual = Vec3d(x2(0,i),x2(1,i),x2(2,i)).ddot( F * Vec3d(x1(0,i),x1(1,i),x1(2,i)) );
  58. EXPECT_NEAR( 0.0, residual, precision );
  59. }
  60. }
  61. void
  62. parser_2D_tracks(const string &_filename, std::vector<Mat> &points2d )
  63. {
  64. std::ifstream myfile(_filename.c_str());
  65. if (!myfile.is_open())
  66. CV_Error(cv::Error::StsError, string("Unable to read file: ") + _filename + "\n");
  67. else {
  68. double x, y;
  69. string line_str;
  70. Mat nan_mat = Mat(2, 1 , CV_64F, -1);
  71. int n_frames = 0, n_tracks = 0, track = 0;
  72. while ( getline(myfile, line_str) )
  73. {
  74. std::istringstream line(line_str);
  75. if ( track > n_tracks )
  76. {
  77. n_tracks = track;
  78. for (int i = 0; i < n_frames; ++i)
  79. cv::hconcat(points2d[i], nan_mat, points2d[i]);
  80. }
  81. for (int frame = 1; line >> x >> y; ++frame)
  82. {
  83. if ( frame > n_frames )
  84. {
  85. n_frames = frame;
  86. points2d.push_back(nan_mat);
  87. }
  88. points2d[frame-1].at<double>(0,track) = x;
  89. points2d[frame-1].at<double>(1,track) = y;
  90. }
  91. ++track;
  92. }
  93. myfile.close();
  94. }
  95. }
  96. } // namespace