模块“对象没有属性'drawMatches'opencv python [英] module' object has no attribute 'drawMatches' opencv python

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

问题描述

我只是在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

#启动SIFT检测器
orb = cv2.ORB()

#使用SIFT查找关键点和描述符
kp1,des1 = orb.detectAndCompute(img1,None)
kp2,des2 = orb.detectAndCompute(img2,None)

#create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck = True)

#匹配描述符。
matches = bf.match(des1,des2)

#绘制前10个匹配。
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches [:10],flags = 2)

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

错误:

  Traceback(最近一次调用):
在< module>中的文件match.py​​,第22行。
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches [:10],flags = 2)
AttributeError:'module'object没有属性'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& & matchesMask = vector< char>(),int flags = DrawMatchesFlags :: DEFAULT)
C ++:void drawMatches(const Mat& img1,const vector< KeyPoint> keypoints1,const Mat& img2,const vector< KeyPoint>关键点2,const向量<向量< 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,[...])



EDIT



实际上是5个月前推出此功能的提交。但是,它尚未在官方文档中。

确保您使用的是最新的OpenCV版本(2.4.7)。
为了完整起见,OpenCV 3.0.0的函数接口看起来像

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


I am just doing a example of feature detection in OpenCV, I have just taken a example as given below. It is giving me a error like module' object has no attribute 'drawMatches' I have checked the OpenCV Docs. Why do i get this error ?

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

Error:

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'

解决方案

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.

Excerpt from the docs:

 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 )

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

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

EDIT

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆