10

I want to match two pictures using Python+OpenCV. I have used SURF to extract keypoints and descriptors from both of them. Now, I need to match these descriptors and for this reason I decided to use Flann Matcher.

flann_params = dict(algorithm = FLANN_INDEX_KDTREE,trees = 4)    
matcher = cv2.FlannBasedMatcher(flann_params, {})

But when I try to use knnMatch with descriptors (desc1, desc2), openCV throws an exception.

raw_matches=matcher.knnMatch(np.asarray(desc1),np.asarray(desc2), 2)

The exception is the following:

raw_matches=matcher.knnMatch(np.asarray(desc1),np.asarray(desc2), 2) #2
cv2.error: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.2/modules/flann/src/miniflann.cpp:299: error: (-210) type=6
 in function buildIndex_

How I could use knnMatch correctly? Is it a Bug?

Sergio
  • 780
  • 1
  • 9
  • 28

2 Answers2

18

I solved this problem using the correct data type with the function np.asarray()

raw_matches=matcher.knnMatch(np.asarray(desc1,np.float32),np.asarray(desc2,np.float32), 2) #2
Sergio
  • 780
  • 1
  • 9
  • 28
0

See the answer to this question.

Here is the relevant code, from Esteban Angee's answer:

r_threshold = 0.6
FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing

Construct your parameters dictionary:

flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 4)
flann = cv2.flann_Index(desc2, flann_params)

Perform nearest neighbours search:

idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
mask = dist[:,0] / dist[:,1] < r_threshold
idx1 = np.arange(len(desc1))
pairs = np.int32( zip(idx1, idx2[:,0]) )

Returns the descriptors that matched:

return pairs[mask]

I am not at my workstation right now, so I'm afraid I can't look at what's wrong with your code, but the above question solved all my troubles when I had the same problem. You do not have to use FlannBasedMatcher, I remember that I also had problems with it.

If it does not help, I will see if I can find my solution tomorrow or so.

Community
  • 1
  • 1
hjweide
  • 11,893
  • 9
  • 45
  • 49
  • I have the same mistake but in this case using idx2=cv2.flann_Index()idx2.build(np.asarray(desc2),flann_params) cv2.error: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.2/modules/flann/src/miniflann.cpp:299: error: (-210) type=6 in function buildIndex_ – Sergio Sep 21 '12 at 11:55
  • Did you try the code I posted in my answer? It worked for me. I'm afraid I don't really understand your comment? – hjweide Sep 21 '12 at 12:10
  • Yes I'm using OpenCV-2.4.2. When I tried your code cv2.flann_Index(desc2, flann_params) throwed an exception about no module. I read that you need to code the following functions: idx2=cv2.flann_Index()idx2.build(np.asarray(desc2),flann_params) – Sergio Sep 21 '12 at 13:18
  • Maybe you are working with a different setup, I did not need to code any additional functions. My setup is Python 2.7 with OpenCV 2.4 on Windows 7. – hjweide Sep 21 '12 at 14:16
  • I have Mac OS. I followed these steps to install opencv with python. http://stackoverflow.com/a/6087778/577909 – Sergio Sep 21 '12 at 14:57