Main MRPT website > C++ reference for MRPT 1.9.9
descriptor_pairing.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef mrpt_vision_descriptor_pairing_H
11 #define mrpt_vision_descriptor_pairing_H
12 
13 #include <mrpt/vision/types.h>
14 
15 namespace mrpt
16 {
17 namespace vision
18 {
19 /** \addtogroup mrptvision_features
20  @{ */
21 
22 /** Search for pairings between two sets of visual descriptors (for now, only
23  * SURF
24  * and SIFT features are considered).
25  * Pairings are returned in one of two formats (or both of them
26  * simultaneously),
27  * depending on the non-NULL output containers present in the call.
28  *
29  * \code
30  * CFeatureList feats1, feats2;
31  * // Populate feature lists [...]
32  *
33  * // Create kd-tree for SIFT features of "feats2":
34  * TSIFTDescriptorsKDTreeIndex<double> feats2_kdtree(feats2);
35  *
36  * // Search correspondences:
37  * std::vector<vector_size_t> pairings_1_to_multi_2;
38  * std::vector<std::pair<size_t,size_t> > pairings_1_to_2;
39  * mrpt::vision::find_descriptor_pairings(
40  * &pairings_1_to_multi_2, // Can be set to nullptr if not needed
41  * &pairings_1_to_2, // Can be set to nullptr if not needed
42  * feats1, feats2_kdtree, // The two sets of features
43  * mrpt::vision::descSIFT // Select descriptor to use
44  * // [further optional params]
45  * );
46  * \endcode
47  *
48  * \sa TSIFTDescriptorsKDTreeIndex, TSURFDescriptorsKDTreeIndex
49  */
50 template <class DESCRIPTOR_KDTREE>
52  std::vector<vector_size_t>* pairings_1_to_multi_2,
53  std::vector<std::pair<size_t, size_t>>* pairings_1_to_2,
54  const CFeatureList& feats_img1, const DESCRIPTOR_KDTREE& feats_img2_kdtree,
55  const mrpt::vision::TDescriptorType descriptor = descSIFT,
56  const size_t max_neighbors = 4, const double max_relative_distance = 1.2,
57  const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance =
58  std::numeric_limits<
59  typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType>::max())
60 {
62  ASSERT_ABOVEEQ_(max_neighbors, 1)
63  ASSERT_(pairings_1_to_multi_2 != nullptr || pairings_1_to_2 != nullptr)
64 
65  typedef typename DESCRIPTOR_KDTREE::kdtree_t::ElementType
66  KDTreeElementType; // The expected data type of elements for the
67  // kd-tree
68  typedef
69  typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType KDTreeDistanceType;
70 
71  const size_t N = feats_img1.size();
72  if (pairings_1_to_multi_2)
73  pairings_1_to_multi_2->assign(
74  N, vector_size_t()); // Reset output container
75  if (pairings_1_to_2)
76  {
77  pairings_1_to_2->clear();
78  pairings_1_to_2->reserve(N);
79  }
80 
81  size_t overall_pairs = 0;
82 
83  if (!N) return overall_pairs; // No features -> nothing to do
84 
85  if (descriptor == descSIFT)
86  {
87  ASSERTMSG_(
88  feats_img1[0]->descriptors.hasDescriptorSIFT(),
89  "Request to match SIFT features but feats_img1 has no SIFT "
90  "descriptors!")
91  ASSERTMSG_(
92  sizeof(KDTreeElementType) ==
93  sizeof(feats_img1[0]->descriptors.SIFT[0]),
94  "Incorrect data type kd_tree::ElementType for SIFT (should be "
95  "uint8_t)")
96  }
97  else if (descriptor == descSURF)
98  {
99  ASSERTMSG_(
100  feats_img1[0]->descriptors.hasDescriptorSURF(),
101  "Request to match SURF features but feats_img1 has no SURF "
102  "descriptors!")
103  ASSERTMSG_(
104  sizeof(KDTreeElementType) ==
105  sizeof(feats_img1[0]->descriptors.SURF[0]),
106  "Incorrect data type kd_tree::ElementType for SURF (should be "
107  "float)")
108  }
109  else
110  {
111  THROW_EXCEPTION("This function only supports SIFT or SURFT descriptors")
112  }
113 
114  std::vector<size_t> indices(max_neighbors);
115  std::vector<double> distances(max_neighbors);
116 
117  for (size_t i = 0; i < N; i++)
118  {
119  const CFeature::TDescriptors& descs = feats_img1[i]->descriptors;
120 
121  const void* ptr_query;
122  if (descriptor == descSIFT)
123  ptr_query = &descs.SIFT[0];
124  else if (descriptor == descSURF)
125  ptr_query = &descs.SURF[0];
126 
127  feats_img2_kdtree.get_kdtree().knnSearch(
128  static_cast<const KDTreeElementType*>(ptr_query), // Query point
129  max_neighbors, // Number of neigbors
130  &indices[0], &distances[0] // Output
131  );
132 
133  // Include all correspondences below the absolute and the relative
134  // threshold (indices comes ordered by distances):
135  const KDTreeDistanceType this_thresh =
136  std::min(max_relative_distance * distances[0], max_distance);
137  for (size_t j = 0; j < max_neighbors; j++)
138  {
139  if (distances[j] <= this_thresh)
140  {
141  overall_pairs++;
142  if (pairings_1_to_multi_2)
143  (*pairings_1_to_multi_2)[i].push_back(indices[j]);
144  if (pairings_1_to_2)
145  pairings_1_to_2->push_back(std::make_pair(i, indices[j]));
146  }
147  else
148  break;
149  }
150  }
151  return overall_pairs;
152  MRPT_END
153 }
154 
155 /** @} */
156 }
157 }
158 #endif
#define min(a, b)
size_t size() const
Definition: CFeature.h:387
#define THROW_EXCEPTION(msg)
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: glext.h:3528
std::vector< float > SURF
SURF feature descriptor.
Definition: CFeature.h:113
#define MRPT_END
std::vector< uint8_t > SIFT
SIFT feature descriptor.
Definition: CFeature.h:112
All the possible descriptors this feature may have.
Definition: CFeature.h:108
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:305
TDescriptorType
The bitwise OR combination of values of TDescriptorType are used in CFeatureExtraction::computeDescri...
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_ABOVEEQ_(__A, __B)
size_t find_descriptor_pairings(std::vector< vector_size_t > *pairings_1_to_multi_2, std::vector< std::pair< size_t, size_t >> *pairings_1_to_2, const CFeatureList &feats_img1, const DESCRIPTOR_KDTREE &feats_img2_kdtree, const mrpt::vision::TDescriptorType descriptor=descSIFT, const size_t max_neighbors=4, const double max_relative_distance=1.2, const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance=std::numeric_limits< typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType >::max())
Search for pairings between two sets of visual descriptors (for now, only SURF and SIFT features are ...
#define ASSERT_(f)
std::vector< size_t > vector_size_t
Definition: types_simple.h:26
#define ASSERTMSG_(f, __ERROR_MSG)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019