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

标签 python image opencv image-processing computer-vision

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

module' 对象没有属性 '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 接口(interface)的一部分。
正如您在 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 接口(interface),您会发现如下内容:
 Python: cv2.drawMatches(img1, keypoints1, [...]) 

编辑

实际上有一个 commit 5个月前引入了这个功能。但是,它(还)不在官方文档中。
确保您使用的是最新的 OpenCV 版本 (2.4.7)。
为了完整起见,OpenCV 3.0.0 的函数接口(interface)看起来像 this :
cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2[, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]]) → outImg

关于python - 模块'对象没有属性 'drawMatches' opencv python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259025/

相关文章:

android - 使用 android.hardware.camera2 设置自定义分辨率和帧率

python - Google Colab不显示图片

python - 删除模板中的空 ModelChoiceField

javascript - AngularJS 模块 'ui.router' 不适用于我的 Djangular 项目

c++ - 使用简单 Blob 检测器时,命名空间 'Blender' stitching.hpp 中没有名为 'cv::detail' 的成员

python - 如何在OpenCV2中将float32图片写入视频文件?

python - 将 swig python 'double *' 对象读入 numpy(也许通过 ctypes?)

css - 如何阻止图像在响应式站点上缩小同时保持其居中

html - CSS - 使图像适合特定大小的 div

python - 加载图像时 OpenCV 显示错误或修改的结果