-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathpnpsolver_module.cpp
88 lines (73 loc) · 3.23 KB
/
pnpsolver_module.cpp
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
/*
* This file is part of PYSLAM
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pybind11/pybind11.h>
#include <sstream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "pybind11/eigen.h"
#include "opencv_type_casters.h"
#include "PnPsolver.h"
#include "MLPnPsolver.h"
namespace py = pybind11;
using namespace pybind11::literals;
using namespace utils;
PYBIND11_MODULE(pnpsolver, m)
{
// optional module docstring
m.doc() = "pybind11 plugin for pnpsolver module";
py::class_<PnPsolverInput>(m, "PnPsolverInput")
.def(py::init<>())
.def_readwrite("points_2d", &PnPsolverInput::mvP2D)
.def_readwrite("points_3d", &PnPsolverInput::mvP3Dw)
.def_readwrite("sigmas2", &PnPsolverInput::mvSigma2)
.def_readwrite("fx", &PnPsolverInput::fx)
.def_readwrite("fy", &PnPsolverInput::fy)
.def_readwrite("cx", &PnPsolverInput::cx)
.def_readwrite("cy", &PnPsolverInput::cy);
py::class_<PnPsolver>(m, "PnPsolver")
.def(py::init<const PnPsolverInput&>())
.def("set_ransac_parameters", &PnPsolver::SetRansacParameters,
"probability"_a = 0.99, "minInliers"_a = 8, "maxIterations"_a = 300, "minSet"_a = 4, "epsilon"_a = 0.4, "th2"_a = 5.991)
.def("find", [](PnPsolver& s){
std::vector<uint8_t> vbInliers;
int nInliers;
cv::Mat transformation = s.find(vbInliers, nInliers);
return std::make_tuple(transformation, vbInliers, nInliers);
})
.def("iterate", [](PnPsolver& s, const int nIterations){
std::vector<uint8_t> vbInliers;
int nInliers;
bool bNoMore;
cv::Mat Tout = s.iterate(nIterations, bNoMore, vbInliers, nInliers);
bool ok = nInliers>0;
return std::make_tuple(ok, Tout, bNoMore, vbInliers, nInliers);
}, "nIterations"_a);
py::class_<MLPnPsolver>(m, "MLPnPsolver")
.def(py::init<const PnPsolverInput&>())
.def("set_ransac_parameters", &MLPnPsolver::SetRansacParameters,
"probability"_a = 0.99, "minInliers"_a = 8, "maxIterations"_a = 300, "minSet"_a = 6, "epsilon"_a = 0.4, "th2"_a = 5.991)
.def("iterate", [](MLPnPsolver& s, const int nIterations){
std::vector<uint8_t> vbInliers;
int nInliers;
bool bNoMore;
Eigen::Matrix4f Tout;
bool ok = s.iterate(nIterations, bNoMore, vbInliers, nInliers, Tout);
return std::make_tuple(ok, Tout, bNoMore, vbInliers, nInliers);
}, "nIterations"_a);
}