# Select Keypoints with mouse

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?

thanks!

edit retag 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?

( 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?

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

Well, not directly, but you could use http://docs.opencv.org/modules/featur... . However, it's not really complicated to do it on your own, it's only one for loop ^^

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

Sort by ยป oldest newest most voted

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.

more

awesome. many thanks!

( 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);


gives:

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.

Thanks!

( 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.

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

Brilliant. Thank you again, much appreciated.

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

Official site

GitHub

Wiki

Documentation