Image Stitching using OpenCV and Python (Creating Panorama Project)

15 / 23

Getting the Best Matches

The obtained descriptors in one image are to be recognized in the other image too. This is because, once the matching features are recognized, the images could be stitched based on these matching features.

Now, we are mainly going to do the following:

  • Use BFMatcher():

    • The Brute-Force matcher(cv2.BFMatcher()) is simple. It takes the descriptor of one feature in the first set and is matched with all other features in the second set using some distance calculation. And the closest one is returned.

    • For BF matcher, first we have to create the BFMatcher object using cv2.BFMatcher(). It takes two optional params:

      • First param is normType: It specifies the distance measurement to be used. For descriptors like ORB, BRIEF, BRISK etc, cv2.NORM_HAMMING should be used, which used Hamming distance as measurement.

      • Second param is a boolean variable, crossCheck: This is false by default. If it is true, Matcher returns only those matches with value (i,j) such that i-th descriptor in set A has j-th descriptor in set B as the best match and vice-versa. That is, the two features in both sets should match each other. It provides consistent results.

  • Use match method of BFMatcher object: : By calling this method, we will be returned the best matches.

  • Get an object of BFMatcher() and store it in bf variable.

    bf = cv2. << your code comes here >>(cv2.NORM_HAMMING, crossCheck=True)
  • Call the match method of bf object and pass the descriptors of the 2 images as input arguments. We will be returned a list of matches.

    matches = bf.<< your code comes here >>(des1,des2)

    matches is a list of DMatch objects. This DMatch object has the following attributes:

    • DMatch.distance - Distance between descriptors. The lower, the better it is.
    • DMatch.trainIdx - Index of the descriptor in train descriptors (des1)
    • DMatch.queryIdx - Index of the descriptor in query descriptors (des2)
    • DMatch.imgIdx - Index of the train image(right image)
  • Now, let us sort them in the order of their distances.

    matches = sorted(matches, key = lambda x:x.distance)
Get Hint See Answer

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...