The Wayback Machine - https://web.archive.org/web/20240629112155/https://www.geeksforgeeks.org/feature-detection-and-matching-with-opencv-python/
Open In App

Feature detection and matching with OpenCV-Python

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see about feature detection in computer vision with OpenCV in Python. Feature detection is the process of checking the important features of the image in this case features of the image can be edges, corners, ridges, and blobs in the images.

In OpenCV, there are a number of methods to detect the features of the image and each technique has its own perks and flaws.

Note: The images we give into these algorithms should be in black and white. This helps the algorithms to focus on the features more.

Image in use: 

Method 1: Haris corner detection

Haris corner detection is a method in which we can detect the corners of the image by sliding a slider box all over the image by finding the corners and it will apply a threshold and the corners will be marked in the image. This algorithm is mainly used to detect the corners of the image.

Syntax: 

cv2.cornerHarris(image, dest, blockSize, kSize, freeParameter, borderType)

Parameters:  

  • Image – The source image to detect the features
  • Dest – Variable to store the output image
  • Block size – Neighborhood size
  • Ksize – Aperture parameter
  • Border type: The pixel revealing type.

Example: Feature detection and matching using OpenCV

Python3




# Importing the libraries
import cv2
import numpy as np
  
# Reading the image and converting the image to B/W
image = cv2.imread('book.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_image = np.float32(gray_image)
  
# Applying the function
dst = cv2.cornerHarris(gray_image, blockSize=2, ksize=3, k=0.04)
  
# dilate to mark the corners
dst = cv2.dilate(dst, None)
image[dst > 0.01 * dst.max()] = [0, 255, 0]
  
cv2.imshow('haris_corner', image)
cv2.waitKey()


Output:

Method 2: Shi-Tomasi corner detection

Shi and Tomasi came up with a different corner detection algorithm which is mostly similar to the Haris corner detection algorithm in which the only difference will be the kernel value in which we can find only the n strongest corners of the image. This can greatly help while we need only the limited and very important features of the image.

Syntax: 

cv2.goodFeaturesToTrack(image, maxc, Quality, maxD)

Parameters:

  • image – The source image we need to extract the features.
  • maxc – Maximum number of corners we want [Negative values gives all the corners]
  • Quality – Quality level parameter (preferred value=0.01)
  • maxD – Maximum distance (preferred value=10)

Example: Feature detection and matching using OpenCV

Python3




# Importing the libraries
import cv2
import numpy as np
  
# Reading the image and converting into B?W
image = cv2.imread("book.png")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Applying the function
corners = cv2.goodFeaturesToTrack(
    gray_image, maxCorners=50, qualityLevel=0.02, minDistance=20)
corners = np.float32(corners)
  
for item in corners:
    x, y = item[0]
    x = int(x)
    y = int(y)
    cv2.circle(image, (x, y), 6, (0, 255, 0), -1)
  
# Showing the image
cv2.imshow('good_features', image)
cv2.waitKey()


Output:

Method 3: SIFT (Scale-Invariant Feature Transform)

While Haris and shi-Tomasi are the algorithms to detect the corners of the image. SIFT is one of the important algorithms that detect objects irrelevant to the scale and rotation of the image and the reference. This helps a lot while we are comparing the real-world objects to an image though it is independent of the angle and scale of the image. This method will return the key points of the images which we need to mark in the image.

Syntax:  

sift = cv2.xfeatures2d.SIFT_create()

kp, des = sift.detectAndCompute(gray_img, None)

This function returns key points which we later use with drawkeypoints() method to draw the keypoints.

Note: The circles in the image represent the keypoints, where the size of the circle directly represents the strength of the key points.

Example: Feature detection and matching using OpenCV

Python3




# Importing the libraries
import cv2
  
# Reading the image and converting into B/W
image = cv2.imread('book.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Applying the function
sift = cv2.xfeatures2d.SIFT_create()
kp, des = sift.detectAndCompute(gray_image, None)
  
  
# Applying the function
kp_image = cv2.drawKeypoints(image, kp, None, color=(
    0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT', kp_image)
cv2.waitKey()


Output:

Method 4: FAST algorithm for corner detection

SURF is fast when compared to SIFT but not as fast to use with real-time devices like mobile phones and surveillance cameras. So FAST algorithm was introduced with a very fast computing time. However FAST gives us only the key points and we may need to compute descriptors with other algorithms like SIFT and SURF. With a Fast algorithm, we can detect corners and also blobs.

Syntax:

fast = cv2.FastFeatureDetector_create()

fast.setNonmaxSuppression(False)

kp = fast.detect(gray_img, None)

Example: Feature detection and matching using OpenCV

Python3




# Importing the libraries
import cv2
  
# Reading the image and converting into B/W
image = cv2.imread('book.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
  
# Applying the function
fast = cv2.FastFeatureDetector_create()
fast.setNonmaxSuppression(False)
  
  
# Drawing the keypoints
kp = fast.detect(gray_image, None)
kp_image = cv2.drawKeypoints(image, kp, None, color=(0, 255, 0))
  
cv2.imshow('FAST', kp_image)
cv2.waitKey()


Output:

Method 5: ORB (Oriented FAST and Rotated Brief)

ORB is a very effective way of detecting the features of the image when compared to SIFT and SURF. ORB is programmed to find fewer features in the image when compared to the SIFT and SURF algorithm because it detects the very important features in less time than them yet this algorithm is considered as a very effective algorithm when compared to other detecting algorithms.

Syntax:  

orb = cv2.ORB_create(nfeatures=2000)

kp, des = orb.detectAndCompute(gray_img, None)

Example: Feature detection and matching using OpenCV

Python3




# Importing the libraries
import cv2
  
# Reading the image and converting into B/W
image = cv2.imread('book.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Applying the function
orb = cv2.ORB_create(nfeatures=2000)
kp, des = orb.detectAndCompute(gray_image, None)
  
# Drawing the keypoints
kp_image = cv2.drawKeypoints(image, kp, None, color=(0, 255, 0), flags=0)
  
cv2.imshow('ORB', kp_image)
cv2.waitKey()


Output:



Previous Article
Next Article

Similar Reads

Feature matching using ORB algorithm in Python-OpenCV
ORB is a fusion of FAST keypoint detector and BRIEF descriptor with some added features to improve the performance. FAST is Features from Accelerated Segment Test used to detect features from the provided image. It also uses a pyramid to produce multiscale-features. Now it doesn’t compute the orientation and descriptors for the features, so this is
2 min read
Feature Matching using Brute Force in OpenCV
In this article, we will do feature matching using Brute Force in Python by using OpenCV library. Prerequisites: OpenCV OpenCV is a python library which is used to solve the computer vision problems. OpenCV is an open source Computer Vision library. So computer vision is a way of teaching intelligence to machine and making them see things just like
13 min read
Feature Matching in OpenCV
OpenCV feature matching is a super cool technology in computer vision that's changing how machines understand the visual world. It's super important in things like image search, object recognition, image stitching, and making pictures look better. If you want to level up your image analysis, classification, and autonomous navigation skills, masteri
8 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection, the image frame coming from a live webcam or video
5 min read
Python | Corner detection with Harris Corner Detection method using OpenCV
Harris Corner detection algorithm was developed to identify the internal corners of an image. The corners of an image are basically identified as the regions in which there are variations in large intensity of the gradient in all possible dimensions and directions. Corners extracted can be a part of the image features, which can be matched with fea
2 min read
Python | Corner Detection with Shi-Tomasi Corner Detection Method using OpenCV
What is a Corner? A corner can be interpreted as the junction of two edges (where an edge is a sudden change in image brightness). Shi-Tomasi Corner Detection - Shi-Tomasi Corner Detection was published by J.Shi and C.Tomasi in their paper 'Good Features to Track'. Here the basic intuition is that corners can be detected by looking for significant
3 min read
Python | Document field detection using Template Matching
Template matching is an image processing technique which is used to find the location of small-parts/template of a large image. This technique is widely used for object detection projects, like product quality, vehicle tracking, robotics etc. In this article, we will learn how to use template matching for detecting the related fields in a document
2 min read
Histogram matching with OpenCV, scikit-image, and Python
Histogram matching is used for normalizing the representation of images, it can be used for feature matching, especially when the pictures are from diverse sources or under varied conditions (depending on the light, etc). each image has a number of channels, each channel is matched individually. Histogram matching is possible only if the number of
3 min read
Template matching using OpenCV in Python
Template matching is a technique for finding areas of an image that are similar to a patch (template). A patch is a small image with certain features. The goal of template matching is to find the patch/template in an image. To find it, the user has to give two input images: Source Image (S) - The image to find the template in, and Template Image (T
6 min read
Feature extraction and image classification using OpenCV
This article is your ultimate guide to becoming a pro at image feature extraction and classification using OpenCV and Python. We'll kick things off with an overview of how OpenCV plays a role in feature extraction, and we'll go through the setup process for the OpenCV environment. You'll get to learn all about different feature extraction technique
11 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg