abidrahmank/OpenCV2-Python-Tutorials

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

Basilisvirus opened this issue · 10 comments

The code:


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)

cv2.imwrite('sift_keypoints.jpg',img)

The error:
AttributeError: 'module' object has no attribute 'SIFT'

Which opencv version is it that you are using? In opencv 3.x, the SIFT and SURF descriptors are part of the xfeatures2d submodule. Try the following to get an instance of a sift descriptor (similar for SURF):

sift = cv.xfeatures2d.SIFT_create()

Ok so i changed the code:

import cv2
import numpy as np

img = cv2.imread('home.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

sift = cv.xfeatures2d.SIFT_create()
kp = sift.detect(gray,None)

img=cv2.drawKeypoints(gray,kp)

cv2.imwrite('sift_keypoints.jpg',img)+

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'

rebuild the library?
I think i had CMake installed, cause i had several errors like this, but once i installed Pycharm IDE, i deleted the Cmake. I never understood what cmake does. Why did they removed the sift anyway?

I solved it by installing the opencv contrib python package

sudo pip3 install opencv-contrib-python

import cv2
sift = cv.xfeatures2d.SIFT_create()

I'm getting same error in opencv==4.1.0 and python3.x when i'm using
sirt = cv2.xfeatures2d.SIFT_create()

Error is
cv2.error: OpenCV(4.1.0) 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'
How can i resolve this?

I'm getting same error in opencv==4.1.0 and python3.x when i'm using
sirt = cv2.xfeatures2d.SIFT_create()

Error is
cv2.error: OpenCV(4.1.0) 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'
How can i resolve this?

You should rebuild openCV sift feature included as it is not free preinstalled.

as far as i can remember, sift is no longer part of opencv. I just re-notices this issue a year later.. Shall i close the issue?

as far as i can remember, sift is no longer part of opencv. I just re-notices this issue a year later.. Shall i close the issue?

Sure. But if you want to re-build opencv with surf - have a look here.

Thanks!

This is my error please guide:
Traceback (most recent call last):
File "C:/Users/Furqan Haider/Desktop/Sensation Home Work/Home Work 2/Python Camera Calibration/Stereo Image/Stereo_Test.py", line 7, in
sift = cv.SIFT()
AttributeError: module 'cv2.cv2' has no attribute 'SIFT'

As i have commented:
"as far as i can remember, sift is no longer part of opencv. I just re-notices this issue a year later.. Shall i close the issue?"
You may not be avle to use SIFT anymore. Look it up a bit more, you may find a way. zoldaten said you can rebuild opencv with surf.