/
bundle_adjust_two_views.cc
254 lines (216 loc) · 10.1 KB
/
bundle_adjust_two_views.cc
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (C) 2015 The Regents of the University of California (Regents).
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// * Neither the name of The Regents or University of California nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Please contact the author of this library if you have any questions.
// Author: Chris Sweeney (cmsweeney@cs.ucsb.edu)
#include "theia/sfm/bundle_adjustment/bundle_adjust_two_views.h"
#include <ceres/ceres.h>
#include <Eigen/Core>
#include <vector>
#include "theia/matching/feature_correspondence.h"
#include "theia/sfm/bundle_adjustment/angular_epipolar_error.h"
#include "theia/sfm/bundle_adjustment/bundle_adjustment.h"
#include "theia/sfm/bundle_adjustment/unit_norm_three_vector_parameterization.h"
#include "theia/sfm/camera/camera.h"
#include "theia/sfm/camera/create_reprojection_error_cost_function.h"
#include "theia/sfm/camera_intrinsics_prior.h"
#include "theia/sfm/triangulation/triangulation.h"
#include "theia/sfm/twoview_info.h"
#include "theia/sfm/types.h"
#include "theia/util/timer.h"
namespace theia {
namespace {
void SetSolverOptions(const BundleAdjustmentOptions& options,
ceres::Solver::Options* solver_options) {
CHECK_NOTNULL(solver_options);
solver_options->linear_solver_type = ceres::DENSE_SCHUR;
solver_options->visibility_clustering_type = ceres::CANONICAL_VIEWS;
solver_options->logging_type = ceres::SILENT;
solver_options->num_threads = options.num_threads;
solver_options->max_num_iterations = 200;
// Solver options takes ownership of the ordering so that we can order the BA
// problem by points and cameras.
solver_options->linear_solver_ordering.reset(
new ceres::ParameterBlockOrdering);
}
// The only intrinsic parameter we want to optimize is the focal length, so we
// keep all intrinsics constant except for focal length by default.
void AddCameraParametersToProblem(const bool constant_extrinsic_parameters,
const bool constant_intrinsic_parameters,
Camera* camera,
ceres::Problem* problem) {
double* camera_extrinsics = camera->mutable_extrinsics();
double* camera_intrinsics = camera->mutable_intrinsics();
const int num_intrinsics = camera->CameraIntrinsics()->NumParameters();
// Add extrinsics to problem
problem->AddParameterBlock(camera_extrinsics, Camera::kExtrinsicsSize);
if (constant_extrinsic_parameters) {
problem->SetParameterBlockConstant(camera_extrinsics);
}
// Keep the intrinsics constant if desired.
if (constant_intrinsic_parameters) {
problem->AddParameterBlock(camera_intrinsics, num_intrinsics);
problem->SetParameterBlockConstant(camera_intrinsics);
} else {
// NOTE: We start at index 1 because the focal length is considered
// variable.
std::vector<int> constant_intrinsics(num_intrinsics - 1);
std::iota(constant_intrinsics.begin(),
constant_intrinsics.end(),
1);
ceres::SubsetParameterization* subset_parameterization =
new ceres::SubsetParameterization(num_intrinsics,
constant_intrinsics);
problem->AddParameterBlock(camera_intrinsics,
num_intrinsics,
subset_parameterization);
}
}
} // namespace
// Triangulates all 3d points and performs standard bundle adjustment on the
// points and cameras.
BundleAdjustmentSummary BundleAdjustTwoViews(
const TwoViewBundleAdjustmentOptions& options,
const std::vector<FeatureCorrespondence>& correspondences,
Camera* camera1,
Camera* camera2,
std::vector<Eigen::Vector4d>* points3d) {
CHECK_NOTNULL(camera1);
CHECK_NOTNULL(camera2);
CHECK_NOTNULL(points3d);
CHECK_EQ(points3d->size(), correspondences.size());
BundleAdjustmentSummary summary;
// Start setup timer.
Timer timer;
// Set problem options.
ceres::Problem::Options problem_options;
ceres::Problem problem(problem_options);
// Set solver options.
ceres::Solver::Options solver_options;
SetSolverOptions(options.ba_options, &solver_options);
ceres::ParameterBlockOrdering* parameter_ordering =
solver_options.linear_solver_ordering.get();
// Add the two cameras as parameter blocks.
AddCameraParametersToProblem(true,
options.constant_camera1_intrinsics,
camera1,
&problem);
AddCameraParametersToProblem(false,
options.constant_camera2_intrinsics,
camera2,
&problem);
parameter_ordering->AddElementToGroup(camera1->mutable_extrinsics(), 2);
parameter_ordering->AddElementToGroup(camera1->mutable_intrinsics(), 1);
parameter_ordering->AddElementToGroup(camera2->mutable_extrinsics(), 2);
parameter_ordering->AddElementToGroup(camera2->mutable_intrinsics(), 1);
// Add triangulated points to the problem.
for (int i = 0; i < points3d->size(); i++) {
problem.AddResidualBlock(CreateReprojectionErrorCostFunction(
camera1->GetCameraIntrinsicsModelType(),
correspondences[i].feature1),
NULL,
camera1->mutable_extrinsics(),
camera1->mutable_intrinsics(),
points3d->at(i).data());
problem.AddResidualBlock(CreateReprojectionErrorCostFunction(
camera2->GetCameraIntrinsicsModelType(),
correspondences[i].feature2),
NULL,
camera2->mutable_extrinsics(),
camera2->mutable_intrinsics(),
points3d->at(i).data());
parameter_ordering->AddElementToGroup(points3d->at(i).data(), 0);
}
// End setup time.
summary.setup_time_in_seconds = timer.ElapsedTimeInSeconds();
// Solve the problem.
ceres::Solver::Summary solver_summary;
ceres::Solve(solver_options, &problem, &solver_summary);
LOG_IF(INFO, options.ba_options.verbose) << solver_summary.FullReport();
// Set the BundleAdjustmentSummary.
summary.solve_time_in_seconds = solver_summary.total_time_in_seconds;
summary.initial_cost = solver_summary.initial_cost;
summary.final_cost = solver_summary.final_cost;
// This only indicates whether the optimization was successfully run and makes
// no guarantees on the quality or convergence.
summary.success = solver_summary.termination_type != ceres::FAILURE;
return summary;
}
BundleAdjustmentSummary BundleAdjustTwoViewsAngular(
const BundleAdjustmentOptions& options,
const std::vector<FeatureCorrespondence>& correspondences,
TwoViewInfo* info) {
CHECK_NOTNULL(info);
BundleAdjustmentSummary summary;
// Start setup timer.
Timer timer;
// Set problem options.
ceres::Problem::Options problem_options;
ceres::Problem problem(problem_options);
// Set solver options.
ceres::Solver::Options solver_options;
SetSolverOptions(options, &solver_options);
// Allow Ceres to determine the ordering.
solver_options.linear_solver_ordering.reset();
// Add the relative rotation as a parameter block.
const int kParameterBlockSize = 3;
problem.AddParameterBlock(info->rotation_2.data(), kParameterBlockSize);
// Add the position as a parameter block, ensuring that the norm is 1.
ceres::LocalParameterization* position_parameterization =
new ceres::AutoDiffLocalParameterization<
UnitNormThreeVectorParameterization, 3, 3>;
problem.AddParameterBlock(info->position_2.data(),
kParameterBlockSize,
position_parameterization);
// Add all the epipolar constraints from feature matches.
for (const FeatureCorrespondence& match : correspondences) {
problem.AddResidualBlock(
AngularEpipolarError::Create(match.feature1, match.feature2),
NULL,
info->rotation_2.data(),
info->position_2.data());
}
// End setup time.
summary.setup_time_in_seconds = timer.ElapsedTimeInSeconds();
// Solve the problem.
ceres::Solver::Summary solver_summary;
ceres::Solve(solver_options, &problem, &solver_summary);
LOG_IF(INFO, options.verbose) << solver_summary.FullReport();
// Set the BundleAdjustmentSummary.
summary.solve_time_in_seconds = solver_summary.total_time_in_seconds;
summary.initial_cost = solver_summary.initial_cost;
summary.final_cost = solver_summary.final_cost;
// This only indicates whether the optimization was successfully run and makes
// no guarantees on the quality or convergence.
summary.success = solver_summary.termination_type != ceres::FAILURE;
return summary;
}
} // namespace theia