import cv2标签:kp1,kp2,匹配,特征,cv2,matches,关于,img2,img1 From: https://www.cnblogs.com/kun-sir/p/16784886.html
import numpy as np
import matplotlib.pyplot as plt
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Brute_Force蛮力匹配
img1=cv2.imread("build1.jpeg",0)
img2=cv2.imread("build3.jpeg",0)
# cv_show("img1",img1)
# cv_show("img2",img2)
sift=cv2.SIFT_create()
kp1=sift.detect(img1,None)
kp2=sift.detect(img2,None)
img1=cv2.drawKeypoints(img1,kp1,img1)
img2=cv2.drawKeypoints(img2,kp2,img2)
kp1,des1=sift.compute(img1,kp1)
kp2,des2=sift.compute(img2,kp2)
# crossCheck表示两个特征点要互相匹,例如A中的第1 个特征点与B中的第j个特征点最近的,并且B中的第j个特征点到A中的第i个特征点也是
#NORM L2:归一化数组的(欧几里德距离),如果其他特征计算方法需要考虑不同的匹配计算方式
#1对1最佳匹配
# bf=cv2.BFMatcher(crossCheck=True)
#
# matches=bf.match(des1,des2)
# matches=sorted(matches,key=lambda x:x.distance)
#
# img3=cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None,flags=2)
#
# cv_show("img3",img3)
bf = cv2.BFMatcher()
matches=bf.knnMatch(des1,des2,k=2)
#对关键点进行过滤
good=[]
for m,n in matches:
if m.distance<0.75*n.distance:
good.append([m])
img3=cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
cv_show("img3",img3)