Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For me, it happened because the keypoints were swapped from the expected between the matcher and the drawMatches. But it can also occur in your case because you have a loop but keypoints2 and matches are reused but not cleared. You should move the keypoints2/descriptors2/matches declarations inside of the loop and also clear those same vectors at the end of the while loop.

For both matcher and drawMatches, Query/Scene comes first, then Train/Object. In your case, descriptors1 is the trained keypoint set and descriptors2 is the query. You need to swap the order in the matcher and drawMatches functions.

Change this:

matcher.match(descriptors1, descriptors2, matches);

to this:

matcher.match(descriptors2, descriptors1, matches);

Also change this:

drawMatches( img1, keypoints1, img2, keypoints2,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
           vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

to this:

drawMatches( img2, keypoints2, img1, keypoints1,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
           vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );