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

时间:2022-11-11
本文介绍了模块'对象没有属性'drawMatches'opencv python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I am just doing an example of feature detection in OpenCV. This example is shown below. It is giving me the following error

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

我检查了 OpenCV 文档,但不确定为什么会出现此错误.有谁知道为什么?

I have checked the OpenCV Docs and am not sure why I'm getting this error. Does anyone know why?

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++ 目前.

The drawMatches Function is not part of the Python interface.
As you can see in the docs, it is only defined for C++ at the moment.

文档摘录:

 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 接口,你会发现类似这样的内容:

If the function had a Python interface, you would find something like this:

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

编辑

实际上有一个 commit 介绍了这个函数.但是,它(还没有)在官方文档中.
确保您使用的是最新的 OpenCV 版本 (2.4.7).为了完整起见,OpenCV 3.0.0 的函数接口看起来像 这个:

There actually was a commit that introduced this function 5 months ago. However, it is not (yet) in the official documentation.
Make sure you are using the newest OpenCV Version (2.4.7). For sake of completeness the Functions interface for OpenCV 3.0.0 will looks like this:

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

这篇关于模块'对象没有属性'drawMatches'opencv python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:笔画宽度变换 (SWT) 实现 (Python) 下一篇:如何在 HoughLinesP 之后合并行?

相关文章

最新文章