| 69 | } |
| 70 | |
| 71 | void ExtractTopScaleFeatures(FeatureKeypoints* keypoints, |
| 72 | FeatureDescriptors* descriptors, |
| 73 | const size_t num_features) { |
| 74 | THROW_CHECK_EQ(keypoints->size(), descriptors->data.rows()); |
| 75 | THROW_CHECK_GT(num_features, 0); |
| 76 | |
| 77 | if (static_cast<size_t>(descriptors->data.rows()) <= num_features) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | std::vector<std::pair<size_t, float>> scales; |
| 82 | scales.reserve(keypoints->size()); |
| 83 | for (size_t i = 0; i < keypoints->size(); ++i) { |
| 84 | scales.emplace_back(i, (*keypoints)[i].ComputeScale()); |
| 85 | } |
| 86 | |
| 87 | std::partial_sort(scales.begin(), |
| 88 | scales.begin() + num_features, |
| 89 | scales.end(), |
| 90 | [](const std::pair<size_t, float>& scale1, |
| 91 | const std::pair<size_t, float>& scale2) { |
| 92 | return scale1.second > scale2.second; |
| 93 | }); |
| 94 | |
| 95 | FeatureKeypoints top_scale_keypoints(num_features); |
| 96 | FeatureDescriptors top_scale_descriptors; |
| 97 | top_scale_descriptors.data.resize(num_features, descriptors->data.cols()); |
| 98 | top_scale_descriptors.type = descriptors->type; |
| 99 | for (size_t i = 0; i < num_features; ++i) { |
| 100 | top_scale_keypoints[i] = (*keypoints)[scales[i].first]; |
| 101 | top_scale_descriptors.data.row(i) = descriptors->data.row(scales[i].first); |
| 102 | } |
| 103 | |
| 104 | *keypoints = std::move(top_scale_keypoints); |
| 105 | *descriptors = std::move(top_scale_descriptors); |
| 106 | } |
| 107 | |
| 108 | } // namespace colmap |