4

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.

3 Answers 3

6

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;
}
4
  • If I store them as list<KeyPoint>, I am wondering if there is anyway to sort it like a simple list.sort()?
    – beaver
    Commented Jun 6, 2012 at 8:54
  • 3
    Yes u can, look at the list documentation: cplusplus.com/reference/stl/list/sort . You can just use a custom function for the "comp" that gets 2 KeyPoint parameters and compares them by the response. Than you just give that method to the list sort.
    – Adrian
    Commented Jun 6, 2012 at 9:08
  • The thing that gets me is that I don't know whether a 'response' with a number closer to positive infinity is better than a 'response' closer to negative infinity or the other way around. I'm going to assume that a more positive response is better.
    – Peter pete
    Commented Jan 29, 2014 at 0:37
  • you can simplify compare_response and just do return first.response < second.response
    – Philip Z.
    Commented Dec 7, 2022 at 14:52
6

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.

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.