Berkeley SfM
Public Member Functions | Private Member Functions | List of all members
bsfm::NaiveMatcher2D2D Class Reference

#include <naive_matcher_2d2d.h>

Inheritance diagram for bsfm::NaiveMatcher2D2D:
bsfm::FeatureMatcher

Public Member Functions

 NaiveMatcher2D2D ()
 
virtual ~NaiveMatcher2D2D ()
 
- Public Member Functions inherited from bsfm::FeatureMatcher
 FeatureMatcher ()
 
virtual ~FeatureMatcher ()
 
virtual void AddImageFeatures (const std::vector< Feature > &image_features, const std::vector< Descriptor > &image_descriptors)
 
virtual void AddImageFeatures (const std::vector< std::vector< Feature > > &image_features, const std::vector< std::vector< Descriptor > > &image_descriptors)
 
virtual bool MatchImages (const FeatureMatcherOptions &options, PairwiseImageMatchList &image_matches)
 

Private Member Functions

virtual bool MatchImagePair (int image_index1, int image_index2, PairwiseImageMatch &feature_matches)
 
void ComputePutativeMatches (const std::vector< Descriptor > &descriptors1, const std::vector< Descriptor > &descriptors2, std::vector< LightFeatureMatch > &putative_matches)
 

Additional Inherited Members

- Protected Member Functions inherited from bsfm::FeatureMatcher
virtual void SymmetricMatches (const std::vector< LightFeatureMatch > &feature_matches_lhs, std::vector< LightFeatureMatch > &feature_matches_rhs)
 
- Protected Attributes inherited from bsfm::FeatureMatcher
std::vector< std::vector< Feature > > image_features_
 
std::vector< std::vector< Descriptor > > image_descriptors_
 
FeatureMatcherOptions options_
 

Detailed Description

Definition at line 65 of file naive_matcher_2d2d.h.

Constructor & Destructor Documentation

bsfm::NaiveMatcher2D2D::NaiveMatcher2D2D ( )
inline

Definition at line 67 of file naive_matcher_2d2d.h.

67 { }
virtual bsfm::NaiveMatcher2D2D::~NaiveMatcher2D2D ( )
inlinevirtual

Definition at line 68 of file naive_matcher_2d2d.h.

68 { }

Member Function Documentation

void bsfm::NaiveMatcher2D2D::ComputePutativeMatches ( const std::vector< Descriptor > &  descriptors1,
const std::vector< Descriptor > &  descriptors2,
std::vector< LightFeatureMatch > &  putative_matches 
)
private

Definition at line 114 of file naive_matcher_2d2d.cpp.

117  {
118  putative_matches.clear();
119 
120  // Get the singletone distance metric for descriptor comparison.
121  DistanceMetric& distance = DistanceMetric::Instance();
122 
123  // Set the maximum tolerable distance between descriptors, if applicable.
125  distance.SetMaximumDistance(options_.maximum_descriptor_distance);
126  }
127 
128  // Store all matches and their distances.
129  for (size_t ii = 0; ii < descriptors1.size(); ++ii) {
130  LightFeatureMatchList one_way_matches;
131  for (size_t jj = 0; jj < descriptors2.size(); ++jj) {
132  double dist = distance(descriptors1[ii], descriptors2[jj]);
133 
134  // If max distance was not set above, distance.Max() will be infinity and
135  // this will always be true.
136  if (dist < distance.Max()) {
137  one_way_matches.emplace_back(ii, jj, dist);
138  }
139  }
140 
141  // Store the best match for this element of features2.
142  if (this->options_.use_lowes_ratio) {
143  // Sort by distance. We only care about the distances between the best 2
144  // matches for the Lowes ratio test.
145  std::partial_sort(one_way_matches.begin(),
146  one_way_matches.begin() + 1,
147  one_way_matches.end(),
149 
150  // The second best match must be within the lowes ratio of the best match.
151  double lowes_ratio_squared =
153  if (one_way_matches[0].distance_ <
154  lowes_ratio_squared * one_way_matches[1].distance_) {
155  putative_matches.emplace_back(one_way_matches[0]);
156  }
157  } else {
158  putative_matches.emplace_back(one_way_matches[0]);
159  }
160  }
161 }
std::vector< LightFeatureMatch > LightFeatureMatchList
static DistanceMetric & Instance()
static bool SortByDistance(const LightFeatureMatch &lhs, const LightFeatureMatch &rhs)
Definition: feature_match.h:93
FeatureMatcherOptions options_
bool bsfm::NaiveMatcher2D2D::MatchImagePair ( int  image_index1,
int  image_index2,
PairwiseImageMatch feature_matches 
)
privatevirtual

Implements bsfm::FeatureMatcher.

Definition at line 42 of file naive_matcher_2d2d.cpp.

44  {
45  image_match.feature_matches_.clear();
46  image_match.descriptor_indices1_.clear();
47  image_match.descriptor_indices2_.clear();
48 
49  // Get the descriptors corresponding to these two images.
50  std::vector<Descriptor>& descriptors1 =
51  this->image_descriptors_[image_index1];
52  std::vector<Descriptor>& descriptors2 =
53  this->image_descriptors_[image_index2];
54 
55  // Normalize descriptors if required by the distance metric.
58 
59  // Compute forward (and reverse, if applicable) matches.
60  LightFeatureMatchList light_feature_matches;
61  ComputePutativeMatches(descriptors1, descriptors2, light_feature_matches);
62 
63  // Check that we got enough matches here. If we didn't, reverse matches won't
64  // help us.
65  if (light_feature_matches.size() < this->options_.min_num_feature_matches) {
66  return false;
67  }
68 
70  LightFeatureMatchList reverse_light_feature_matches;
71  ComputePutativeMatches(descriptors2, descriptors1,
72  reverse_light_feature_matches);
73  this->SymmetricMatches(reverse_light_feature_matches,
74  light_feature_matches);
75  }
76 
77  if (light_feature_matches.size() < this->options_.min_num_feature_matches) {
78  return false;
79  }
80 
81  // Check how many features the user wants.
82  unsigned int num_features_out = light_feature_matches.size();
83  if (this->options_.only_keep_best_matches) {
84  num_features_out =
85  std::min(num_features_out, this->options_.num_best_matches);
86 
87  // Return relevant matches in sorted order.
88  std::partial_sort(light_feature_matches.begin(),
89  light_feature_matches.begin() + num_features_out,
90  light_feature_matches.end(),
92  }
93 
94  // Convert from LightFeatureMatchList to FeatureMatchList for the output.
95  for (int ii = 0; ii < num_features_out; ++ii) {
96  const auto& match = light_feature_matches[ii];
97 
98  const Feature& matched_feature1 =
99  this->image_features_[image_index1][match.feature_index1_];
100  const Feature& matched_feature2 =
101  this->image_features_[image_index2][match.feature_index2_];
102 
103  image_match.feature_matches_.emplace_back(matched_feature1,
104  matched_feature2);
105 
106  // Also store the index of each descriptor used for this match.
107  image_match.descriptor_indices1_.push_back(match.feature_index1_);
108  image_match.descriptor_indices2_.push_back(match.feature_index2_);
109  }
110 
111  return true;
112 }
virtual void SymmetricMatches(const std::vector< LightFeatureMatch > &feature_matches_lhs, std::vector< LightFeatureMatch > &feature_matches_rhs)
std::vector< std::vector< Feature > > image_features_
std::vector< LightFeatureMatch > LightFeatureMatchList
static DistanceMetric & Instance()
static bool SortByDistance(const LightFeatureMatch &lhs, const LightFeatureMatch &rhs)
Definition: feature_match.h:93
void ComputePutativeMatches(const std::vector< Descriptor > &descriptors1, const std::vector< Descriptor > &descriptors2, std::vector< LightFeatureMatch > &putative_matches)
std::vector< std::vector< Descriptor > > image_descriptors_
bool MaybeNormalizeDescriptors(std::vector< Descriptor > &descriptors) const
FeatureMatcherOptions options_

The documentation for this class was generated from the following files: