49

When I run my python code

import numpy as np
import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('/home/shar/home.jpg',0)          # queryImage
img2 = cv2.imread('/home/shar/home2.jpg',0) # trainImage

# Initiate SIFT detector

sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# 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:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)


plt.imshow(img3),plt.show()

From this line

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)

I get this error

TypeError: Required argument 'outImg' (pos 6) not found

I am using python3 and opencv3

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
shar
  • 1,233
  • 6
  • 20
  • 30
  • I imagine outImg should be a numpy array – Padraic Cunningham Jul 25 '15 at 21:52
  • its signature is `cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2[, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]])` so I presume if you specify `flags` you need to specify all the previous params as well – Pynchia Jul 25 '15 at 22:24
  • 2
    I think that documentation is wrong / misleading. I've created an [issue on opencv github](https://github.com/Itseez/opencv/issues/4237) based on the problems found by OP. – J Richard Snape Jul 26 '15 at 00:29
  • A side note - the factory method `cv2.BFMatcher_create()` has replaced the default `cv2.BFMatcher()`, [according to the official documentation](https://docs.opencv.org/4.1.0/d3/da1/classcv_1_1BFMatcher.html). – Zhanwen Chen Apr 30 '19 at 18:08

4 Answers4

82

You seem to be following this tutorial page (based on the code you've shown in this and your two related questions 1, 2).

The function documentation is here (although I note it is still labelled "beta") and implies that outImg is optional. However, the python error message is explicit - an argument is required in position 6, it is named outImg in the function signature. I suspect the documentation may not exactly match the code requirements. It appears that the signature of the C++ code that the python binding is calling has no default value for outImg, so need that argument to be supplied.

Note that you can inspect the doc string for the actual binding in the python3 interpreter (if it exists) by looking at <function_name>.__doc__. In this case, you can see that outImg is not shown as optional. Here is the output from my installation:

>>> cv2.drawMatchesKnn.__doc__
'drawMatchesKnn(img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchC
olor[, singlePointColor[, matchesMask[, flags]]]]) -> outImg'

Solution (note - verified on a windows install, not Linux)

You might note the last example on that tutorial, which uses the following code - passing in None in the place of outImg. I think that will work for your case also.

draw_params = dict(matchColor = (0,255,0),
                   singlePointColor = (255,0,0),
                   matchesMask = matchesMask,
                   flags = 0)

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

You don't need to pass all the draw_params dict, you could try just passing flags i.e.

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,flags=2)

I have verified this on a fresh install of OpenCV 3 (albeit on Windows, using a prebuilt binary)

Community
  • 1
  • 1
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • NameError: name 'matchesMask' is not defined – shar Jul 26 '15 at 20:43
  • 1
    OK - that's a bit terse, To be honest. If you try to use the first version of the code - i.e. using `draw_params` then of course you have to define your own `matchesMask`. However, immediately under that I give an option that does not require you to use `draw_params`. Replace your current call to `drawMatchesKnn()` with the very last line in my answer - ignore the quoted code from the tutorial beginning `draw_params`. I included that for completeness, but realise it may be confusing. See also the example I gave in answering your other question. – J Richard Snape Jul 26 '15 at 20:46
  • 1
    Verified on Linux Mint 17.3 Cinnamon with OpenCV 2.4.9 Lifesaver, @JRichardSnape – Kanishka Ganguly Jun 23 '16 at 22:57
  • 1
    Validated with macOS 10.12 Sierra and OpenCV 3.2.0. Works! – maephisto May 19 '17 at 12:25
  • Recieve this error: SystemError: returned NULL without setting an error – john Jul 20 '18 at 00:15
17

Okay guys , I am too a newbie and learning a lot after hours of research online it appears to be a BUG on a error know as Error (-255) NumpyAllocator , many site will suggest you open cv2.cpp file and comment out the line 163 code , my suggestion is if you are using OpenCV 3.1 download grade to OpenCV 3.0.0

the bug seems to be within OpenCV 3.1 in addition to this the code for using ORB Algorithm which is documented on OpenCV.org is a bit outdated where it states enter code here# Initiate ORB detector enter code hereorb = cv2.ORB() # note you will get a error as this has now enter code herechange to : enter code hereorb = cv2.ORB_create()

Here is my example of the code using OpenCV 3.0.0 on Windows 10 :

  # Example of Brute Force matching base on ORB Algorithm
  #Modify Author : Waheed Rafiq R&D student Birmingham City University UK
  #Original author : OpenCV.org
  #Date Updated : 21/04/2016 : 13:45 

  import numpy as np
  import cv2
  from matplotlib import pyplot as plt 

  img1 = cv2.imread('wr-pb.jpg',0)          # queryImage
  img2 = cv2.imread('Waheed.jpg',0) # trainImage

  # Initiate ORB detector
  orb = cv2.ORB_create()

  # find the keypoints and descriptors with ORB
  kp1, des1 = orb.detectAndCompute(img1,None)
  kp2, des2 = orb.detectAndCompute(img2,None)

  # create BFMatcher object
  bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

 # Match descriptors.
 matches = bf.match(des1,des2)

 # Sort them in the order of their distance.
 matches = sorted(matches, key = lambda x:x.distance)

 # Draw first 10 matches.
 img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches ,None, flags=2)

 plt.imshow(img3),plt.show()

I hope this helps , I love stack Over flow its the best resource out on internet.

Waheed Rafiq
  • 460
  • 1
  • 6
  • 17
3

My code: img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, flags=2)

worked after this code, keyword and parameter="None": img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches ,None, flags=2)

kcmn
  • 31
  • 2
0

This is probably a bug. What you can do is you can pass the 6th argument as None.

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2,None)

I was having a similar kind of problem when experimenting with SIFT. I was able to solve it when I used None as an argument.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ymdatta
  • 80
  • 7