<filename>src/Day 3/feature/matching.py
import cv2
from matplotlib import pyplot as plt
# Load the images.
img0 = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\kfc2.png',
 cv2.IMREAD_GRAYSCALE)
img1 = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\kfc1.jpg',
 cv2.IMREAD_GRAYSCALE)

# Perform SIFT feature detection and description.

sift = cv2.xfeatures2d.SIFT_create()

kp0, des0 = sift.detectAndCompute(img0, None)
kp1, des1 = sift.detectAndCompute(img1, None)
# Perform brute-force matching.

bf = cv2.BFMatcher()
matches = bf.knnMatch(des0,des1,k=2)
#iterate through the descriptors and determine
#whether they are a match or not, and then calculate the quality of this match (distance) and
#sort the matches so that we can display the top n matches with a degree of confidence that
#they are, in fact, matching

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
# cv.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img0,kp0,img1,kp1,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.imshow(img3),plt.show()
<filename>src/Day 3/feature/matching.py
import cv2
from matplotlib import pyplot as plt
# Load the images.
img0 = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\kfc2.png',
 cv2.IMREAD_GRAYSCALE)
img1 = cv2.imread(r'C:\Users\harrizazham98\Desktop\OpenCVForPython\resources\Day 3\kfc1.jpg',
 cv2.IMREAD_GRAYSCALE)

# Perform SIFT feature detection and description.

sift = cv2.xfeatures2d.SIFT_create()

kp0, des0 = sift.detectAndCompute(img0, None)
kp1, des1 = sift.detectAndCompute(img1, None)
# Perform brute-force matching.

bf = cv2.BFMatcher()
matches = bf.knnMatch(des0,des1,k=2)
#iterate through the descriptors and determine
#whether they are a match or not, and then calculate the quality of this match (distance) and
#sort the matches so that we can display the top n matches with a degree of confidence that
#they are, in fact, matching

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
# cv.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img0,kp0,img1,kp1,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.imshow(img3),plt.show()
# Load the images. # Perform SIFT feature detection and description. # Perform brute-force matching. #iterate through the descriptors and determine #whether they are a match or not, and then calculate the quality of this match (distance) and #sort the matches so that we can display the top n matches with a degree of confidence that #they are, in fact, matching # Apply ratio test # cv.drawMatchesKnn expects list of lists as matches.