FLANN特征匹配(Python)

输入图片

FLANN特征匹配(Python)_第1张图片

FLANN特征匹配(Python)_第2张图片

算法输出图

FLANN特征匹配(Python)_第3张图片

可以看到,这里的算法效果比之前的ORB算法要好很多了! 可以点击来访问一下,就知道差别了~
ORB特征匹配(python)

代码

import cv2
from matplotlib import pyplot as plt

queryImage = cv2.imread('6.jpg', 0)
trainingImage = cv2.imread('7.jpg', 0)

sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(queryImage, None)
kp2, des2 = sift.detectAndCompute(trainingImage, None)

FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
searchParams = dict(checks=50)

flann = cv2.FlannBasedMatcher(indexParams, searchParams)

matches = flann.knnMatch(des1, des2, k=2)

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

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

drawParams = dict(matchColor=(0, 255, 0),
                  singlePointColor=(255, 0, 0),
                  matchesMask=matchesMask,
                  flags=0
                  )
resultImage = cv2.drawMatchesKnn(queryImage, kp1, trainingImage, kp2, matches, None, **drawParams)
plt.xticks([]), plt.yticks([])
plt.imshow(resultImage), plt.show()

你可能感兴趣的:(Python,OpenCV)