Main MRPT website > C++ reference
MRPT logo
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-2014, 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  using mrpt::math::TPose3D;
22 
23  /** \addtogroup mrptvision_features
24  @{ */
25 
26  /** Search for pairings between two sets of visual descriptors (for now, only SURF
27  * and SIFT features are considered).
28  * Pairings are returned in one of two formats (or both of them simultaneously),
29  * depending on the non-NULL output containers present in the call.
30  *
31  * \code
32  * CFeatureList feats1, feats2;
33  * // Populate feature lists [...]
34  *
35  * // Create kd-tree for SIFT features of "feats2":
36  * TSIFTDescriptorsKDTreeIndex<double> feats2_kdtree(feats2);
37  *
38  * // Search correspondences:
39  * std::vector<vector_size_t> pairings_1_to_multi_2;
40  * std::vector<std::pair<size_t,size_t> > pairings_1_to_2;
41  * mrpt::vision::find_descriptor_pairings(
42  * &pairings_1_to_multi_2, // Can be set to NULL if not needed
43  * &pairings_1_to_2, // Can be set to NULL if not needed
44  * feats1, feats2_kdtree, // The two sets of features
45  * mrpt::vision::descSIFT // Select descriptor to use
46  * // [further optional params]
47  * );
48  * \endcode
49  *
50  * \sa TSIFTDescriptorsKDTreeIndex, TSURFDescriptorsKDTreeIndex
51  */
52  template <class DESCRIPTOR_KDTREE>
54  std::vector<vector_size_t> * pairings_1_to_multi_2,
55  std::vector<std::pair<size_t,size_t> > * pairings_1_to_2,
56  const CFeatureList & feats_img1,
57  const DESCRIPTOR_KDTREE & feats_img2_kdtree,
58  const mrpt::vision::TDescriptorType descriptor = descSIFT,
59  const size_t max_neighbors = 4,
60  const double max_relative_distance = 1.2,
61  const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance = std::numeric_limits<typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType>::max()
62  )
63  {
65  ASSERT_ABOVEEQ_(max_neighbors,1)
66  ASSERT_(pairings_1_to_multi_2!=NULL || pairings_1_to_2!=NULL)
67 
68  typedef typename DESCRIPTOR_KDTREE::kdtree_t::ElementType KDTreeElementType; // The expected data type of elements for the kd-tree
69  typedef typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType KDTreeDistanceType;
70 
71  const size_t N=feats_img1.size();
72  if (pairings_1_to_multi_2) pairings_1_to_multi_2->assign(N, vector_size_t()); // Reset output container
73  if (pairings_1_to_2) { pairings_1_to_2->clear(); pairings_1_to_2->reserve(N); }
74 
75  size_t overall_pairs = 0;
76 
77  if (!N) return overall_pairs; // No features -> nothing to do
78 
79  if (descriptor==descSIFT) {
80  ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSIFT(), "Request to match SIFT features but feats_img1 has no SIFT descriptors!")
81  ASSERTMSG_(sizeof(KDTreeElementType)==sizeof(feats_img1[0]->descriptors.SIFT[0]),"Incorrect data type kd_tree::ElementType for SIFT (should be uint8_t)")
82  }
83  else if (descriptor==descSURF) {
84  ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSURF(), "Request to match SURF features but feats_img1 has no SURF descriptors!")
85  ASSERTMSG_(sizeof(KDTreeElementType)==sizeof(feats_img1[0]->descriptors.SURF[0]),"Incorrect data type kd_tree::ElementType for SURF (should be float)")
86  }
87  else { THROW_EXCEPTION("This function only supports SIFT or SURFT descriptors") }
88 
89  std::vector<size_t> indices(max_neighbors);
90  std::vector<double> distances(max_neighbors);
91 
92  for (size_t i=0;i<N;i++)
93  {
94  const CFeature::TDescriptors &descs = feats_img1[i]->descriptors;
95 
96  const void * ptr_query;
97  if (descriptor==descSIFT) ptr_query = &descs.SIFT[0];
98  else if (descriptor==descSURF) ptr_query = &descs.SURF[0];
99 
100  feats_img2_kdtree.get_kdtree().knnSearch(
101  static_cast<const KDTreeElementType*>( ptr_query ), // Query point
102  max_neighbors, // Number of neigbors
103  &indices[0], &distances[0] // Output
104  );
105 
106  // Include all correspondences below the absolute and the relative threshold (indices comes ordered by distances):
107  const KDTreeDistanceType this_thresh = std::min( max_relative_distance*distances[0], max_distance);
108  for (size_t j=0;j<max_neighbors;j++)
109  {
110  if (distances[j]<=this_thresh) {
111  overall_pairs++;
112  if (pairings_1_to_multi_2) (*pairings_1_to_multi_2)[i].push_back(indices[j]);
113  if (pairings_1_to_2) pairings_1_to_2->push_back(std::make_pair(i,indices[j]));
114  }
115  else break;
116  }
117  }
118  return overall_pairs;
119  MRPT_END
120  }
121 
122  /** @} */
123 
124  }
125 }
126 #endif
127 
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 ...
size_t size() const
Definition: CFeature.h:284
#define THROW_EXCEPTION(msg)
std::vector< float > SURF
SURF feature descriptor.
Definition: CFeature.h:93
std::vector< size_t > vector_size_t
Definition: types_simple.h:22
#define MRPT_END
std::vector< uint8_t > SIFT
SIFT feature descriptor.
Definition: CFeature.h:92
All the possible descriptors this feature may have.
Definition: CFeature.h:88
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:215
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)
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
#define ASSERT_(f)
Lightweight 3D point.
#define ASSERTMSG_(f, __ERROR_MSG)
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:31



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014