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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

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

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.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

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.