0% found this document useful (0 votes)
8 views4 pages

210720U Sift

Uploaded by

Chanupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

210720U Sift

Uploaded by

Chanupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

17/07/2024, 23:11 SIFT feature matching

SIFT Feature Matching


In [2]: # Import modules
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# Load images
image = cv.imread('store/store_shelf.png', cv.IMREAD_REDUCED_COLOR_2)
assert image is not None
template = cv.imread('store/oat_crunch.jpg', cv.IMREAD_REDUCED_COLOR_2)
assert template is not None

In [3]: # Create Sift routine


sift = cv.SIFT_create()
kp1, des1 = sift.detectAndCompute(image, None)
kp2, des2 = sift.detectAndCompute(template, None)

In [4]: print(f"Number of keypoints in first image: {len(kp1)}")


print(f"Number of keypoints in second image: {len(kp2)}")

Number of keypoints in first image: 2409


Number of keypoints in second image: 5864

In [19]: # Flaan matcher


FLAAN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLAAN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)

# Need to draw only good matches, so create a mask


matchesMask = [[0,0] for i in range(len(matches))]

# Ratio test as per Lowe's paper


for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]

dra_params = dict(matchColor = (0,255,0),


singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = cv.DrawMatchesFlags_DEFAULT)

# Draw matches
img3 = cv.drawMatchesKnn(image, kp1, template, kp2, matches, None, **dra_

plt.figure(figsize=(10, 8))
plt.imshow(cv.cvtColor(img3, cv.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

localhost:8888/nbconvert/html/SIFT feature matching.ipynb?download=false 1/4


17/07/2024, 23:11 SIFT feature matching

Trying this out for other images


In [21]: # Load images
image = cv.imread('books.jpg', cv.IMREAD_REDUCED_COLOR_2)
assert image is not None
template = cv.imread('atomic_habits.jpg', cv.IMREAD_REDUCED_COLOR_2)
assert template is not None

In [40]: # Plot images side by side


plt.figure(figsize=(15, 7))

# Plot left image


plt.subplot(1, 2, 1)
plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB))
plt.title('Image')
plt.axis('off') # Hide the axes

# Plot right image


plt.subplot(1, 2, 2)
plt.imshow(cv.cvtColor(template, cv.COLOR_BGR2RGB))
plt.title('Template')
plt.axis('off') # Hide the axes

plt.show()

localhost:8888/nbconvert/html/SIFT feature matching.ipynb?download=false 2/4


17/07/2024, 23:11 SIFT feature matching

In [35]: # Create Sift routine


sift = cv.SIFT_create()
kp1, des1 = sift.detectAndCompute(image, None)
kp2, des2 = sift.detectAndCompute(template, None)

In [23]: print(f"Number of keypoints in first image: {len(kp1)}")


print(f"Number of keypoints in second image: {len(kp2)}")

Number of keypoints in first image: 1560


Number of keypoints in second image: 33938

In [24]: # Flaan matcher


FLAAN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLAAN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)

# Need to draw only good matches, so create a mask


matchesMask = [[0,0] for i in range(len(matches))]

# Ratio test as per Lowe's paper


for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]

dra_params = dict(matchColor = (0,255,0),


singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = cv.DrawMatchesFlags_DEFAULT)

# Draw matches
img3 = cv.drawMatchesKnn(image, kp1, template, kp2, matches, None, **dra_

plt.figure(figsize=(10, 8))
plt.imshow(cv.cvtColor(img3, cv.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

localhost:8888/nbconvert/html/SIFT feature matching.ipynb?download=false 3/4


17/07/2024, 23:11 SIFT feature matching

In [ ]:

localhost:8888/nbconvert/html/SIFT feature matching.ipynb?download=false 4/4

You might also like