MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
ransac_fundamental.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <algorithm>
11#include <iostream>
12#include <set>
13#include <stdexcept>
14
15#include "util/system.h"
16#include "math/algo.h"
18
20
21RansacFundamental::RansacFundamental (Options const& options)
22 : opts(options)
23{
24}
25
26void
28{
29 if (this->opts.verbose_output)
30 {
31 std::cout << "RANSAC-F: Running for " << this->opts.max_iterations
32 << " iterations, threshold " << this->opts.threshold
33 << "..." << std::endl;
34 }
35
36 std::vector<int> inliers;
37 inliers.reserve(matches.size());
38 for (int iteration = 0; iteration < this->opts.max_iterations; ++iteration)
39 {
40 FundamentalMatrix fundamental;
41 this->estimate_8_point(matches, &fundamental);
42 this->find_inliers(matches, fundamental, &inliers);
43 if (inliers.size() > result->inliers.size())
44 {
45 if (this->opts.verbose_output)
46 {
47 std::cout << "RANSAC-F: Iteration " << iteration
48 << ", inliers " << inliers.size() << " ("
49 << (100.0 * inliers.size() / matches.size())
50 << "%)" << std::endl;
51 }
52
53 result->fundamental = fundamental;
54 std::swap(result->inliers, inliers);
55 inliers.reserve(matches.size());
56 }
57 }
58}
59
60void
61RansacFundamental::estimate_8_point (Correspondences2D2D const& matches,
62 FundamentalMatrix* fundamental)
63{
64 if (matches.size() < 8)
65 throw std::invalid_argument("At least 8 matches required");
66
67 /*
68 * Draw 8 random numbers in the interval [0, matches.size() - 1]
69 * without duplicates. This is done by keeping a set with drawn numbers.
70 */
71 std::set<int> result;
72 while (result.size() < 8)
73 result.insert(util::system::rand_int() % matches.size());
74
75 math::Matrix<double, 3, 8> pset1, pset2;
76 std::set<int>::const_iterator iter = result.begin();
77 for (int i = 0; i < 8; ++i, ++iter)
78 {
79 Correspondence2D2D const& match = matches[*iter];
80 pset1(0, i) = match.p1[0];
81 pset1(1, i) = match.p1[1];
82 pset1(2, i) = 1.0;
83 pset2(0, i) = match.p2[0];
84 pset2(1, i) = match.p2[1];
85 pset2(2, i) = 1.0;
86 }
87
88 /* Compute fundamental matrix using normalized 8-point. */
89 sfm::fundamental_8_point(pset1, pset2, fundamental);
91}
92
93void
94RansacFundamental::find_inliers (Correspondences2D2D const& matches,
95 FundamentalMatrix const& fundamental, std::vector<int>* result)
96{
97 result->resize(0);
98 double const squared_thres = this->opts.threshold * this->opts.threshold;
99 for (std::size_t i = 0; i < matches.size(); ++i)
100 {
101 double error = sampson_distance(fundamental, matches[i]);
102 if (error < squared_thres)
103 result->push_back(i);
104 }
105}
106
T * begin(void)
Definition matrix.h:506
void estimate(Correspondences2D2D const &matches, Result *result)
void enforce_fundamental_constraints(FundamentalMatrix *matrix)
Constraints the given matrix to have TWO NON-ZERO eigenvalues.
math::Matrix< double, 3, 3 > FundamentalMatrix
Definition fundamental.h:60
std::vector< Correspondence2D2D > Correspondences2D2D
bool fundamental_8_point(Eight2DPoints const &points_view_1, Eight2DPoints const &points_view_2, FundamentalMatrix *result)
Algorithm to compute the fundamental or essential matrix from 8 image correspondences.
double sampson_distance(FundamentalMatrix const &F, Correspondence2D2D const &m)
Computes the Sampson distance for an image correspondence given the fundamental matrix between two vi...
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478
int rand_int(void)
Returns a random number in [0, 2^31].
Definition system.h:126
#define SFM_NAMESPACE_END
Definition defines.h:14
#define SFM_NAMESPACE_BEGIN
Definition defines.h:13
Two image coordinates which correspond to each other in terms of observing the same point in the scen...
bool verbose_output
Produce status messages on the console.
double threshold
Threshold used to determine inliers.
int max_iterations
The number of RANSAC iterations.
FundamentalMatrix fundamental
The resulting fundamental matrix which led to the inliers.
std::vector< int > inliers
The indices of inliers in the correspondences which led to the homography matrix.