Blender V4.3
fundamental_test.cc
Go to the documentation of this file.
1// Copyright (c) 2007, 2008 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#include <iostream>
22
29#include "testing/testing.h"
30
31namespace {
32
33using namespace libmv;
34
35TEST(Fundamental, FundamentalFromProjections) {
36 Mat34 P1_gt, P2_gt;
37 // clang-format off
38 P1_gt << 1, 0, 0, 0,
39 0, 1, 0, 0,
40 0, 0, 1, 0;
41 P2_gt << 1, 1, 1, 3,
42 0, 2, 0, 3,
43 0, 1, 1, 0;
44 // clang-format on
45 Mat3 F_gt;
46 FundamentalFromProjections(P1_gt, P2_gt, &F_gt);
47
48 Mat34 P1, P2;
49 ProjectionsFromFundamental(F_gt, &P1, &P2);
50
51 Mat3 F;
53
54 EXPECT_MATRIX_PROP(F_gt, F, 1e-6);
55}
56
57TEST(Fundamental, PreconditionerFromPoints) {
58 int n = 4;
59 Mat points(2, n);
60 // clang-format off
61 points << 0, 0, 1, 1,
62 0, 2, 1, 3;
63 // clang-format on
64
65 Mat3 T;
66 PreconditionerFromPoints(points, &T);
67
68 Mat normalized_points;
69 ApplyTransformationToPoints(points, T, &normalized_points);
70
71 Vec mean, variance;
72 MeanAndVarianceAlongRows(normalized_points, &mean, &variance);
73
74 EXPECT_NEAR(0, mean(0), 1e-8);
75 EXPECT_NEAR(0, mean(1), 1e-8);
76 EXPECT_NEAR(2, variance(0), 1e-8);
77 EXPECT_NEAR(2, variance(1), 1e-8);
78}
79
80TEST(Fundamental, EssentialFromFundamental) {
82
83 Mat3 E_from_Rt;
84 EssentialFromRt(d.R1, d.t1, d.R2, d.t2, &E_from_Rt);
85
86 Mat3 E_from_F;
87 EssentialFromFundamental(d.F, d.K1, d.K2, &E_from_F);
88
89 EXPECT_MATRIX_PROP(E_from_Rt, E_from_F, 1e-6);
90}
91
92TEST(Fundamental, MotionFromEssential) {
94
95 Mat3 E;
96 EssentialFromRt(d.R1, d.t1, d.R2, d.t2, &E);
97
98 Mat3 R;
99 Vec3 t;
100 RelativeCameraMotion(d.R1, d.t1, d.R2, d.t2, &R, &t);
101 NormalizeL2(&t);
102
103 std::vector<Mat3> Rs;
104 std::vector<Vec3> ts;
105 MotionFromEssential(E, &Rs, &ts);
106 bool one_solution_is_correct = false;
107 for (size_t i = 0; i < Rs.size(); ++i) {
108 if (FrobeniusDistance(Rs[i], R) < 1e-8 && DistanceL2(ts[i], t) < 1e-8) {
109 one_solution_is_correct = true;
110 break;
111 }
112 }
113 EXPECT_TRUE(one_solution_is_correct);
114}
115
118
119 Mat3 E;
120 EssentialFromRt(d.R1, d.t1, d.R2, d.t2, &E);
121
122 Mat3 R;
123 Vec3 t;
124 RelativeCameraMotion(d.R1, d.t1, d.R2, d.t2, &R, &t);
125 NormalizeL2(&t);
126
127 std::vector<Mat3> Rs;
128 std::vector<Vec3> ts;
129 MotionFromEssential(E, &Rs, &ts);
130
131 Vec2 x1, x2;
132 MatrixColumn(d.x1, 0, &x1);
133 MatrixColumn(d.x2, 0, &x2);
134 int solution = MotionFromEssentialChooseSolution(Rs, ts, d.K1, x1, d.K2, x2);
135
136 EXPECT_LE(0, solution);
137 EXPECT_LE(solution, 3);
138 EXPECT_LE(FrobeniusDistance(Rs[solution], R), 1e-8);
139 EXPECT_LE(DistanceL2(ts[solution], t), 1e-8);
140}
141
144
145 Mat3 E;
146 EssentialFromRt(d.R1, d.t1, d.R2, d.t2, &E);
147
148 Mat3 R;
149 Vec3 t;
150 RelativeCameraMotion(d.R1, d.t1, d.R2, d.t2, &R, &t);
151 NormalizeL2(&t);
152
153 Vec2 x1, x2;
154 MatrixColumn(d.x1, 0, &x1);
155 MatrixColumn(d.x2, 0, &x2);
156
157 Mat3 R_estimated;
158 Vec3 t_estimated;
160 E, d.K1, x1, d.K2, x2, &R_estimated, &t_estimated);
161
162 EXPECT_LE(FrobeniusDistance(R_estimated, R), 1e-8);
163 EXPECT_LE(DistanceL2(t_estimated, t), 1e-8);
164}
165
166} // namespace
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
float[3][3] Mat3
Definition gpu_matrix.cc:28
#define T
#define F
#define R
Eigen::VectorXd Vec
Definition numeric.h:61
void EssentialFromRt(const Mat3 &R1, const Vec3 &t1, const Mat3 &R2, const Vec3 &t2, Mat3 *E)
void FundamentalFromProjections(const Mat34 &P1, const Mat34 &P2, Mat3 *F)
void PreconditionerFromPoints(const Mat &points, Mat3 *T)
double DistanceL2(const TVec &x, const TVec &y)
Definition numeric.h:212
TwoViewDataSet TwoRealisticCameras(bool same_K)
Eigen::Vector2d Vec2
Definition numeric.h:105
TEST(PolynomialCameraIntrinsics2, ApplyOnFocalCenter)
double FrobeniusDistance(const TMat &A, const TMat &B)
Definition numeric.h:278
Eigen::MatrixXd Mat
Definition numeric.h:60
void MatrixColumn(const Mat &A, int i, Vec2 *v)
Definition numeric.cc:127
void MeanAndVarianceAlongRows(const Mat &A, Vec *mean_pointer, Vec *variance_pointer)
Definition numeric.cc:90
void ProjectionsFromFundamental(const Mat3 &F, Mat34 *P1, Mat34 *P2)
void MotionFromEssential(const Mat3 &E, std::vector< Mat3 > *Rs, std::vector< Vec3 > *ts)
bool MotionFromEssentialAndCorrespondence(const Mat3 &E, const Mat3 &K1, const Vec2 &x1, const Mat3 &K2, const Vec2 &x2, Mat3 *R, Vec3 *t)
Eigen::Matrix< double, 3, 4 > Mat34
Definition numeric.h:73
void ApplyTransformationToPoints(const Mat &points, const Mat3 &T, Mat *transformed_points)
int MotionFromEssentialChooseSolution(const std::vector< Mat3 > &Rs, const std::vector< Vec3 > &ts, const Mat3 &K1, const Vec2 &x1, const Mat3 &K2, const Vec2 &x2)
void EssentialFromFundamental(const Mat3 &F, const Mat3 &K1, const Mat3 &K2, Mat3 *E)
void RelativeCameraMotion(const Mat3 &R1, const Vec3 &t1, const Mat3 &R2, const Vec3 &t2, Mat3 *R, Vec3 *t)
Eigen::Vector3d Vec3
Definition numeric.h:106
double NormalizeL2(TVec *x)
Definition numeric.h:232