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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020