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

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

当我 运行 我的 python 代码

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()

来自这一行

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

我收到这个错误

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

我正在使用 python3 和 opencv3

你好像是 following this tutorial page (based on the code you've shown in this and your two related questions 1, ).

python 绑定正在调用的 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 没有 outImg 的默认值,因此需要提供该参数。

请注意,您可以通过查看 <function_name>.__doc__ 检查 python3 解释器(如果存在)中实际绑定的文档字符串。在这种情况下,您可以看到 outImg 而不是 显示为可选。这是我的安装输出:

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

解决方案(注意 - 在 windows 安装上验证,而不是 Linux)

您可能会注意到 last example on that tutorial,它使用以下代码 - 在 outImg 的位置传入 None。我认为这也适用于您的情况。

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)

你不需要传递所有的 draw_params 字典,你可以尝试只传递 flags

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

我已经在全新安装的 OpenCV 3 上验证了这一点(尽管是在 Windows 上,使用预构建的二进制文件)

好吧,我是个新手,经过数小时的在线研究后学到了很多东西它似乎是一个错误的 BUG,称为 Error (-255) NumpyAllocator ,许多网站会建议你打开 cv2.cpp 文件并注释掉第 163 行代码,我的建议是如果您使用的是 OpenCV 3.1 下载等级到 OpenCV 3.0.0

这个错误似乎在 OpenCV 3.1 中,此外,OpenCV.org 上记录的使用 ORB 算法的代码有点过时了 它指出的地方 enter code here#启动ORB检测器 enter code hereorb = cv2.ORB() # 注意你会收到一个错误,因为现在 enter code here 更改为: enter code hereorb = cv2.ORB_create()

这是我在 Windows 10 上使用 OpenCV 3.0.0 的代码示例:

  # 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()

我希望这对您有所帮助,我喜欢 stack Over flow 它是 Internet 上最好的资源。

这可能是一个错误。您可以做的是将第 6 个参数作为 None.

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

我在 experimenting with SIFT 时遇到了类似的问题。当我使用 None 作为参数时,我能够解决它。

我的代码:img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, flags=2)

在此代码、关键字和参数之后工作="None":img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches ,None, flags=2)