Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort opencv KeyPoints according to the response?

Tags:

c++

opencv

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.

like image 504
beaver Avatar asked Dec 09 '22 23:12

beaver


2 Answers

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;
}
like image 193
jlengrand Avatar answered Dec 30 '22 11:12

jlengrand


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.

like image 35
50ty Avatar answered Dec 30 '22 12:12

50ty