SURFマッチングpython実装


# -*- coding: utf-8 -*-
"""
Created on Thu Jun 20 16:25:27 2019
    OPENCV 3.1.0     
  png middlebury       
@author: zhangzhicheng
"""

import numpy as np
import cv2
from matplotlib import pyplot as plt
leftImage = cv2.imread('E:\\stereovisioncoding\\SemiGlobalMathingImplementationmatlab\\image_left.png')
rightImage = cv2.imread('E:\\stereovisioncoding\\SemiGlobalMathingImplementationmatlab\\image_right.png')

#  sift
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(leftImage, None)
kp2, des2 = sift.detectAndCompute(rightImage, 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))]
print("matches", matches[0])
for i, (m,n) in enumerate (matches):
    if m.distance < 0.07*n.distance:
        matchesMask[i] = [1,0]

drawParams = dict(matchColor = (0, 255, 0), singlePointColor=None,
                  matchesMask = matchesMask, flags = 2)#flag=2      ,flag=0        
resultImage = cv2.drawMatchesKnn(leftImage,kp1,rightImage,kp2,matches,None,**drawParams)
plt.imshow(resultImage)
plt.show()