SIFT OpenCV 官方文档:

https://docs.opencv.org/master/da/df5/tutorial_py_sift_intro.html

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_sift_intro/py_sift_intro.html



AttributeError: module 'cv2' has no attribute 'SIFT'

在用python使用opencv进行SIFT时候,编译出现这样问题:


Traceback (most recent call last):   File "F:\image\test.py", line 7, in <module>     sift = cv2.SIFT()



出错代码如下:


import cv2 import numpy as np  img = cv2.imread('home.jpg') gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  sift = cv2.SIFT() kp = sift.detect(gray,None)  img=cv2.drawKeypoints(gray,kp)



原因:opencv将SIFT等算法整合到xfeatures2d集合里面了。变更后写法如下:



import cv2 import numpy as np   img = cv2.imread('home.jpg') gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)   sift = cv2.xfeatures2d.SIFT_create() kp = sift.detect(gray,None)   img=cv2.drawKeypoints(gray,kp,img) cv2.imwrite('sift_keypoints.jpg',img)



然后又出现错误:


Traceback (most recent call last):   File "F:\image\test.py", line 7, in <module>     sift = cv2.xfeatures2d.SIFT_create() AttributeError: module 'cv2' has no attribute 'xfeatures2d'



再尝试解决:

出错原因:

**原因:**3.X以后OpenCv只包含部分内容,需要神经网络或者其他的函数需要导入opencv_contrib

解决方法:

pip install opencv-contrib-python

注意: 

如果已经安装OpenCv2,则需要先卸载​​pip uninstall opencv-python​​再安装

参考资料:​​https://pypi.python.org/pypi/opencv-contrib-python​


再次出错:


cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207:
error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration;
Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'cv::xfeatures2d::SIFT::create'




问题待解决。。。


------------------------------

感谢,按照一楼大神的方法,问题已解决。

版本的问题,直接在终端pip install opencv-contrib-python和你一样的问题,后来重新装了opencv-contrib-python 3.3版本,可以正常使用。

具体来说就是

第一步,卸载原有的opencv-contrib-python

pip3 uninstall opencv-contrib-python(我使用的是python3.6.5)

如果你使用的是python2的版本,使用pip uninstall opencv-contrib-python

第二步,安装新的版本

pip3 install --user opencv-contrib-python==3.3.0.10我使用的是python3.6.5)

如果你使用的是python2的版本,使用pip install --user opencv-contrib-python==3.3.0.10。

如果出现了readtimeouterror就关掉terminal重新输入几次。因为下载源在外网,可能不稳定。