如何根据响应对opencv KeyPoints进行排序?

bea*_*ver 3 c++ opencv

我有一些OpenCV KeyPoints,它们存储为vector<KeyPoint>list<KeyPoint>.如何根据KeyPoints的响应对它们进行排序以获得最佳的n个关键点?

问候.

jle*_*and 6

查看文档,并猜测你正在尝试做这样的事情,

以下是KeyPoint在OpenCV中的实现方式.

所以根据我的理解,你想要使用的是响应元素:

float response; // the response by which the most strong keypoints have been selected. Can be used for the further sorting or subsampling
Run Code Online (Sandbox Code Playgroud)

所以这绝对是我在你的情况下要做的.创建一个函数,通过响应对您的矢量进行排序:)

希望这可以帮助

编辑:

试图利用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;
}
Run Code Online (Sandbox Code Playgroud)

  • 是的你可以,查看列表文档:http://www.cplusplus.com/reference/stl/list/sort/.您可以使用自定义函数来获取2个KeyPoint参数的"comp",并通过响应对它们进行比较.比你只是将该方法提供给列表排序. (3认同)

小智 5

我已将关键点存储为std::vector<cv::KeyPoint>并对其进行排序:

 std::sort(keypoints.begin(), keypoints.end(), [](cv::KeyPoint a, cv::KeyPoint b) { return a.response > b.response; });
Run Code Online (Sandbox Code Playgroud)

注意:lambda-expression需要使用C++ 11.