29 #ifndef mrpt_vision_descriptor_pairing_H
30 #define mrpt_vision_descriptor_pairing_H
71 template <
class DESCRIPTOR_KDTREE>
73 std::vector<vector_size_t> * pairings_1_to_multi_2,
74 std::vector<std::pair<size_t,size_t> > * pairings_1_to_2,
76 const DESCRIPTOR_KDTREE & feats_img2_kdtree,
78 const size_t max_neighbors = 4,
79 const double max_relative_distance = 1.2,
80 const typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType max_distance = std::numeric_limits<typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType>::max()
85 ASSERT_(pairings_1_to_multi_2!=NULL || pairings_1_to_2!=NULL)
87 typedef typename DESCRIPTOR_KDTREE::kdtree_t::ElementType KDTreeElementType;
88 typedef typename DESCRIPTOR_KDTREE::kdtree_t::DistanceType KDTreeDistanceType;
90 const size_t N=feats_img1.
size();
91 if (pairings_1_to_multi_2) pairings_1_to_multi_2->assign(N,
vector_size_t());
92 if (pairings_1_to_2) { pairings_1_to_2->clear(); pairings_1_to_2->reserve(N); }
94 size_t overall_pairs = 0;
96 if (!N)
return overall_pairs;
99 ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSIFT(),
"Request to match SIFT features but feats_img1 has no SIFT descriptors!")
100 ASSERTMSG_(
sizeof(KDTreeElementType)==
sizeof(feats_img1[0]->descriptors.SIFT[0]),
"Incorrect data type kd_tree::ElementType for SIFT (should be uint8_t)")
103 ASSERTMSG_(feats_img1[0]->descriptors.hasDescriptorSURF(),
"Request to match SURF features but feats_img1 has no SURF descriptors!")
104 ASSERTMSG_(
sizeof(KDTreeElementType)==
sizeof(feats_img1[0]->descriptors.SURF[0]),
"Incorrect data type kd_tree::ElementType for SURF (should be float)")
106 else {
THROW_EXCEPTION(
"This function only supports SIFT or SURFT descriptors") }
108 std::vector<size_t> indices(max_neighbors);
109 std::vector<double> distances(max_neighbors);
111 for (
size_t i=0;i<N;i++)
115 const void * ptr_query;
117 else if (descriptor==
descSURF) ptr_query = &descs.
SURF[0];
119 feats_img2_kdtree.get_kdtree().knnSearch(
120 static_cast<const KDTreeElementType*>( ptr_query ),
122 &indices[0], &distances[0]
126 const KDTreeDistanceType this_thresh = std::min( max_relative_distance*distances[0], max_distance);
127 for (
size_t j=0;j<max_neighbors;j++)
129 if (distances[j]<=this_thresh) {
131 if (pairings_1_to_multi_2) (*pairings_1_to_multi_2)[i].push_back(indices[j]);
132 if (pairings_1_to_2) pairings_1_to_2->push_back(std::make_pair(i,indices[j]));
137 return overall_pairs;