Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
15namespace 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 SURF
23 * and SIFT features are considered).
24 * Pairings are returned in one of two formats (or both of them 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<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 NULL if not needed
39 * &pairings_1_to_2, // Can be set to NULL 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<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,
53 const DESCRIPTOR_KDTREE & feats_img2_kdtree,
54 const mrpt::vision::TDescriptorType descriptor = descSIFT,
55 const size_t max_neighbors = 4,
56 const double max_relative_distance = 1.2,
57 const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance = std::numeric_limits<typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType>::max()
58 )
59 {
61 ASSERT_ABOVEEQ_(max_neighbors,1)
62 ASSERT_(pairings_1_to_multi_2!=NULL || pairings_1_to_2!=NULL)
63
64 typedef typename DESCRIPTOR_KDTREE::kdtree_t::ElementType KDTreeElementType; // The expected data type of elements for the kd-tree
65 typedef typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType KDTreeDistanceType;
66
67 const size_t N=feats_img1.size();
68 if (pairings_1_to_multi_2) pairings_1_to_multi_2->assign(N, vector_size_t()); // Reset output container
69 if (pairings_1_to_2) { pairings_1_to_2->clear(); pairings_1_to_2->reserve(N); }
70
71 size_t overall_pairs = 0;
72
73 if (!N) return overall_pairs; // No features -> nothing to do
74
75 if (descriptor==descSIFT) {
76 ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSIFT(), "Request to match SIFT features but feats_img1 has no SIFT descriptors!")
77 ASSERTMSG_(sizeof(KDTreeElementType)==sizeof(feats_img1[0]->descriptors.SIFT[0]),"Incorrect data type kd_tree::ElementType for SIFT (should be uint8_t)")
78 }
79 else if (descriptor==descSURF) {
80 ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSURF(), "Request to match SURF features but feats_img1 has no SURF descriptors!")
81 ASSERTMSG_(sizeof(KDTreeElementType)==sizeof(feats_img1[0]->descriptors.SURF[0]),"Incorrect data type kd_tree::ElementType for SURF (should be float)")
82 }
83 else { THROW_EXCEPTION("This function only supports SIFT or SURFT descriptors") }
84
85 std::vector<size_t> indices(max_neighbors);
86 std::vector<double> distances(max_neighbors);
87
88 for (size_t i=0;i<N;i++)
89 {
90 const CFeature::TDescriptors &descs = feats_img1[i]->descriptors;
91
92 const void * ptr_query;
93 if (descriptor==descSIFT) ptr_query = &descs.SIFT[0];
94 else if (descriptor==descSURF) ptr_query = &descs.SURF[0];
95
96 feats_img2_kdtree.get_kdtree().knnSearch(
97 static_cast<const KDTreeElementType*>( ptr_query ), // Query point
98 max_neighbors, // Number of neigbors
99 &indices[0], &distances[0] // Output
100 );
101
102 // Include all correspondences below the absolute and the relative threshold (indices comes ordered by distances):
103 const KDTreeDistanceType this_thresh = std::min( max_relative_distance*distances[0], max_distance);
104 for (size_t j=0;j<max_neighbors;j++)
105 {
106 if (distances[j]<=this_thresh) {
107 overall_pairs++;
108 if (pairings_1_to_multi_2) (*pairings_1_to_multi_2)[i].push_back(indices[j]);
109 if (pairings_1_to_2) pairings_1_to_2->push_back(std::make_pair(i,indices[j]));
110 }
111 else break;
112 }
113 }
114 return overall_pairs;
116 }
117
118 /** @} */
119
120 }
121}
122#endif
123
A list of visual features, to be used as output by detectors, as input/output by trackers,...
Definition: CFeature.h:212
size_t size() const
Definition: CFeature.h:280
std::vector< size_t > vector_size_t
Definition: types_simple.h:25
TDescriptorType
The bitwise OR combination of values of TDescriptorType are used in CFeatureExtraction::computeDescri...
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 MRPT_START
Definition: mrpt_macros.h:349
#define ASSERT_(f)
Definition: mrpt_macros.h:261
#define MRPT_END
Definition: mrpt_macros.h:353
#define THROW_EXCEPTION(msg)
Definition: mrpt_macros.h:110
#define ASSERTMSG_(f, __ERROR_MSG)
Definition: mrpt_macros.h:260
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: mrpt_macros.h:269
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
All the possible descriptors this feature may have.
Definition: CFeature.h:85
std::vector< float > SURF
SURF feature descriptor.
Definition: CFeature.h:89
std::vector< uint8_t > SIFT
SIFT feature descriptor.
Definition: CFeature.h:88



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Tue Dec 27 00:53:09 UTC 2022