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时候,编译出现这样问题:html
Traceback (most recent call last): File "F:\image\test.py", line 7, in <module> sift = cv2.SIFT()
出错代码以下:python
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)
而后又出现错误:sql
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_contribui
解决方法:this
pip install opencv-contrib-python
注意:
若是已经安装OpenCv2,则须要先卸载pip uninstall opencv-python
再安装url
参考资料:https://pypi.python.org/pypi/opencv-contrib-pythonspa
再次出错:
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'
问题待解决。。。
------------------------------
感谢,按照一楼大神的方法,问题已解决。
“
”
具体来讲就是
第一步,卸载原有的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从新输入几回。由于下载源在外网,可能不稳定。