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