forked from colmap/pycolmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_view_geometry.cc
159 lines (136 loc) · 6.56 KB
/
two_view_geometry.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
// Authors: John Lambert (johnwlambert), Paul-Edouard Sarlin (skydes)
#include <iostream>
#include <fstream>
#include "colmap/base/camera.h"
#include "colmap/base/pose.h"
#include "colmap/estimators/two_view_geometry.h"
#include "colmap/optim/loransac.h"
#include "colmap/util/random.h"
using namespace colmap;
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/eigen.h>
namespace py = pybind11;
#include "log_exceptions.h"
#include "helpers.h"
py::dict two_view_geometry_estimation(
const std::vector<Eigen::Vector2d> points2D1,
const std::vector<Eigen::Vector2d> points2D2,
Camera& camera1,
Camera& camera2,
TwoViewGeometry::Options options
) {
SetPRNGSeed(0);
// Check that both vectors have the same size.
THROW_CHECK_EQ(points2D1.size(), points2D2.size());
// Failure output dictionary.
py::dict failure_dict;
failure_dict["success"] = false;
py::gil_scoped_release release;
FeatureMatches matches;
matches.reserve(points2D1.size());
for (size_t i=0; i < points2D1.size(); i++) {
matches.emplace_back(i,i);
}
TwoViewGeometry two_view_geometry;
two_view_geometry.EstimateCalibrated(
camera1, points2D1, camera2, points2D2, matches, options);
if (!two_view_geometry.EstimateRelativePose(camera1, points2D1, camera2, points2D2)) {
return failure_dict;
}
const FeatureMatches inlier_matches = two_view_geometry.inlier_matches;
// Convert vector<char> to vector<int>.
std::vector<bool> inliers(points2D1.size(), false);
for (auto m : inlier_matches) {
inliers[m.point2D_idx1] = true;
}
// Success output dictionary.
// See https://github.com/colmap/colmap/blob/dev/src/estimators/two_view_geometry.h#L48
// for a definition of the different configuration types.
py::gil_scoped_acquire acquire;
py::dict success_dict;
success_dict["success"] = true;
success_dict["configuration_type"] = two_view_geometry.config;
success_dict["qvec"] = two_view_geometry.qvec;
success_dict["tvec"] = two_view_geometry.tvec;
success_dict["num_inliers"] = inlier_matches.size();
success_dict["inliers"] = inliers;
return success_dict;
}
py::dict two_view_geometry_estimation(
const std::vector<Eigen::Vector2d> points2D1,
const std::vector<Eigen::Vector2d> points2D2,
Camera& camera1,
Camera& camera2,
const double max_error_px,
const double min_inlier_ratio,
const int min_num_trials,
const int max_num_trials,
const double confidence
) {
TwoViewGeometry::Options two_view_geometry_options;
two_view_geometry_options.ransac_options.max_error = max_error_px;
two_view_geometry_options.ransac_options.min_inlier_ratio = min_inlier_ratio;
two_view_geometry_options.ransac_options.min_num_trials = min_num_trials;
two_view_geometry_options.ransac_options.max_num_trials = max_num_trials;
two_view_geometry_options.ransac_options.confidence = confidence;
return two_view_geometry_estimation(
points2D1, points2D2, camera1, camera2, two_view_geometry_options);
}
void bind_two_view_geometry_estimation(py::module& m, py::class_<RANSACOptions> PyRANSACOptions) {
auto PyEstimationOptions =
py::class_<TwoViewGeometry::Options>(m, "TwoViewGeometryOptions")
.def(py::init<>([PyRANSACOptions]() {
TwoViewGeometry::Options options;
// init through Python to obtain the new defaults defined in __init__
options.ransac_options = PyRANSACOptions().cast<RANSACOptions>();
return options;
}))
.def_readwrite("min_num_inliers", &TwoViewGeometry::Options::min_num_inliers)
.def_readwrite("min_E_F_inlier_ratio", &TwoViewGeometry::Options::min_E_F_inlier_ratio)
.def_readwrite("max_H_inlier_ratio", &TwoViewGeometry::Options::max_H_inlier_ratio)
.def_readwrite("watermark_min_inlier_ratio", &TwoViewGeometry::Options::watermark_min_inlier_ratio)
.def_readwrite("watermark_border_size", &TwoViewGeometry::Options::watermark_border_size)
.def_readwrite("detect_watermark", &TwoViewGeometry::Options::detect_watermark)
.def_readwrite("multiple_ignore_watermark", &TwoViewGeometry::Options::multiple_ignore_watermark)
.def_readwrite("ransac", &TwoViewGeometry::Options::ransac_options);
make_dataclass(PyEstimationOptions);
auto est_options = PyEstimationOptions().cast<TwoViewGeometry::Options>();
py::enum_<TwoViewGeometry::ConfigurationType>(m, "TwoViewGeometry")
.value("UNDEFINED", TwoViewGeometry::UNDEFINED)
.value("DEGENERATE", TwoViewGeometry::DEGENERATE)
.value("CALIBRATED", TwoViewGeometry::CALIBRATED)
.value("UNCALIBRATED", TwoViewGeometry::UNCALIBRATED)
.value("PLANAR", TwoViewGeometry::PLANAR)
.value("PANORAMIC", TwoViewGeometry::PANORAMIC)
.value("PLANAR_OR_PANORAMIC", TwoViewGeometry::PLANAR_OR_PANORAMIC)
.value("WATERMARK", TwoViewGeometry::WATERMARK)
.value("MULTIPLE", TwoViewGeometry::MULTIPLE);
m.def(
"two_view_geometry_estimation",
static_cast<py::dict (*)(const std::vector<Eigen::Vector2d>,
const std::vector<Eigen::Vector2d>,
Camera&, Camera&,
const TwoViewGeometry::Options
)>(&two_view_geometry_estimation),
py::arg("points2D1"), py::arg("points2D2"),
py::arg("camera1"), py::arg("camera2"),
py::arg("estimation_options") = est_options,
"Generic two-view geometry estimation");
m.def(
"two_view_geometry_estimation",
static_cast<py::dict (*)(const std::vector<Eigen::Vector2d>,
const std::vector<Eigen::Vector2d>,
Camera&, Camera&,
const double, const double,
const int, const int, const double
)>(&two_view_geometry_estimation),
py::arg("points2D1"), py::arg("points2D2"),
py::arg("camera1"), py::arg("camera2"),
py::arg("max_error_px") = est_options.ransac_options.max_error,
py::arg("min_inlier_ratio") = est_options.ransac_options.min_inlier_ratio,
py::arg("min_num_trials") = est_options.ransac_options.min_num_trials,
py::arg("max_num_trials") = est_options.ransac_options.max_num_trials,
py::arg("confidence") = est_options.ransac_options.confidence,
"Generic two-view geometry estimation");
}