Image Keypoints Matching Example with Brute-Force method in OpenCV

     Feature matching involves identifying common points or keypoints in multiple images. These keypoints represent distinctive areas in images, making them ideal for tasks like finding corresponding points between images, object tracking, and more. Feature matching is essential for applications where recognizing similar patterns or objects is required.

     OpenCV provides a comprehensive toolkit to achieve these tasks. One of the key components for feature matching is the Brute-Force Matcher. In this blog post, we will delve into the Brute-Force Matcher in OpenCV, understand its working principles, and demonstrate how it can be utilized for feature matching. The tutorial covers:

  1. Understanding the Brute-Force Matcher
  2. Implementing Brute-Force Matcher in OpenCV
  3. Conclusion

     Let's get started.


Understanding the Brute-Force Matcher

    The Brute-Force Matcher is a straightforward method for feature matching. It exhaustively compares each feature in one image with all the features in another image to find potential matches. It is part of the OpenCV library and works by comparing keypoints from two different images based on their descriptors. Descriptors are numerical representations of keypoints' local features. The method involves the following steps to match given images. 

  1. Keypoint Extraction and Description: First, keypoints are extracted from both images using algorithms like SIFT, SURF, or ORB. These keypoints are then described using their descriptors.

  2. Matching Descriptors: For each descriptor in the first image, the Brute-Force Matcher computes the distance to all descriptors in the second image. The distance metric can be Euclidean distance or another suitable measure.

  3. Finding the Best Match: The matcher selects the descriptor in the second image with the smallest distance to the descriptor in the first image. This indicates the best match for that particular keypoint.

  4. Filtering Matches: To avoid incorrect matches, a threshold is applied to filter out matches that are not sufficiently distinct.


Implementing Brute-Force Matcher in OpenCV
   In this tutorial, we use BFMatcher() and it's knnMatch method to implement Brute-Force matching. The knnMatch method provided by the Brute-Force Matcher is a powerful tool that extends beyond simple feature matching. "knn" stands for "k-nearest neighbors," which is a concept in machine learning and data analysis. In this context, it refers to finding the k nearest neighbors for each feature in the reference image within the query image.  

    We'll start loading the target images convert them to grayscale. Then we call ORB detector and find keypoints and compute descriptors for both images. We create Brute-Force Matcher object and match descriptors with
knnMatch() method. After that, we apply ratio test to filter matches. Finally, we draw matches and display the original images with keypoints marked.

import cv2
from matplotlib import pyplot as plt

# Load two images
image1 = cv2.imread('items.jpg', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('rotated_items1.jpg', cv2.IMREAD_GRAYSCALE)

# Create ORB detector
orb = cv2.ORB_create()

# Detect keypoints and compute descriptors for both images
keypoints1, descriptors1 = orb.detectAndCompute(image1, None)
keypoints2, descriptors2 = orb.detectAndCompute(image2, None)

# Create a Brute-Force Matcher
bf = cv2.BFMatcher()

# Match descriptors
matches = bf.knnMatch(descriptors1, descriptors2, k=2)

# Apply ratio test to filter matches
good_matches = []
for m, n in matches:
if m.distance < 0.5 * n.distance:

# Draw matches
matched_image = cv2.drawMatches(image1, keypoints1, 
                        image2, keypoints2, good_matches, None)

# Display the original image with keypoints marked
plt.figure(figsize = (14, 10))
plt.title('Image mathching with Brute-Force method')


      The Brute-Force Matcher in OpenCV is a powerful tool for feature matching, enabling the identification of common keypoints across images. The method provides a solid foundation for understanding how feature matching works. By retrieving multiple potential matches for each feature, you gain more control over the matching process and can implement various filtering techniques to ensure robust matches.
    In this tutorial, we've briefly explored Brute-Force Matcher  and its implementation with OpenCV API in Python. 

No comments:

Post a Comment