Feature matching using ORB algorithm in PythonOpenCV (original) (raw)

ORB (Oriented FAST and Rotated BRIEF) is a feature detection and description algorithm used to identify and match keypoints between images. It combines the FAST keypoint detector with the BRIEF descriptor and introduces orientation compensation to achieve rotation invariance.

Working

Implementation

Let's consider two images of the same object. In this implementation, we will use ORB with OpenCV to detect keypoints, compute descriptors, and match features between the images. You may use this sample image.

Step 1: Installing required libraries

Before we start, make sure OpenCV is installed. If not, install it using the following command:

!pip install opencv-python opencv-python-headless

Step 2: Importing Libraries

Importing OpenCV, Numpy and Matplotlib libraries for the implementation.

Python `

import numpy as np import cv2 import matplotlib.pyplot as plt from google.colab.patches import cv2_imshow

`

Step 3: Loading the Images

Loading the query and training images using cv2.imread() and convert them to grayscale since ORB operates on single-channel images.

Python `

query_img = cv2.imread('/content/sports-car test.webp') train_img = cv2.imread('/content/sports-car train.webp')

query_img_bw = cv2.cvtColor(query_img, cv2.COLOR_BGR2GRAY) train_img_bw = cv2.cvtColor(train_img, cv2.COLOR_BGR2GRAY)

`

Step 4: Detecting Keypoints and Finding Descriptors

Using the ORB detector to identify keypoints in both images and compute their corresponding descriptors. The keypoints represent distinctive image regions, while the descriptors encode their local appearance for feature matching.

Python `

orb = cv2.ORB_create()

queryKeypoints, queryDescriptors = orb.detectAndCompute(query_img_bw, None) trainKeypoints, trainDescriptors = orb.detectAndCompute(train_img_bw, None)

`

Step 5: Matching the Descriptors

We use a Brute-Force Matcher to compare the descriptors from both images and find the best matching keypoints based on descriptor similarity.

Python `

matcher = cv2.BFMatcher() matches = matcher.match(queryDescriptors, trainDescriptors)

`

Step 6: Visualizing the Matches

Finally, we visualize the matches by drawing lines between the matched keypoints. The output image is resized for improved visibility and displayed using Matplotlib.

Python `

final_img = cv2.drawMatches(query_img, queryKeypoints, train_img, trainKeypoints, matches[:20], None) final_img = cv2.resize(final_img, (1000, 650))

plt.figure(figsize=(10, 6)) plt.imshow(cv2.cvtColor(final_img, cv2.COLOR_BGR2RGB)) plt.title("Feature Matches") plt.axis('off')
plt.show()

`