Welcome to mirror list, hosted at ThFree Co, Russian Federation.

euclidean_resection_test.cc « multiview « libmv « libmv « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ec9dbda3cfee7b05a5d30b060b337457f75902e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) 2009 libmv authors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#include "libmv/multiview/euclidean_resection.h"
#include "libmv/numeric/numeric.h"
#include "libmv/logging/logging.h"
#include "libmv/multiview/projection.h"
#include "testing/testing.h"

using namespace libmv::euclidean_resection;
using namespace libmv;

// Generates all necessary inputs and expected outputs for EuclideanResection.
void CreateCameraSystem(const Mat3& KK,
                        const Mat3X& x_image,
                        const Vec& X_distances,
                        const Mat3& R_input,
                        const Vec3& T_input,
                        Mat2X *x_camera,
                        Mat3X *X_world,
                        Mat3  *R_expected,
                        Vec3  *T_expected) {
  int num_points = x_image.cols();

  Mat3X x_unit_cam(3, num_points);
  x_unit_cam = KK.inverse() * x_image;

  // Create normalized camera coordinates to be used as an input to the PnP
  // function, instead of using NormalizeColumnVectors(&x_unit_cam).
  *x_camera = x_unit_cam.block(0, 0, 2, num_points);
  for (int i = 0; i < num_points; ++i) {
    x_unit_cam.col(i).normalize();
  }

  // Create the 3D points in the camera system.
  Mat X_camera(3, num_points);
  for (int i = 0; i < num_points; ++i) {
    X_camera.col(i) = X_distances(i) * x_unit_cam.col(i);
  }

  // Apply the transformation to the camera 3D points
  Mat translation_matrix(3, num_points);
  translation_matrix.row(0).setConstant(T_input(0));
  translation_matrix.row(1).setConstant(T_input(1));
  translation_matrix.row(2).setConstant(T_input(2));

  *X_world = R_input * X_camera + translation_matrix;

  // Create the expected result for comparison.
  *R_expected = R_input.transpose();
  *T_expected = *R_expected * (-T_input);
};

TEST(AbsoluteOrientation, QuaternionSolution) {
  int num_points = 4;
  Mat X;
  Mat Xp;
  X = 100 * Mat::Random(3, num_points);

  // Create a random translation and rotation.
  Mat3 R_input;
  R_input = Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitY())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ());

  Vec3 t_input;
  t_input.setRandom();
  t_input = 100 * t_input;

  Mat translation_matrix(3, num_points);
  translation_matrix.row(0).setConstant(t_input(0));
  translation_matrix.row(1).setConstant(t_input(1));
  translation_matrix.row(2).setConstant(t_input(2));

  // Create the transformed 3D points Xp as Xp = R * X + t.
  Xp = R_input * X + translation_matrix;

  // Output variables.
  Mat3 R;
  Vec3 t;

  AbsoluteOrientation(X, Xp, &R, &t);

  EXPECT_MATRIX_NEAR(t, t_input, 1e-6);
  EXPECT_MATRIX_NEAR(R, R_input, 1e-8);
}

TEST(EuclideanResection, Points4KnownImagePointsRandomTranslationRotation) {
  // In this test only the translation and rotation are random. The image
  // points are selected from a real case and are well conditioned.
  Vec2i image_dimensions;
  image_dimensions << 1600, 1200;

  Mat3 KK;
  KK << 2796, 0,     804,
        0 ,   2796,  641,
        0,    0,     1;

  // The real image points.
  int num_points = 4;
  Mat3X x_image(3, num_points);
  x_image << 1164.06, 734.948, 749.599, 430.727,
             681.386, 844.59, 496.315,  580.775,
             1,       1,      1,        1;


  // A vector of the 4 distances to the 3D points.
  Vec X_distances = 100 * Vec::Random(num_points).array().abs();

  // Create the random camera motion R and t that resection should recover.
  Mat3 R_input;
  R_input = Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitY())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ());

  Vec3 T_input;
  T_input.setRandom();
  T_input = 100 * T_input;

  // Create the camera system, also getting the expected result of the
  // transformation.
  Mat3 R_expected;
  Vec3 T_expected;
  Mat3X X_world;
  Mat2X x_camera;
  CreateCameraSystem(KK, x_image, X_distances, R_input, T_input,
                     &x_camera, &X_world, &R_expected, &T_expected);

  // Finally, run the code under test.
  Mat3 R_output;
  Vec3 T_output;
  EuclideanResection(x_camera, X_world,
                     &R_output, &T_output,
                     RESECTION_ANSAR_DANIILIDIS);

  EXPECT_MATRIX_NEAR(T_output, T_expected, 1e-5);
  EXPECT_MATRIX_NEAR(R_output, R_expected, 1e-7);

  // For now, the EPnP doesn't have a non-linear optimization step and so is
  // not precise enough with only 4 points.
  //
  // TODO(jmichot): Reenable this test when there is nonlinear refinement.
#if 0
  R_output.setIdentity();
  T_output.setZero();

  EuclideanResection(x_camera, X_world,
                     &R_output, &T_output,
                     RESECTION_EPNP);

  EXPECT_MATRIX_NEAR(T_output, T_expected, 1e-5);
  EXPECT_MATRIX_NEAR(R_output, R_expected, 1e-7);*/
#endif
}

// TODO(jmichot): Reduce the code duplication here with the code above.
TEST(EuclideanResection, Points6AllRandomInput) {
  Mat3 KK;
  KK << 2796, 0,    804,
        0 ,   2796, 641,
        0,    0,    1;

  // Create random image points for a 1600x1200 image.
  int w = 1600;
  int h = 1200;
  int num_points = 6;
  Mat3X x_image(3, num_points);
  x_image.row(0) = w * Vec::Random(num_points).array().abs();
  x_image.row(1) = h * Vec::Random(num_points).array().abs();
  x_image.row(2).setOnes();

  // Normalized camera coordinates to be used as an input to the PnP function.
  Mat2X x_camera;
  Vec X_distances = 100 * Vec::Random(num_points).array().abs();

  // Create the random camera motion R and t that resection should recover.
  Mat3 R_input;
  R_input = Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitY())
          * Eigen::AngleAxisd(rand(), Eigen::Vector3d::UnitZ());

  Vec3 T_input;
  T_input.setRandom();
  T_input = 100 * T_input;

  // Create the camera system.
  Mat3 R_expected;
  Vec3 T_expected;
  Mat3X X_world;
  CreateCameraSystem(KK, x_image, X_distances, R_input, T_input,
                     &x_camera, &X_world, &R_expected, &T_expected);

  // Test each of the resection methods.
  {
    Mat3 R_output;
    Vec3 T_output;
    EuclideanResection(x_camera, X_world,
                       &R_output, &T_output,
                       RESECTION_ANSAR_DANIILIDIS);
    EXPECT_MATRIX_NEAR(T_output, T_expected, 1e-5);
    EXPECT_MATRIX_NEAR(R_output, R_expected, 1e-7);
  }
  {
    Mat3 R_output;
    Vec3 T_output;
    EuclideanResection(x_camera, X_world,
                       &R_output, &T_output,
                       RESECTION_EPNP);
    EXPECT_MATRIX_NEAR(T_output, T_expected, 1e-5);
    EXPECT_MATRIX_NEAR(R_output, R_expected, 1e-7);
  }
  {
    Mat3 R_output;
    Vec3 T_output;
    EuclideanResection(x_image, X_world, KK,
                       &R_output, &T_output);
    EXPECT_MATRIX_NEAR(T_output, T_expected, 1e-5);
    EXPECT_MATRIX_NEAR(R_output, R_expected, 1e-7);
  }
}