OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DenseMatcher.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2  * OKVIS - Open Keyframe-based Visual-Inertial SLAM
3  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of Autonomous Systems Lab / ETH Zurich nor the names of
14  * its contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Created on:
30  * Author: Simon Lynen
31  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
32  *********************************************************************************/
33 
41 #ifndef INCLUDE_OKVIS_DENSEMATCHER_HPP_
42 #define INCLUDE_OKVIS_DENSEMATCHER_HPP_
43 
44 #include <algorithm>
45 #include <memory>
46 #include <mutex>
47 
49 
50 #include <okvis/assert_macros.hpp>
51 #include "ThreadPool.hpp"
52 
54 namespace okvis {
55 
59 class DenseMatcher {
60  public:
61  OKVIS_DEFINE_EXCEPTION(Exception, std::runtime_error)
62  typedef std::shared_ptr<DenseMatcher> Ptr;
71  DenseMatcher(unsigned char numMatcherThreads = 8, unsigned char numBest = 4,
72  bool useDistanceRatioThreshold = false);
73 
74  virtual ~DenseMatcher();
75 
78  template<typename MATCHING_ALGORITHM_T>
79  void match(MATCHING_ALGORITHM_T & matchingAlgorithm);
80 
86  template<typename MATCHING_ALGORITHM_T>
87  void matchInImageSpace(MATCHING_ALGORITHM_T & matchingAlgorithm);
88 
90  void matchSlow(MatchingAlgorithm & matchingAlgorithm);
91 
92  typedef float distance_t;
93 
95  struct Pairing {
98  : indexA(-1),
99  distance(std::numeric_limits<float>::max()) {
100  }
102  Pairing(int ia)
103  : indexA(ia),
104  distance(std::numeric_limits<float>::max()) {
105  }
107  Pairing(int ia, distance_t d)
108  : indexA(ia),
109  distance(d) {
110  }
111 
113  bool operator<(const Pairing & rhs) const {
114  return distance < rhs.distance;
115  }
116 
117  int indexA;
118  distance_t distance;
119  };
120 
122  typedef std::vector<pairing_t> pairing_list_t;
123 
127  struct MatchJob {
128 
129  //convenience, to make clear what is meant
131  typedef std::vector<pairing_t> pairing_list_t;
132 
134  std::vector<std::vector<pairing_t> > * vMyBest;
135 
138 
141 
143  std::mutex* mutexes;
144  };
145 
152  template<typename MATCHING_ALGORITHM_T>
153  void matchBody(
154  void (DenseMatcher::*doWorkPtr)(MatchJob&, MATCHING_ALGORITHM_T*),
155  MATCHING_ALGORITHM_T& matchingAlgorithm);
156 
163  template<typename MATCHING_ALGORITHM_T>
164  void doWorkLinearMatching(MatchJob & my_job,
165  MATCHING_ALGORITHM_T* matchingAlgorithm);
166 
177  template<typename MATCHING_ALGORITHM_T>
178  void doWorkImageSpaceMatching(MatchJob & my_job,
179  MATCHING_ALGORITHM_T* matchingAlgorithm);
180 
189  void assignbest(int myIndexScored, pairing_list_t & vPairsWithScore,
190  std::vector<std::vector<pairing_t> > & aiBestList,
191  std::mutex* locks, int startidx);
192 
202  template<typename MATCHING_ALGORITHM_T>
203  inline void listBIteration(MATCHING_ALGORITHM_T* matchingAlgorithm,
204  std::vector<pairing_t>& aiBest, size_t shortindexA,
205  size_t i);
206 
207  unsigned char numMatcherThreads_;
208  unsigned char numBest_;
210 
211  std::unique_ptr<okvis::ThreadPool> matcherThreadPool_;
212 };
213 
214 } // namespace okvis
215 
217 
218 #endif /* INCLUDE_OKVIS_DENSEMATCHER_HPP_ */
unsigned char numBest_
The set number of best pairings to save.
Definition: DenseMatcher.hpp:208
void doWorkLinearMatching(MatchJob &my_job, MATCHING_ALGORITHM_T *matchingAlgorithm)
The threading worker. This matches a keypoint with every other keypoint to find the best match...
Definition: DenseMatcher.hpp:183
A struct to save an index and distance pair.
Definition: DenseMatcher.hpp:95
std::vector< std::vector< pairing_t > > * vMyBest
The list of best matches so far.
Definition: DenseMatcher.hpp:134
Header file for the MatchingAlgorithm class.
std::unique_ptr< okvis::ThreadPool > matcherThreadPool_
The threads.
Definition: DenseMatcher.hpp:211
void match(MATCHING_ALGORITHM_T &matchingAlgorithm)
Execute a matching algorithm. This is the fast, templated version. Use this.
Definition: DenseMatcher.hpp:129
an interface for 1-1 matching between lists of things.
Definition: MatchingAlgorithm.hpp:66
void assignbest(int myIndexScored, pairing_list_t &vPairsWithScore, std::vector< std::vector< pairing_t > > &aiBestList, std::mutex *locks, int startidx)
A recursive function that reassigns weak matches, if a stronger match is found for a particular point...
Definition: DenseMatcher.cpp:69
DenseMatcher::pairing_t pairing_t
Definition: DenseMatcher.hpp:130
std::mutex * mutexes
Mutexes for read/write synchronization in assignment of best match.
Definition: DenseMatcher.hpp:143
DenseMatcher::Pairing pairing_t
Definition: DenseMatcher.hpp:121
float distance_t
Definition: DenseMatcher.hpp:92
unsigned char numMatcherThreads_
The set number of threads.
Definition: DenseMatcher.hpp:207
bool useDistanceRatioThreshold_
Use ratio of best and second best match instead of absolute threshold.
Definition: DenseMatcher.hpp:209
A data struct for the worker thread.
Definition: DenseMatcher.hpp:127
void matchInImageSpace(MATCHING_ALGORITHM_T &matchingAlgorithm)
Execute a matching algorithm implementing image space matching (i.e. match landmarks with features in...
Definition: DenseMatcher.hpp:140
#define OKVIS_DEFINE_EXCEPTION(exceptionName, exceptionParent)
Macro for defining an exception with a given parent.
Definition: assert_macros.hpp:52
This file contains some useful assert macros.
distance_t distance
Distance to paired keypoint.
Definition: DenseMatcher.hpp:118
int iThreadID
The thread ID of this job.
Definition: DenseMatcher.hpp:137
std::vector< pairing_t > pairing_list_t
Definition: DenseMatcher.hpp:122
std::vector< pairing_t > pairing_list_t
Definition: DenseMatcher.hpp:131
int indexA
Index of paired keypoint.
Definition: DenseMatcher.hpp:117
This class matches keypoints from two frames in parallel.
Definition: DenseMatcher.hpp:59
Pairing(int ia)
Constructor with maximum distance.
Definition: DenseMatcher.hpp:102
bool operator<(const Pairing &rhs) const
Compares distances.
Definition: DenseMatcher.hpp:113
DenseMatcher::pairing_list_t * vpairs
The list of pairs for this thread.
Definition: DenseMatcher.hpp:140
void doWorkImageSpaceMatching(MatchJob &my_job, MATCHING_ALGORITHM_T *matchingAlgorithm)
The threading worker. This matches a keypoint with only a subset of the other keypoints to find the b...
Definition: DenseMatcher.hpp:231
void matchBody(void(DenseMatcher::*doWorkPtr)(MatchJob &, MATCHING_ALGORITHM_T *), MATCHING_ALGORITHM_T &matchingAlgorithm)
This function creates all the matching threads and assigns the best matches afterwards.
Definition: DenseMatcher.hpp:48
Header implementation file for the DenseMatcher class.
void matchSlow(MatchingAlgorithm &matchingAlgorithm)
Execute a matching algorithm. This is the slow, runtime polymorphic version. Don't use this...
Definition: DenseMatcher.cpp:62
Pairing()
Default constructor.
Definition: DenseMatcher.hpp:97
void listBIteration(MATCHING_ALGORITHM_T *matchingAlgorithm, std::vector< pairing_t > &aiBest, size_t shortindexA, size_t i)
This calculates the distance between to keypoint descriptors. If it is better than the /e numBest_ fo...
Definition: DenseMatcher.hpp:153
std::shared_ptr< DenseMatcher > Ptr
Definition: DenseMatcher.hpp:62
Pairing(int ia, distance_t d)
Constructor.
Definition: DenseMatcher.hpp:107
Header file for the ThreadPool class.