我有一些存储为
敬礼。
vector<KeyPoint>
或者 list<KeyPoint>
的 OpenCV 关键点。
如何按照关键点的响应对它们进行排序,以获取最好的n个关键点?敬礼。
我已将关键点存储为std::vector<cv::KeyPoint>
并使用以下方式进行排序:
std::sort(keypoints.begin(), keypoints.end(), [](cv::KeyPoint a, cv::KeyPoint b) { return a.response > b.response; });
通过查看文档,我猜测您正在尝试做类似于 这样的事情,
以下是OpenCV中KeyPoint的实现方式。
所以从我的理解来看,您想要使用的是响应元素:
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;
}
如果您将关键点存储在向量中:
#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;
}
list<KeyPoint>
,我想知道是否有任何方法可以像简单的list.sort()
一样对其进行排序? - beavercompare_response
函数,直接使用return first.response < second.response
。 - Philip Z.