Professional Documents
Culture Documents
Exp 2
Exp 2
Exp 2
Experiment:1.2
Aim: Write a program to assess various feature matching algorithms for object
recognition.
Description:
Feature matching algorithms are crucial for object recognition tasks as they establish
correspondences between key points or descriptors extracted from images. Some
commonly used feature matching algorithms for object recognition are:
· Brute-Force Matching: Brute-force matching involves comparing each feature in one
image with every feature in another image using a distance metric (e.g., Euclidean
distance, Hamming distance). It is a straightforward approach but can be computationally
expensive for large feature sets.
· Fast Library for Approximate Nearest Neighbors (FLANN): FLANN is an approximate
nearest neighbor search library that accelerates feature matching by using indexing
structures. It performs fast approximate nearest neighbor searches, reducing the
computational cost compared to brute-force matching.
· Nearest Neighbor with Ratio Test: In this approach, the nearest neighbors of each
feature are found, and a ratio test is applied to select the best match. The ratio test
compares the distances between the best match and the second-best match. If the ratio
is below a certain threshold, the match is considered valid. This method helps to reduce
false matches caused by ambiguous correspondences.
Pseudo code/Algorithms/Flowchart/Steps:
1. Import the necessary libraries: numpy, cv2 (OpenCV), and matplotlib.pyplot.
1. Read two grayscale images: img1 and img2.
2. Resize img1 to match the dimensions of img2.
3. Create an ORB (Oriented FAST and Rotated BRIEF) detector and descriptor.
4. Detect keypoints and compute descriptors for both img1 and img2.
5. Create a Brute-Force Matcher (BFMatcher) using Hamming distance and enable cross-
checking.
6. Match the descriptors of img1 and img2 using the BFMatcher.
7. Sort the matches based on their distances.
Implementation:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread('pqr.jpg',0)
img2 = cv2.imread('xyz.jpg',0)
img1=cv2.resize(img1, (img2.shape[1],img2.shape[0]))
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:20],None, flags=2)
plt.imshow(img3)
plt.show()
Output: