问题描述
我正在尝试按照opencv教程。不幸的是,它在flann.knnMatch(des1,des2,k = 2)失败了。这是我的代码:
I am trying to follow the opencv tutorial here. Unfortunately, it fails at flann.knnMatch(des1,des2,k=2). Here is my code:
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)
我试图更改匹配以镜像,如下所示:
I have tried to change the matching to mirror the fix in this question like so:
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
我真的不确定我做错了什么。有人能指出我正确的方向吗?
I'm really not sure what I'm doing wrong. Can someone point me in the right direction?
如果您碰巧知道SURF或ORB的全景/拼接的PYTHON工作示例相当简单,我也会很感激。我已经搜索了很多内容,并且只找到了关于如何完成它的操作(或者用C语言编写)或者只找到了未完成/破坏的例子。
If you happen to know of a working PYTHON example of SURF or ORB for panorama/stitching that's fairly straightforward, I would appreciate that too. I have googled around quite a bit and have only found pieces of operations about how it might be accomplished (or it's written in C) or have only found unfinished/broken examples.
谢谢!
推荐答案
surf.compute()
返回两个关键点和描述符列表。 flann.knnMatch()
感到困惑,因为 des1
是一对列表,而不是 k = 1
它找到另一对列表(即 des2
)。检查des1和des2的形状()
。
surf.compute()
returns both keypoints and descriptors lists. flann.knnMatch()
gets confused because des1
is a pair of lists and instead of k=1
it finds another pair of lists (namely des2
). Check the shape()
of des1 and des2.
传递 des1 [1]
和 des2 [1]
到 flann.knnMatch()
或使用 surf.detectAndCompute()
代替 surf.detect()
和 surf.compute()
。
Either pass des1[1]
and des2[1]
to flann.knnMatch()
or use surf.detectAndCompute()
in replacement of surf.detect()
and surf.compute()
.
这篇关于OpenCV和Python:knnMatch参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!