Python 类型错误:找不到必需的参数“outImg”(位置 6)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31631352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:19:27  来源:igfitidea点击:

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

pythonopencvopencv3.0

提问by shar

When I run my python code

当我运行我的 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()

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

我正在使用 python3 和 opencv3

采纳答案by J Richard Snape

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

您似乎正在关注本教程页面(基于您在本教程中显示的代码以及您的两个相关问题12)。

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

函数文档是在这里(虽然我注意到它仍然是标有“测试版”),并暗示outImg是可选的。但是,python 错误消息是明确的 - 位置 6 需要一个参数,它outImg在函数签名中命名。我怀疑文档可能与代码要求不完全匹配。它显示了的签名C ++代码,该蟒结合正在呼叫具有用于没有缺省值outImg,所以需要将要提供的参数。

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 outImgis notshown as optional. Here is the output from my installation:

请注意,您可以通过查看<function_name>.__doc__. 在这种情况下,你可以看到,outImg不是显示为可选。这是我安装的输出:

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

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

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

您可能会注意到该教程中最后一个示例,它使用以下代码来None代替outImg。我认为这也适用于您的情况。

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_paramsdict, you could try just passing flagsi.e.

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

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)

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

回答by Wazzie

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

好吧,伙计们,我也是一个新手,经过数小时的在线研究后学到了很多东西,它似乎是一个错误,称为 Error (-255) NumpyAllocator ,许多站点会建议您打开 cv2.cpp 文件并注释掉该行163 代码,我的建议是如果您使用 OpenCV 3.1 下载等级到 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()

该错误似乎在 OpenCV 3.1 中,除此之外,OpenCV.org 上记录的使用 ORB 算法的代码有点过时了#Initiate enter code hereORB enter code heredetection orb = cv2.ORB() # 注意你会得到一个错误因为这现在已enter code here更改为: enter code hereorb = cv2.ORB_create()

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

这是我在 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()

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

我希望这会有所帮助,我喜欢堆栈溢出,它是互联网上最好的资源。

回答by ymdatta

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

这可能是一个错误。您可以做的是将第 6 个参数作为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 Noneas an argument.

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

回答by kcmn

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

我的代码: 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)

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