Exp 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Course Name: Computer Vision Lab Course Code: CSP-422

Experiment:1.2

Aim: Write a program to assess various feature matching algorithms for object
recognition.

Software Required: Google Colab

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.

Name: Ayushman Kumar UID: 20BCS8232


Course Name: Computer Vision Lab Course Code: CSP-422

8. Select the top 20 matches with the smallest distances.


9. Draw and visualize the matches between img1 and img2 using
cv2.drawMatches function.
10. Display the resulting image using matplotlib.pyplot.imshow and
plt.show().

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:

Name: Ayushman Kumar UID: 20BCS8232

You might also like