我正在尝试遵循opencv教程here。不幸的是,它在flann.knnMatch(des1,des2,k = 2)处失败。这是我的代码:
import cv2
import time
import numpy as np
im1 = cv2.imread('61_a.tif')
im2 = cv2.imread('61_b.tif')
surf = cv2.SURF(500,3,4,1,0)
print "Detect and Compute"
kp1 = surf.detect(im1,None)
kp2 = surf.detect(im2,None)
des1 = surf.compute(im1,kp1)
des2 = surf.compute(im2,kp2)
MIN_MATCH_COUNT = 5
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
matches = matcher.knnMatch(des1,des2,k=2)
TypeError: Argument given by name ('k') and position (2)
flann = cv2.flann_Index(des2, index_params)
matches = flann.knnMatch(des1,2,params={})
flann = cv2.flann_Index(des2, index_params)
TypeError: features is not a numerical tuple
最佳答案
surf.compute()
返回关键点和描述符列表。 flann.knnMatch()
感到困惑,因为des1
是一对列表,而不是k=1
而是找到另一对列表(即des2
)。检查des1和des2的shape()
。
可以将des1[1]
和des2[1]
传递给flann.knnMatch()
,也可以使用surf.detectAndCompute()
代替surf.detect()
和surf.compute()
。
https://stackoverflow.com/questions/24293707/