cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
descriptors.hpp
Go to the documentation of this file.
1#pragma once
2
42
43namespace toolbox::pcl
44{
45
97template<typename Signature>
98std::vector<std::pair<size_t, size_t>> match_descriptors(
99 const std::vector<Signature>& source_descriptors,
100 const std::vector<Signature>& target_descriptors,
101 typename Signature::data_type max_distance)
102{
103 std::vector<std::pair<size_t, size_t>> matches;
104
105 for (size_t i = 0; i < source_descriptors.size(); ++i) {
106 typename Signature::data_type min_dist = std::numeric_limits<typename Signature::data_type>::max();
107 size_t best_match = 0;
108
109 for (size_t j = 0; j < target_descriptors.size(); ++j) {
110 auto dist = source_descriptors[i].distance(target_descriptors[j]);
111 if (dist < min_dist) {
112 min_dist = dist;
113 best_match = j;
114 }
115 }
116
117 if (min_dist < max_distance) {
118 matches.emplace_back(i, best_match);
119 }
120 }
121
122 return matches;
123}
124
138template<typename Signature>
139std::vector<std::pair<size_t, size_t>> match_descriptors_ratio_test(
140 const std::vector<Signature>& source_descriptors,
141 const std::vector<Signature>& target_descriptors,
142 float ratio_threshold = 0.8f)
143{
144 std::vector<std::pair<size_t, size_t>> matches;
145
146 for (size_t i = 0; i < source_descriptors.size(); ++i) {
147 typename Signature::data_type best_dist = std::numeric_limits<typename Signature::data_type>::max();
148 typename Signature::data_type second_best_dist = std::numeric_limits<typename Signature::data_type>::max();
149 size_t best_match = 0;
150
151 // 找到最佳和次佳匹配 / Find best and second-best matches
152 for (size_t j = 0; j < target_descriptors.size(); ++j) {
153 auto dist = source_descriptors[i].distance(target_descriptors[j]);
154 if (dist < best_dist) {
155 second_best_dist = best_dist;
156 best_dist = dist;
157 best_match = j;
158 } else if (dist < second_best_dist) {
159 second_best_dist = dist;
160 }
161 }
162
163 // 应用比率测试 / Apply ratio test
164 if (best_dist < ratio_threshold * second_best_dist) {
165 matches.emplace_back(i, best_match);
166 }
167 }
168
169 return matches;
170}
171
183template<typename Signature>
184Signature compute_descriptor_centroid(const std::vector<Signature>& descriptors) {
185 if (descriptors.empty()) {
186 return Signature{};
187 }
188
189 // 这里需要根据具体的Signature类型实现 / Implementation depends on specific Signature type
190 // 示例实现适用于具有histogram成员的签名 / Example implementation for signatures with histogram member
191 Signature centroid = descriptors[0];
192
193 // 具体实现依赖于描述子类型 / Specific implementation depends on descriptor type
194 // 通常需要对所有维度求平均 / Usually need to average all dimensions
195
196 return centroid;
197}
198
215template<typename Signature>
216std::pair<float, float> evaluate_descriptor_matching(
217 const std::vector<Signature>& descriptors1,
218 const std::vector<Signature>& descriptors2,
219 const std::vector<std::pair<size_t, size_t>>& ground_truth_matches,
220 typename Signature::data_type max_distance)
221{
222 auto predicted_matches = match_descriptors(descriptors1, descriptors2, max_distance);
223
224 // 转换为set以便快速查找 / Convert to set for fast lookup
225 std::set<std::pair<size_t, size_t>> gt_set(
226 ground_truth_matches.begin(), ground_truth_matches.end());
227
228 int true_positives = 0;
229 for (const auto& match : predicted_matches) {
230 if (gt_set.count(match) > 0) {
231 true_positives++;
232 }
233 }
234
235 float precision = predicted_matches.empty() ? 0.0f :
236 static_cast<float>(true_positives) / predicted_matches.size();
237 float recall = ground_truth_matches.empty() ? 0.0f :
238 static_cast<float>(true_positives) / ground_truth_matches.size();
239
240 return {precision, recall};
241}
242
// end of descriptors group
244
245} // namespace toolbox::pcl
std::vector< std::pair< size_t, size_t > > match_descriptors(const std::vector< Signature > &source_descriptors, const std::vector< Signature > &target_descriptors, typename Signature::data_type max_distance)
描述子类型及其特点 / Descriptor types and their characteristics
Definition descriptors.hpp:98
Signature compute_descriptor_centroid(const std::vector< Signature > &descriptors)
计算描述子集的统计信息 / Compute statistics of descriptor set
Definition descriptors.hpp:184
std::vector< std::pair< size_t, size_t > > match_descriptors_ratio_test(const std::vector< Signature > &source_descriptors, const std::vector< Signature > &target_descriptors, float ratio_threshold=0.8f)
使用比率测试的描述子匹配 / Descriptor matching with ratio test
Definition descriptors.hpp:139
std::pair< float, float > evaluate_descriptor_matching(const std::vector< Signature > &descriptors1, const std::vector< Signature > &descriptors2, const std::vector< std::pair< size_t, size_t > > &ground_truth_matches, typename Signature::data_type max_distance)
描述子性能评估辅助函数 / Descriptor performance evaluation helper
Definition descriptors.hpp:216
Definition base_correspondence_generator.hpp:18