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從新輸入幾回。由於下載源在外網,可能不穩定。