This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pip install opencv-python==3.3.0.10 opencv-contrib-python==3.3.0.10 | |
#https://stackoverflow.com/questions/37039224/attributeerror-module-object-has-no-attribute-xfeatures2d-python-opencv-2 | |
#Updated April 25/2020 | |
import numpy as np | |
import cv2 | |
import matplotlib.pyplot as plt | |
img1 = cv2.imread('D:\\Template\\p23.jpg',0) # queryImage | |
img2 = cv2.imread('D:\\TestImage\T3.jpeg',0) # trainImage | |
sift = cv2.xfeatures2d.SIFT_create() | |
# find the keypoints and descriptors with SIFT | |
kp1, des1 = sift.detectAndCompute(img1,None) | |
kp2, des2 = sift.detectAndCompute(img2,None) | |
# BFMatcher with default params | |
bf = cv2.BFMatcher() | |
matches = bf.knnMatch(des1,des2, k=2) | |
# Apply ratio test | |
good = [] | |
for m,n in matches: | |
if m.distance < 0.7*n.distance: | |
good.append([m]) | |
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2) | |
plt.imshow(img3),plt.show() | |
print('Number of matches - SIFT') | |
print(len(good)) |
No comments:
Post a Comment