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-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
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<std::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<std::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  /** The expected data type of elements for the kd-tree*/
66  using KDTreeElementType = typename DESCRIPTOR_KDTREE::kdtree_t::ElementType;
67  using KDTreeDistanceType =
68  typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType;
69 
70  const size_t N = feats_img1.size();
71  if (pairings_1_to_multi_2)
72  pairings_1_to_multi_2->assign(
73  N, std::vector<size_t>()); // Reset output container
74  if (pairings_1_to_2)
75  {
76  pairings_1_to_2->clear();
77  pairings_1_to_2->reserve(N);
78  }
79 
80  size_t overall_pairs = 0;
81 
82  if (!N) return overall_pairs; // No features -> nothing to do
83 
84  if (descriptor == descSIFT)
85  {
86  ASSERTMSG_(
87  feats_img1[0]->descriptors.hasDescriptorSIFT(),
88  "Request to match SIFT features but feats_img1 has no SIFT "
89  "descriptors!");
90  ASSERTMSG_(
91  sizeof(KDTreeElementType) ==
92  sizeof(feats_img1[0]->descriptors.SIFT[0]),
93  "Incorrect data type kd_tree::ElementType for SIFT (should be "
94  "uint8_t)");
95  }
96  else if (descriptor == descSURF)
97  {
98  ASSERTMSG_(
99  feats_img1[0]->descriptors.hasDescriptorSURF(),
100  "Request to match SURF features but feats_img1 has no SURF "
101  "descriptors!");
102  ASSERTMSG_(
103  sizeof(KDTreeElementType) ==
104  sizeof(feats_img1[0]->descriptors.SURF[0]),
105  "Incorrect data type kd_tree::ElementType for SURF (should be "
106  "float)");
107  }
108  else
109  {
111  "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 } // namespace vision
157 } // namespace mrpt
158 #endif
mrpt::vision::CFeatureList::size
size_t size() const
Definition: CFeature.h:388
mrpt::vision::TDescriptorType
TDescriptorType
The bitwise OR combination of values of TDescriptorType are used in CFeatureExtraction::computeDescri...
Definition: vision/include/mrpt/vision/types.h:95
mrpt::vision::CFeature::TDescriptors::SURF
std::vector< float > SURF
SURF feature descriptor.
Definition: CFeature.h:114
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::vision::CFeatureList
A list of visual features, to be used as output by detectors, as input/output by trackers,...
Definition: CFeature.h:304
mrpt::vision::CFeature::TDescriptors::SIFT
std::vector< uint8_t > SIFT
SIFT feature descriptor.
Definition: CFeature.h:113
mrpt::vision::descSIFT
@ descSIFT
SIFT descriptors.
Definition: vision/include/mrpt/vision/types.h:100
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::vision::descSURF
@ descSURF
SURF descriptors.
Definition: vision/include/mrpt/vision/types.h:102
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
ASSERT_ABOVEEQ_
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:183
min
#define min(a, b)
Definition: rplidar_driver.cpp:42
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::vision::find_descriptor_pairings
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 ...
Definition: descriptor_pairing.h:51
types.h
mrpt::vision::CFeature::TDescriptors
All the possible descriptors this feature may have.
Definition: CFeature.h:109
indices
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: glext.h:3529



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST