MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
ransac_homography.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"
17#include "math/matrix_tools.h"
19
21
22RansacHomography::RansacHomography (Options const& options)
23 : opts(options)
24{
25}
26
27void
29{
30 if (this->opts.verbose_output)
31 {
32 std::cout << "RANSAC-H: Running for " << this->opts.max_iterations
33 << " iterations, threshold " << this->opts.threshold
34 << "..." << std::endl;
35 }
36
37 std::vector<int> inliers;
38 inliers.reserve(matches.size());
39 for (int iteration = 0; iteration < this->opts.max_iterations; ++iteration)
40 {
41 HomographyMatrix homography;
42 this->compute_homography(matches, &homography);
43 this->evaluate_homography(matches, homography, &inliers);
44 if (inliers.size() > result->inliers.size())
45 {
46 if (this->opts.verbose_output)
47 {
48 std::cout << "RANSAC-H: Iteration " << iteration
49 << ", inliers " << inliers.size() << " ("
50 << (100.0 * inliers.size() / matches.size())
51 << "%)" << std::endl;
52 }
53
54 result->homography = homography;
55 std::swap(result->inliers, inliers);
56 inliers.reserve(matches.size());
57 }
58 }
59}
60
61void
62RansacHomography::compute_homography (Correspondences2D2D const& matches,
63 HomographyMatrix* homography)
64{
65 if (matches.size() < 4)
66 throw std::invalid_argument("At least 4 matches required");
67
68 /*
69 * Draw 4 random numbers in the interval [0, matches.size() - 1]
70 * without duplicates. This is done by keeping a set with drawn numbers.
71 */
72 std::set<int> result;
73 while (result.size() < 4)
74 result.insert(util::system::rand_int() % matches.size());
75
76 Correspondences2D2D four_correspondeces(4);
77 std::set<int>::const_iterator iter = result.begin();
78 for (std::size_t i = 0; i < 4; ++i, ++iter)
79 four_correspondeces[i] = matches[*iter];
80
81 sfm::homography_dlt(four_correspondeces, homography);
82 *homography /= (*homography)[8];
83}
84
85void
86RansacHomography::evaluate_homography (Correspondences2D2D const& matches,
87 HomographyMatrix const& homography, std::vector<int>* inliers)
88{
89 double const square_threshold = MATH_POW2(this->opts.threshold);
90 inliers->resize(0);
91 for (std::size_t i = 0; i < matches.size(); ++i)
92 {
93 Correspondence2D2D const& match = matches[i];
94 double error = sfm::symmetric_transfer_error(homography, match);
95 if (error < square_threshold)
96 inliers->push_back(i);
97 }
98}
99
void estimate(Correspondences2D2D const &matches, Result *result)
#define MATH_POW2(x)
Definition defines.h:68
bool homography_dlt(Correspondences2D2D const &points, HomographyMatrix *result)
Direct linear transformation algorithm to compute the homography matrix from image correspondences.
Definition homography.cc:21
math::Matrix3d HomographyMatrix
Definition homography.h:19
std::vector< Correspondence2D2D > Correspondences2D2D
double symmetric_transfer_error(HomographyMatrix const &homography, Correspondence2D2D const &match)
Computes the symmetric transfer error for an image correspondence given the homography matrix between...
Definition homography.cc:66
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
double threshold
Threshold used to determine inliers.
int max_iterations
The number of RANSAC iterations.
bool verbose_output
Produce status messages on the console.
HomographyMatrix homography
The resulting homography matrix which led to the inliers.
std::vector< int > inliers
The indices of inliers in the correspondences which led to the homography matrix.