Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pymagsac/include/magsac_python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <vector>
#include <opencv2/core/core.hpp>

// Make RANSAC sampling reproducible when seed >= 0, else restore the default
// nondeterministic behavior. Implemented in magsac_python.cpp.
void applyMagsacSeed(int seed);

int adaptiveInlierSelection_(
const std::vector<double>& srcPts_,
const std::vector<double>& dstPts_,
Expand Down
36 changes: 26 additions & 10 deletions src/pymagsac/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace py = pybind11;


py::tuple adaptiveInlierSelection(
py::array_t<double> x1y1_,
py::array_t<double> x2y2_,
Expand Down Expand Up @@ -89,7 +90,8 @@ py::tuple findRigidTransformation(
double conf,
int min_iters,
int max_iters,
int partition_num)
int partition_num,
int seed)
{
py::buffer_info buf1 = correspondences_.request();
size_t NUM_TENTS = buf1.shape[0];
Expand Down Expand Up @@ -117,6 +119,7 @@ py::tuple findRigidTransformation(
probabilities.assign(ptr_prob, ptr_prob + buf_prob.size);
}

applyMagsacSeed(seed);
int num_inl = findRigidTransformation_(
correspondences,
inliers,
Expand Down Expand Up @@ -159,7 +162,8 @@ py::tuple findFundamentalMatrix(
double conf,
int min_iters,
int max_iters,
int partition_num)
int partition_num,
int seed)
{
py::buffer_info buf1 = correspondences_.request();
size_t NUM_TENTS = buf1.shape[0];
Expand Down Expand Up @@ -187,6 +191,7 @@ py::tuple findFundamentalMatrix(
probabilities.assign(ptr_prob, ptr_prob + buf_prob.size);
}

applyMagsacSeed(seed);
int num_inl = findFundamentalMatrix_(
correspondences,
inliers,
Expand Down Expand Up @@ -235,7 +240,8 @@ py::tuple findEssentialMatrix(
double conf,
int min_iters,
int max_iters,
int partition_num)
int partition_num,
int seed)
{
py::buffer_info buf1 = correspondences_.request();
size_t NUM_TENTS = buf1.shape[0];
Expand Down Expand Up @@ -285,6 +291,7 @@ py::tuple findEssentialMatrix(
probabilities.assign(ptr_prob, ptr_prob + buf_prob.size);
}

applyMagsacSeed(seed);
int num_inl = findEssentialMatrix_(
correspondences,
inliers,
Expand Down Expand Up @@ -333,7 +340,8 @@ py::tuple findLine2D(
double conf,
int min_iters,
int max_iters,
int partition_num)
int partition_num,
int seed)
{
py::buffer_info buf1 = points_.request();
size_t NUM_TENTS = buf1.shape[0];
Expand All @@ -359,6 +367,7 @@ py::tuple findLine2D(
probabilities.assign(ptr_prob, ptr_prob + buf_prob.size);
}

applyMagsacSeed(seed);
int num_inl = findLine2D_(
points,
inliers,
Expand Down Expand Up @@ -405,7 +414,8 @@ py::tuple findHomography(
double conf,
int min_iters,
int max_iters,
int partition_num)
int partition_num,
int seed)
{
py::buffer_info buf1 = correspondences_.request();
size_t NUM_TENTS = buf1.shape[0];
Expand Down Expand Up @@ -433,6 +443,7 @@ py::tuple findHomography(
probabilities.assign(ptr_prob, ptr_prob + buf_prob.size);
}

applyMagsacSeed(seed);
int num_inl = findHomography_(
correspondences,
inliers,
Expand Down Expand Up @@ -506,7 +517,8 @@ PYBIND11_PLUGIN(pymagsac) {
py::arg("conf") = 0.99,
py::arg("min_iters") = 50,
py::arg("max_iters") = 1000,
py::arg("partition_num") = 5);
py::arg("partition_num") = 5,
py::arg("seed") = -1);

m.def("findFundamentalMatrix", &findFundamentalMatrix, R"doc(some doc)doc",
py::arg("correspondences"),
Expand All @@ -521,7 +533,8 @@ PYBIND11_PLUGIN(pymagsac) {
py::arg("conf") = 0.99,
py::arg("min_iters") = 50,
py::arg("max_iters") = 1000,
py::arg("partition_num") = 5);
py::arg("partition_num") = 5,
py::arg("seed") = -1);

m.def("findRigidTransformation", &findRigidTransformation, R"doc(some doc)doc",
py::arg("correspondences"),
Expand All @@ -532,7 +545,8 @@ PYBIND11_PLUGIN(pymagsac) {
py::arg("conf") = 0.99,
py::arg("min_iters") = 50,
py::arg("max_iters") = 1000,
py::arg("partition_num") = 5);
py::arg("partition_num") = 5,
py::arg("seed") = -1);

m.def("findHomography", &findHomography, R"doc(some doc)doc",
py::arg("correspondences"),
Expand All @@ -547,7 +561,8 @@ PYBIND11_PLUGIN(pymagsac) {
py::arg("conf") = 0.99,
py::arg("min_iters") = 50,
py::arg("max_iters") = 1000,
py::arg("partition_num") = 5);
py::arg("partition_num") = 5,
py::arg("seed") = -1);

m.def("findLine2D", &findLine2D, R"doc(some doc)doc",
py::arg("points"),
Expand All @@ -560,7 +575,8 @@ PYBIND11_PLUGIN(pymagsac) {
py::arg("conf") = 0.99,
py::arg("min_iters") = 50,
py::arg("max_iters") = 1000,
py::arg("partition_num") = 5);
py::arg("partition_num") = 5,
py::arg("seed") = -1);


return m.ptr();
Expand Down
17 changes: 17 additions & 0 deletions src/pymagsac/src/magsac_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@

#include <gflags/gflags.h>

#include <cstdlib>
#include <cstdint>

// Make RANSAC sampling reproducible when seed >= 0, else restore nondeterministic
// behavior. setGlobalSeed covers all UniformRandomGenerator-based samplers;
// std::srand covers samplers/solvers that use std::rand()/Eigen::Random.
void applyMagsacSeed(int seed)
{
if (seed >= 0)
{
gcransac::utils::UniformRandomGenerator<size_t>::setGlobalSeed(static_cast<std::uint64_t>(seed));
std::srand(static_cast<unsigned>(seed));
}
else
gcransac::utils::UniformRandomGenerator<size_t>::clearGlobalSeed();
}

int findRigidTransformation_(
std::vector<double>& correspondences,
std::vector<bool>& inliers,
Expand Down