How it works...

In this recipe, we implement two heuristics for filtering bad matches. The first one is the ratio test. It checks whether thebest matche is significantly better than the second best one. The check is performed through comparing matching scores. Two best matches for each keypoint are found using the knnMatch method of the cv2.BFMatcher class.

The second heuristic is the cross-check test. For two images, A and B, it checks whether the matches found in B for keypoints in A are the same as the ones found in A for keypoints in B. The correspondences that were found in both directions are retained, and the other ones are removed.

The following is the expected output:

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.