0

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)

I get the error:

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={})

BUT then I get this error:

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?

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.

Thanks!

Community
  • 1
  • 1
Spatial Pariah
  • 351
  • 4
  • 17

1 Answers1

1

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.

Either pass des1[1] and des2[1] to flann.knnMatch() or use surf.detectAndCompute() in replacement of surf.detect() and surf.compute().