Skip to content

Beginners Opencv · Tutorials

Feature Matching (Brute-Force) – OpenCV 3.4 with python 3 Tutorial 26

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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

8 comments

  1. Thanks Sir, You explained very well.. Sir also explain the differences among the three mentioned detectors algorithm and which one is suitable in which scenario.

  2. Thank you so much for your great video about detecting and matching key points between two images.
    I have a request that is could you please provide a video like this one and explain how we can get disparity map? Because I obtain the disparity map wrongly from images.
    Thanks a lot.

Join the discussion