当前位置: 首页 > 面试题库 >

模块'对象没有属性'drawMatches'opencv python

胡鸿羲
2023-03-14
问题内容

我只是在做一个OpenCV中特征检测的例子。该示例如下所示。它给我以下错误

模块”对象没有属性“ drawMatches”

我已经检查了OpenCV文档,但不确定为什么会出现此错误。有人知道为什么吗?

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

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

# Initiate SIFT detector
orb = cv2.ORB()

# find the keypoints and descriptors with SIFT
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)

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

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

错误:

Traceback (most recent call last):
File "match.py", line 22, in <module>
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)
AttributeError: 'module' object has no attribute 'drawMatches'

问题答案:

drawMatches函数不是Python界面的一部分。
正如您在docs中看到的那样,它仅C++在当前定义。

摘录自文档:

 C++: void drawMatches(const Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<char>& matchesMask=vector<char>(), int flags=DrawMatchesFlags::DEFAULT )
 C++: void drawMatches(const Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<vector<DMatch>>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<vector<char>>& matchesMask=vector<vector<char> >(), int flags=DrawMatchesFlags::DEFAULT )

如果该函数具有Python接口,则会发现以下内容:

 Python: cv2.drawMatches(img1, keypoints1, [...])

编辑

实际上,有一个提交在5个月前引入了此功能。但是,官方文档中尚未(尚未)。
确保您使用的是最新的OpenCV版本(2.4.7)。为了完整起见,OpenCV
3.0.0的Functions接口看起来像这样:

cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2[, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]]) → outImg


 类似资料:
  • 我正在尝试使用Tensorflow在谷歌云上运行一个培训工作。我试图通过运行以下命令来运行培训。 但是当我运行一个作业时,我得到了以下错误。知道为什么吗?

  • 问题内容: 我正在尝试使用Python下载网站的HTML源代码,但收到此错误。 我在这里遵循指南:http : //www.boddie.org.uk/python/HTML.html 我正在使用Python 3。 问题答案: 这适用于Python2.x。 对于Python 3,请在docs中查看:

  • 变得非常困惑。四处寻找,但找不到任何有用的帮助。我知道错误了 回溯(最近一次调用):文件“/Users/Andrew/Desktop/password.py”,第2行,格式为cgi。Fieldstorage()AttributeError:“模块”对象没有属性“Fieldstorage”

  • 问题内容: 我正在尝试运行一个简单的代码,并且在Canopy中安装了matplotlib和numpy的所有依赖项。还是我出错了。 错误: 我已经为numpy和matplotlib安装了依赖项:1.)libsvm-3.17.win64-py2.7 2.)pyparsing-2.0.3-1.win64-py2.7 3.)python-dateutil-2.4.2-2。 win64-py2.7 4.)p

  • 问题内容: 我正在尝试使用Selenium WebDriver生成Firefox实例。过去,在安装geckodriver并确保它位于我的PATH之后,我能够执行此操作。但是,我切换到使用phantomjs已有大约一年的时间,直到最近才决定再次试用Firefox。不幸的是,现在当我尝试实例化webdriver.Firefox对象时,我得到了AttributeError,称该对象没有称为“ Firef

  • 问题内容: 尝试运行python脚本时,出现错误。我正在使用Python 2.6。 完整错误: 问题答案: 我认为无论您要运行什么,都期望将其与python的特殊调试版本一起使用。通常在sys模块的标准发行版中找不到,并且我相信如果您构建调试python,它将在那里: http://docs.python.org/c-api/intro.html#debugging- builds 这可能也可能是