SIFT暴力匹配 FLANN快速匹配

from PIL import Image
import cv2
import numpy as np
import matplotlib.pyplot as plt
#计算特征点
cs1 = cv2.imread("cs1.jpg")
img1 = cv2.resize(cs1,(500,500))
gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
cv2.imshow("img1",img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(gray,None)  #搜索关键点
img_sift = cv2.drawKeypoints(img1,kp,img1) #绘制关键点
cv2.imshow("img_sift",img_sift)
cv2.waitKey(0)
cv2.destroyAllWindows()

kp,des = sift.compute(gray,kp)  #计算关键点 得到关键点参数
print(len(kp))
print (des.shape)
print (des[0])

#蛮力匹配
cs1 = cv2.imread("cs1.jpg")
cs2 = cv2.imread("cs2.jpg")
img1 = cv2.resize(cs1,(500,500))
img2 = cv2.resize(cs2,(500,500))

kp1 = sift.detect(img1,None)
kp2 = sift.detect(img2,None)
kp1,des1 = sift.compute(img1,kp1,img1)
kp2,des2 = sift.compute(img2,kp2,img2)

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[:50],None,flags=2)
cv2.imshow("img3",img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
#一对多的匹配
bf = cv2.BFMatcher()
matches2 = bf.knnMatch(des1,des2,k=2)
good = []
for m,n in matches2:
    if m.distance

SIFT暴力匹配 FLANN快速匹配_第1张图片
SIFT暴力匹配 FLANN快速匹配_第2张图片
SIFT暴力匹配 FLANN快速匹配_第3张图片

你可能感兴趣的:(SIFT暴力匹配 FLANN快速匹配)