简体   繁体   中英

How to sort opencv KeyPoints according to the response?

I've some OpenCV KeyPoints, and they are stored as vector<KeyPoint> or list<KeyPoint> . How to sort them according to the response of the KeyPoints to obtain the best n keypoints?

Regards.

Looking at the documentation, and guessing you are trying to do something like this ,

Here is how KeyPoint is implemented in OpenCV.

So from what I understand, what you want to use is the response element:

float response; // the response by which the most strong keypoints have been selected. Can be used for the further sorting or subsampling

So this is definitely what I would be going to in your case. Create a function that sorts your vector by response :)

Hope this helps

EDIT :

Trying to take advantage of Adrian's advice (This is my first cpp code though, so expect to have some corrections to perform)

// 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;
}

I've stored the keypoints as std::vector<cv::KeyPoint> and sorted them with:

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

Note: Usage of C++ 11 required for lambda-expression.

if you store keypoints in a vector:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM