I’m trying a simple thing like
detector = cv2.SIFT()
and get this bad error
detector = cv2.SIFT() AttributeError: 'module' object has no attribute 'SIFT'
I do not understand that because cv2 is installed.
cv2.__version__
is
$Rev: 4557 $
My system is Ubuntu 12.04.
Maybe someone has got the same problem and could help me.
EDIT:
Long story short, testypypypy.py:
import cv2 detector = cv2.SIFT()
ERROR:
Traceback (most recent call last): File "testypypy.py", line 3, in <module> detector = cv2.SIFT() AttributeError: 'module' object has no attribute 'SIFT
If I take SURF
it works because SURF
is in dir(cv2)
but if I also take cv2.BFMatcher()
I get the same error… So it’s missing and I have to add it but I don’t know how.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There is a pip source that makes this very easy.
-
If you have another version of opencv-python installed use this command to remove it to avoid conflicts:
pip uninstall opencv-python
-
Then install the contrib version with this:
pip install opencv-contrib-python
-
SIFT usage:
import cv2 sift = cv2.xfeatures2d.SIFT_create()
Method 2
For recent information on this issue (as of Sept 2015) consult this page.
Most information on this question here is obsolete.
What pyimagesearch is saying is that SURF/SIFT were moved to opencv_contrib
because of patent issues.
For installation there is also a nice page that tells you how to install opencv with opencv_contrib
and Python support so you get SURF/SIFT.
Notice that the API also changed. Now it’s like this:
sift = cv2.xfeatures2d.SIFT_create()
Before I found the above pages, I also suffered quite a bit. But the pages listed do a very good job of helping with installation and explaining what’s wrong.
Method 3
FYI, as of 3.0.0 SIFT and friends are in a contrib repo located at https://github.com/Itseez/opencv_contrib and are not included with opencv by default.
Method 4
I think this is far from the “correct” way to do it (the “correct” way on Ubuntu seems to be to stick to a broken and/or outdated OpenCV), but for me building opencv-2.4.6.1 from source brings back cv2.SIFT and cv2.SURF.
Steps:
- Download opencv-2.4.6.1.tar.gz from opencv.org.
-
Extract the source:
tar -xf opencv-2.4.6.1.tar.gz -C /tmp
-
Configure the source. This will tell OpenCV to install into .opencv-2.4.6.1 in your home directory:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D CMAKE_INSTALL_PREFIX=~/.opencv-2.4.6.1 /tmp/opencv-2.4.6.1
-
Build and install:
cd /tmp/opencv-2.4.6.1 make -j4 make install
-
Set PYTHONPATH (this works in bash, I have no clue about other shells):
export PYTHONPATH=~/.opencv-2.4.6.1/lib/python2.7/dist-packages
Now if I start python and import cv2 (for me, this produces a gnome-keyring warning), I have cv2.SIFT and cv2.SURF available.
Method 5
for debian users its ‘easy’ to create their own libopencv-nonfree package.
i followed the opencv tutorial for python, but in my debian the SIFT and SURF modules were missing. And there is no non-free package available for debian including SIFT and SURF etc.
They were stripped from the package due to license issues….
i never created a package for debian before (adding a new module etc) but i followed some small steps in the debian tutorials and tried and guessed around a bit, and after 1 day, voila… i got working a libopencv-nonfree2.4 deb package and a python module with correct bindings.
(i dont know if i also needed to install the newly built python-opencv package or only the nonfree… i re-installed both and got a working python opencv library with all necessary nonfree modules!)
ok, here it is:
!this is for libopencv 2.4!
!you can do all steps except installing as a normal user!
we need the built essesntials and some tools from debian repository to compile and create a new package:
sudo apt-get install build-essential fakeroot devscripts
create a directory in your home and change to that directory:
cd ~ && mkdir opencv-debian cd opencv-debian
download the needed packages:
apt-get source libopencv-core2.4
and download all needed dependency packages to build the new opencv
apt-get build-dep libopencv-core2.4
this will download the neeeded sources and create a directory called “opencv-2.4.9.1+dfsg”
change to that directory:
cd opencv-2.4.9.1+dfsg
now you can test if the package will built without modifications by typing:
fakeroot debian/rules binary
this will take a long time!
this step should finish without errors
you now have a lot of .deb packages in your opencv-debian directory
now we make some modifications to the package definition to let debian buld the nonfree modules and package!
change to the opencv-debian directory and download the correct opencv source.. in my case opencv 2.4.9 or so
i got mine from https://github.com/Itseez/opencv/releases
wget https://codeload.github.com/Itseez/opencv/tar.gz/2.4.9
this will download opencv-2.4.9.tar.gz
extract the archive:
tar -xzvf opencv-2.4.9.tar.gz
this will unpack the original source to a directory called opencv-2.4.9
now copy the nonfree modules from original source to the debian source:
cp -rv opencv-2.4.9/modules/nonfree opencv-2.4.9.1+dfsg/modules/
ok, now we have the source of the nonfree modules, but thats not enough for debian… we need to modify 1 file and create a new one
we have to edit the debian control file and add a new section at end of file:
(i use mcedit as an editor here)
mcedit opencv-2.4.9.1+dfsg/debian/control
or use any other editor of your choice
and add this section:
Package: libopencv-nonfree2.4 Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: OpenCV Nonfree Modules like SIFT and SURF This package contains nonfree modules for the OpenCV (Open Computer Vision) library. . The Open Computer Vision Library is a collection of algorithms and sample code for various computer vision problems. The library is compatible with IPL (Intel's Image Processing Library) and, if available, can use IPP (Intel's Integrated Performance Primitives) for better performance. . OpenCV provides low level portable data types and operators, and a set of high level functionalities for video acquisition, image processing and analysis, structural analysis, motion analysis and object tracking, object recognition, camera calibration and 3D reconstruction.
now we create a new file called libopencv-nonfree2.4.install
touch opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install
and edit:
mcedit opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install
and add the following content:
usr/lib/*/libopencv_nonfree.so.*
ok, thats it, now create the packages again:
cd opencv-2.4.9.1+dfsg
first a clean up:
fakeroot debian/rules clean
and build:
fakeroot debian/rules binary
et voila… after a while you have a fresh built and a new package libopencv-nonfree2.4.deb!
now install as root:
dpkg -i libopencv-nonfree2.4.deb dpkg -i python-opencv.deb
and test!
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('test.jpg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) sift = cv2.SIFT() kp = sift.detect(gray,None) img=cv2.drawKeypoints(gray,kp) corners = cv2.goodFeaturesToTrack(gray,16,0.05,10) corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(img,(x,y),90,255,3) plt.imshow(img),plt.show()
have fun!
Method 6
IN opencv3.x SIFT() & SURF() are no longer exist .for this
uninstall all the opencv versions
python -m pip uninstall opencv-python python -m pip uninstall opencv-contrib-python
after that install opencv-contrib to include sift() and surf() using below given command with python(3.x)
python -m pip install opencv-contrib-python==3.4.2.16
then you can use
sift = cv2.xfeatures2d.SIFT_create()
Method 7
As an Anaconda user, I wanted to find one or two appropriate commands to solve the problem. Fortunately, this answer helped. For conda 4.5.11 (use conda -V
to check Anaconda version) I have performed next steps:
# Python version does not matter, most likely, check yourself conda create -n myenv python=3.6 conda activate myenv conda install -c menpo opencv
That will install OpenCV 2.4.11. Anaconda’s another command conda install -c menpo opencv3
will install OpenCV3, but Python has to be downgraded to 2.7. To install OpenCV3 with Python3 use next (due to the first link):
conda create -n myenv python pip install opencv-python==3.4.2.16 pip install opencv-contrib-python==3.4.2.16
Check the SIFT:
conda activate myenv python >>> cv2.xfeatures2d.SIFT_create() <xfeatures2d_SIFT 000002A3478655B0>
Method 8
just change SHIFT
to ORB
,
I think it make occur because of non-relevant version,
ORB is efficient and better alternative of SHIFT or SURF.
As I also face same problem when i was used cv2.SHIFT()
ERROR: AttributeError: ‘module’ object has no attribute ‘SIFT’
Now its completely working for me please try this:
ORB = cv2.ORB()
Method 9
since I had already compiled opencv when I discovered this problem, all I had to do was (from my opencv build directory):
make opencv_nonfree sudo make install
Method 10
This is what worked for me in September 2020:
- Remove previous versions:
pip uninstall opencv-python pip uninstall opencv-contrib-python
- Install this particular version (3.4.2.16 was no longer available)
pip install opencv-python==3.4.2.17 pip install opencv-contrib-python==3.4.2.17
- Create the detectors with:
import cv2 sift = cv2.xfeatures2d.SIFT_create() surf = cv2.xfeatures2d.SURF_create()
Method 11
Install Python opencv
pip install opencv-python
and instead of using ..
cv2.SIFT()
Use
cv2.SIFT_create()
working code using opencv-python below
import cv2 img1 = cv2.imread('yourimg.png',0) sift = cv2.SIFT_create() kp1, des1 = sift.detectAndCompute(img1,None) #keypoint and descriptors ...
you can also install “opencv-contrib-python” and use “cv2.xfeatures2d.SIFT_create()” but that is secondary and up to you..
working code using the python package opencv-contrib-python
import cv2 img1 = cv2.imread('yourimg.png',0) sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(img1,None) #keypoint and descriptors
Thanks
Method 12
- Install OpenCV-Contrib
-
import cv2
sift = cv2.xfeatures2d.SIFT_create() -
sift.something()
This is the easy way to install the Contrib.
Method 13
Just change the version of opencv to 3.4.2.16 .Since it is patented it is not available in newer version.
Method 14
You can proceed in this way. It must work for you as well!
Step 1:
virtualenv venv source venv/bin/activate
Step 2:
sudo python3 -m pip install opencv-python==3.4.2.16 sudo python3 -m pip install opencv-contrib-python==3.4.2.16
Step 3:
import cv2 sift = cv2.xfeatures2d.SIFT_create()
Don’t use cv2.SIFT() . It will raise an exception.
Method 15
Follow this installation operation
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
using this command will install library to your /usr/local/lib.
Method 16
It is now back to normal opencv, thanks to the expiration of the patent!
Check this:
https://docs.opencv.org/4.5.4/da/df5/tutorial_py_sift_intro.html
As long as your opencv version is 4.4.0+!
Method 17
The approach suggested by vizzy also works with OpenCV 2.4.8, as when building the non-free package under Ubuntu 14.04 LTS.
This dependency issue may prevent installation of the non-free package:
libopencv-nonfree2.4 depends on libopencv-ocl2.4; however: Package libopencv-ocl2.4 is not installed.
Easily fixable because the missing package can be installed from the ones just built:
dpkg -i libopencv-ocl2.4_2.4.8+dfsg1-2ubuntu1_amd64.deb
After that the install proceeds as explained in vizzy’s answer.
Method 18
None of the above suggested solutions worked for me. I use Anaconda and found that opencv version 3.3.1 still had Sift enabled. If you want to test in isolated conda environment, try the following inspired from @A.Ametov’s answer above
conda create -n testenv opencv=3.3.1 conda activate testenv conda activate myenv python #Check version of opencv being used >>> import cv2 >>> cv2.__version__ #Check if Sift is available >>> cv2.xfeatures2d.SIFT_create() <xfeatures2d_SIFT 000002A3478655B0>
Method 19
Change this:
sift = cv2.xfeatures2d.SIFT_create()
By this:
cv2.ORB_create()
Method 20
The following page provides a relatively good guide that requires few corrections : https://cv-tricks.com/how-to/installation-of-opencv-4-1-0-in-windows-10-from-source/
On step 8, when you are choosing generator for the project (tool used for building), don’t forget to specify x64 in the second field if you need it. If you don’t, you will get LNK1112 error, which is a linker error caused if one module is created with the x64 compiler, and another module is created with the x86 compiler.
Next, when choosing flags in step 9, don’t forget to tick the “OPENCV_ENABLE_NONFREE”, and set the path for flag “OPENCV_EXTRA_MODULES_PATH” flag. The path must be set to “modules” folder in opencv-contrib-python.
So you need both opencv, and opencv-contrib-python in order to use SIFT and SURF.
Method 21
As per June 2020, I suppose this version (pip install -U opencv-contrib-python==3.4.2.16
) is still working. so install it and enjoy.
Method 22
try this
!pip install opencv-contrib-python==4.4.0.44
sift = cv2.SIFT_create()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0