What is Scale Invariant Feature Transformation SIFT in OpenCV

This recipe explains what is Scale Invariant Feature Transformation SIFT in OpenCV

Recipe Objective: What is Scale Invariant Feature Transformation (SIFT) in OpenCV?

This recipe explains what Scale Invariant Feature Transformation (SIFT) is and how it works.

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below. Let us read the image in grayscale for better results.

Input Image

import numpy as np
import cv2
image = cv2.imread('ironman.jpg',0)

Step 2: Understanding SIFT

SIFT is a feature detection technique. Speaking of feature detection, we must know are features of an image first. Features can be anything such as corners, blobs, shapes, etc., that define the image. As the name suggests, SIFT is a scale-invariant method, and hence it involves scaling the picture. That is, the features are detected at any scale of the given image. For example, a corner in a zoomed-out version of one image might appear like a circle in a zoomed-in version of the same image. And hence, this corner may be missed out in the zoomed-out image. But as SIFT is scale-invariant, it will capture this corner also. The SIFT procedure involves four main steps. They are as listed below.

  1. Scale Space Extrema Detection: In this step, the image is smoothened using the difference of Gaussian blurring with a kernel size of 3. The kernels are also compared with the previous and next scales. If the central point of this kernel is the local extrema, then the point is considered a potentially key point (i.e., feature).
  2. Key point Localization: To obtain better results, the potential key points obtained must be further refined. That is why we compare the intensity of these key points with a threshold value (which is 0.03 by default). If the intensity is less than this value, then those key points are rejected. Edges are also removed using eigenvalues and ratios since we are only trying to extract the absolute features.
  3. Orientation Assignment: A direction is assigned to each pixel depending on the change in intensity, and an orientation histogram is created. The highest point of the histogram is considered orientation calculation. This helps us create critical issues with the same feature and scale but in different directions so that the same elements are detected even when we use a rotated version of the same image.
  4. Key point descriptor and key point matching : The Keypoint descriptors are created in this step, but only vectors are obtained from the orientation histogram. With the help of this and also by identifying the nearest neighbors, the key points are matched with the original image.

Step 3: SIFT implementation

There is no built-in function to implement SIFT in the OpenCV package. We have to install an additional package called the opencv-contrib-python using the following command in the terminal.

pip install opencv-contrib-python

Once we have this installed, then we can use the cv2.xfeatures2d.SIFT_create() function to create a SIFT object. We can extract the key points of the image using the detect() method of the sift object we created earlier, which takes the actual image as the input.

sift = cv2.xfeatures2d.SIFT_create()
key_points = sift.detect(image)

After extracting the key points, we can map it to our original image using the cv2.drawKeypoints() function, which takes the following arguments.

  • image: The original image.
  • keypoints: The detected keypoints
  • outImage: The output image.

new_image = cv2.drawKeypoints(image, key_points, None)

Step 4: Display the output

Let us display the output using cv2.imshow() function.

cv2.imshow("Detected Features", new_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

SIFT

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.