OpenCV和Python:knnMatch参数的问题

时间:2014-06-18 19:35:19

标签: python opencv image-processing

我正在尝试关注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)

我尝试更改匹配以镜像fix in this question,如此:

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或ORB的全景/拼接的PYTHON示例相当简单,我也会很感激。我已经搜索了很多内容,并且只找到了关于如何完成它(或者用C语言编写)的操作,或者只发现了未完成/破坏的例子。

谢谢!

1 个答案:

答案 0 :(得分:1)

surf.compute()返回关键点和描述符列表。 flann.knnMatch()感到困惑,因为des1是一对列表而不是k=1,它会找到另一对列表(即des2)。检查des1和des2的shape()

des1[1]des2[1]传递给flann.knnMatch()或使用surf.detectAndCompute()代替surf.detect()surf.compute()