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