10 #ifndef mrpt_vision_descriptor_pairing_H
11 #define mrpt_vision_descriptor_pairing_H
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,
56 const size_t max_neighbors = 4,
const double max_relative_distance = 1.2,
57 const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance =
59 typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType>::max())
63 ASSERT_(pairings_1_to_multi_2 !=
nullptr || pairings_1_to_2 !=
nullptr);
66 using KDTreeElementType =
typename DESCRIPTOR_KDTREE::kdtree_t::ElementType;
67 using KDTreeDistanceType =
68 typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType;
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>());
76 pairings_1_to_2->clear();
77 pairings_1_to_2->reserve(N);
80 size_t overall_pairs = 0;
82 if (!N)
return overall_pairs;
87 feats_img1[0]->descriptors.hasDescriptorSIFT(),
88 "Request to match SIFT features but feats_img1 has no SIFT "
91 sizeof(KDTreeElementType) ==
92 sizeof(feats_img1[0]->descriptors.SIFT[0]),
93 "Incorrect data type kd_tree::ElementType for SIFT (should be "
99 feats_img1[0]->descriptors.hasDescriptorSURF(),
100 "Request to match SURF features but feats_img1 has no SURF "
103 sizeof(KDTreeElementType) ==
104 sizeof(feats_img1[0]->descriptors.SURF[0]),
105 "Incorrect data type kd_tree::ElementType for SURF (should be "
111 "This function only supports SIFT or SURFT descriptors");
114 std::vector<size_t>
indices(max_neighbors);
115 std::vector<double> distances(max_neighbors);
117 for (
size_t i = 0; i < N; i++)
121 const void* ptr_query;
123 ptr_query = &descs.
SIFT[0];
125 ptr_query = &descs.
SURF[0];
127 feats_img2_kdtree.get_kdtree().knnSearch(
128 static_cast<const KDTreeElementType*
>(ptr_query),
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++)
139 if (distances[j] <= this_thresh)
142 if (pairings_1_to_multi_2)
143 (*pairings_1_to_multi_2)[i].push_back(
indices[j]);
145 pairings_1_to_2->push_back(std::make_pair(i,
indices[j]));
151 return overall_pairs;