【发布时间】:2016-01-22 21:21:14
【问题描述】:
我有一些 OpenCV 关键点,它们存储为 vector<KeyPoint> 或 list<KeyPoint>。
如何根据 KeyPoints 的响应对它们进行排序以获得最佳的 n 个关键点?
问候。
【问题讨论】:
我有一些 OpenCV 关键点,它们存储为 vector<KeyPoint> 或 list<KeyPoint>。
如何根据 KeyPoints 的响应对它们进行排序以获得最佳的 n 个关键点?
问候。
【问题讨论】:
查看文档,猜测您正在尝试做something like this,
这里是 OpenCV 中的how KeyPoint is implemented。
所以据我了解,您要使用的是响应元素:
float response; // the response by which the most strong keypoints have been selected. Can be used for the further sorting or subsampling
所以这绝对是你的情况。 创建一个按响应对向量进行排序的函数:)
希望对你有帮助
编辑:
尝试利用 Adrian 的建议(虽然这是我的第一个 cpp 代码,所以希望执行一些更正)
// list::sort
#include <list>
#include <cctype>
using namespace std;
// response comparison, for list sorting
bool compare_response(KeyPoints first, KeyPoints second)
{
if (first.response < second.response) return true;
else return false;
}
int main ()
{
list<KeyPoints> mylist;
list<KeyPoints>::iterator it;
// opencv code that fills up my list
mylist.sort(compare_response);
return 0;
}
【讨论】:
list<KeyPoint>,我想知道是否有办法像简单的list.sort() 一样对其进行排序?
我已将关键点存储为 std::vector<cv::KeyPoint> 并使用以下命令对其进行排序:
std::sort(keypoints.begin(), keypoints.end(), [](cv::KeyPoint a, cv::KeyPoint b) { return a.response > b.response; });
注意:lambda 表达式需要使用 C++ 11。
【讨论】:
如果您将关键点存储在向量中:
#include <algorithm> // std::sort
#include <vector> // std::vector
int main() {
std::vector<KeyPoint> keypoints;
// extract keypoints right here
std::sort(keypoints.begin(), keypoints.end(), response_comparator);
// do what ever you want with keypoints which sorted by response
}
bool response_comparator(const KeyPoint& p1, const KeyPoint& p2) {
return p1.response > p2.response;
}
【讨论】: