In this tutorial we will talk about Feature Matching with OpenCV. In my example I used the same book cover but in different lighting conditions, position and perspective.

For a normal comparison they could be different but two simple steps we can compare the main characteristics:

  • Orb detector method
  • Brute Force Matching

Orb detector method

Passing the image to the ORB Detector method we obtain an array with the definition of the image characteristics. Obviously it will not do the pixel by pixel analysis because obviously they will be different. Here is the code:

# ORB Detector
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)

Now let’s take the descriptor of both images, in my example defined with des1 and des2 . Obviously we need a comparison function for feature matching.

Brute Force Matching

For this purpose we use the BFMatcher opencv method. Here are some parameters to set:

  • Norm_hamming is used when comparing Orb detector arrays
  • crosscheck = true it allows us to have only the results with the best score in the comparison

This is the code:

# Brute Force Matching
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)

Finally we draw the lines that represent the equalities:

matching_result = cv2.drawMatches(img1, kp1, img2, kp2, matches[:50], None, flags=2)

The image below is an example of the final result. I do not recommend using this method for real-time analysis, such as a video, because it requires a lot of computing energy