OpenCV Feature Matching — SIFT Algorithm (Scale Invariant Feature Transform)

durga prasad
Analytics Vidhya
Published in
3 min readJul 12, 2020

Distinctive Image Features from Scale-Invariant Keypoint — David G. Lowe

Here we have discussed the Template Matching method for Object Detection. The Template Matching method is mostly static and the process works only if the template/subset image is exactly contained in the full/target image. Slight deviations like the direction change, image intensity change, scale changes will not go well with Template Matching and results in very poor results. This makes the Template Matching method of object detection less usable and doesn't make to real-world applications.

To overcome the above pitfalls of the Template Matching methods, SIFT (Scale Invariant Feature Transform) can be used. SIFT algorithm addresses the problems of feature matching with changing scale, intensity, and rotation. This makes this process more dynamic and the template image doesn’t need to be exactly contained in the full/main image. This is considered one of the best approaches for feature matching and is widely used.

Steps to Perform Object Detection in python using OpenCV and SIFT

  1. Load the train image and test image, do the necessary conversion between the RGB channels to make the image compatible while displayed using matplotlib
  2. create ORB (Oriented fast and Rotated Brief) with the underlying SIFT algorithm
  3. Calculate the key points and descriptors for the train and test image separately
  4. Create the Brute Force matcher with the required parameters and here we use the KNN(K- nearest neighbor) matches which yields the Matches based on the similarity distances and let us further filter this out only considering if the distance between the two matches as good if the distance between them is 75%
  5. Use the good matches calculated in the above step to draw and highlight the matching areas

The code for the above steps and the results are displayed in the following images. Here the template image is an Indian currency note and the test image also contains currency notes. The main point to note here is the template image currency, the format, intensity, alignment is completely different and still, the algorithm finds the best possible matches of the template in the test image

The detection of key points in an image is nothing but selecting the points on the image which are considered to be good features and the descriptors are the representation of a point’s local neighborhood

Train Image
Test Image
Object Matching result — SIFT

--

--