8

When I tried to use drawMatchesKnn function as mentioned in this tutorial for FLANN feature matching, I get the following error

AttributeError: 'module' object has no attribute 'drawMatchesKnn'

I checked with other resources that drawMatchesKnn method is present in opencv.

Why am I getting this error?

Thanks in advance

rohangulati
  • 251
  • 1
  • 4
  • 12
  • OpenCV Version : 2.4.7 – rohangulati Nov 24 '13 at 09:32
  • 1
    Use opencv version 3.x. Build from source in master branch – Abid Rahman K Nov 24 '13 at 12:40
  • `IMP - This tutorial is meant for OpenCV 3x version. Not OpenCV 2x`, it says clearly on the README page. You didn't read that? – bad_keypoints Apr 14 '15 at 08:49
  • 1
    @bad_keypoints actually the tutorials don't work in 2x or 3x. The tutorials are filled with calls like this `cv2.SIFT()`that don't exist in 3x (needs to be compiled with contrilbs and callled like `cv2.xfeatures2d.SIFT_create()`, so the tutorials are just a mess of old and new and few really work without changes. – tiagosilva Feb 11 '16 at 16:50

3 Answers3

5

The functions cv2.drawMatches and cv2.drawMatchesKnn are not available in newer versions of OpenCV 2.4. @rayryeng provided a lightweight alternative which works as is for the output of DescriptorMatcher.match. The difference with DescriptorMatcher.knnMatch is that the matches are returned as a list of lists. To use the @rayryeng alternative, the matches must be extracted into a 1-D list.

For example, the Brute-Force Matching with SIFT Descriptors and Ratio Test tutorial could be amended as such:

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
       # Removed the brackets around m 
       good.append(m)

# Invoke @rayryeng's drawMatches alternative, note it requires grayscale images
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
drawMatches(gray1,kp1,gray2,kp2,good)
Community
  • 1
  • 1
ryanmeasel
  • 246
  • 4
  • 7
  • 1
    Just wanted to say thank you for this. I've added this to my original post to make it complete and I've linked you in my answer. – rayryeng Dec 04 '16 at 07:11
0

You need to use OpenCV version 3. drawMatchesKnn() is present in 3.0.0-alpha but not in 2.4.11

That error is there, because you are using an old version of OpenCV.

mirosval
  • 6,671
  • 3
  • 32
  • 46
-2

Instead of doing good.append(m) try good.append([m])

blackgreen
  • 34,072
  • 23
  • 111
  • 129