Ask Your Question

Select Keypoints with mouse

asked 2015-03-22 08:03:36 -0500

antithing gravatar image

Hi all, I have a few Keypoints in a video stream:

    vector<cv::KeyPoint> keypoints;

I need to add them to a point list in a specific order:

            vector<Point2d> imagePoints;

So my question is.... How can i select a Keypoint with a mouse click, so that i can convert it to the point of my choosing?

I can get the coordinates with a setMouseCallback, but what is the best way to select a keypoint in that region?


edit retag flag offensive close merge delete


Probably going throught the vector of keypoints and finding the closest one to the mouse-click coordiantes. Where exactly is the problem?

Guanta gravatar imageGuanta ( 2015-03-22 08:28:15 -0500 )edit

ok, thanks for your reply. So I store the mouse click, then compare it to the vector list coordinates. Is there a function for finding the nearest one?

antithing gravatar imageantithing ( 2015-03-22 08:45:48 -0500 )edit

Well, not directly, but you could use . However, it's not really complicated to do it on your own, it's only one for loop ^^

Guanta gravatar imageGuanta ( 2015-03-22 09:01:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-22 09:16:12 -0500

Guanta gravatar image

Okay, so here one possible solution:

// given your mousepoint coordiantes x,y (e.g. through setMouseCallback)
cv::Point mouse(x,y);
int nearest_index = -1;
float min_dist = std::numeric_limits<float>::max();
for( size_t i = 0; i < keypoints.size(); i++ ) {
  double distance = cv::norm(mouse - keypoints[i].pt);
  if ( distance < min_dist ) {
    min_dist = distance;
    nearest_index = i;
cv::KeyPoint nearest = keypoints[nearest_index];

As also said in the comments, you can also use BFMatcher or FlannMatcher if this solution is too slow for you.

edit flag offensive delete link more


awesome. many thanks!

antithing gravatar imageantithing ( 2015-03-22 09:47:35 -0500 )edit

Sorry, one more thing... i am getting an error with your code... this line:

       double distance = cv::norm(mouse - keypoints[i].pt);


no operator "-" matches these operands operand types are: cv::Point - cv::Point2f

I have tried converting from keypoints to point2f, but get the same error.


antithing gravatar imageantithing ( 2015-03-22 12:30:32 -0500 )edit

Somehow I thought that this question will come, changing from cv::Point mouse(x,y) to cv::Point2f mouse(x,y) should fix it.

Guanta gravatar imageGuanta ( 2015-03-22 16:52:43 -0500 )edit

Brilliant. Thank you again, much appreciated.

antithing gravatar imageantithing ( 2015-03-22 18:00:52 -0500 )edit

Question Tools

1 follower


Asked: 2015-03-22 08:03:36 -0500

Seen: 822 times

Last updated: Mar 22 '15