Can't use SURF, SIFT in OpenCV

147,770

Solution 1

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:

  1. Download opencv-2.4.6.1.tar.gz from opencv.org.
  2. Extract the source:

    tar -xf opencv-2.4.6.1.tar.gz -C /tmp
    
  3. 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
    
  4. Build and install:

    cd /tmp/opencv-2.4.6.1
    make -j4
    make install
    
  5. 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.

Solution 2

There is a pip source that makes this very easy.

  1. If you have another version of opencv-python installed use this command to remove it to avoid conflicts:

    pip uninstall opencv-python
    
  2. Then install the contrib version with this:

    pip install opencv-contrib-python
    
  3. SIFT usage:

    import cv2
    sift = cv2.xfeatures2d.SIFT_create()
    

Solution 3

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.

Solution 4

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.

Solution 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!

Share:
147,770
Linda
Author by

Linda

Updated on July 25, 2022

Comments

  • Linda
    Linda almost 2 years

    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.

  • Linda
    Linda over 10 years
    I've tried this out but got still the same Error. Is it possible to delete opencv fully and will it help?!
  • Mårten W
    Mårten W over 10 years
    Is it the new or old cv2 that is loaded? What does print cv2.__version__ say?
  • Linda
    Linda over 10 years
    print cv2.__version__ says $Rev: 4557 $
  • Mårten W
    Mårten W over 10 years
    @Linda: Then it loads the old one. Restart python, and try import os,sys, followed by os.chdir(os.path.expanduser('~/.opencv-2.4.6/lib')) and sys.path.append(os.path.expanduser('~/.opencv-2.4.6.1/lib/py‌​thon2.7/dist-package‌​s')), and then check which version it loads. It should be the new one.
  • Linda
    Linda over 10 years
    well its working now!!! Thank you (but its still $Rev: 4557 $ !!! And do i always have to put this lines into my code? Is there a possible way to fix it for ever ? BTW: its os.chdir(os.path.expanduser('~/.opencv-2.4.6.1/lib'))
  • Mårten W
    Mårten W over 10 years
    @Linda: All I have to do is to set PYTHONPATH before i start python (i do this in my .bashrc, so it is done on login), and then I can import cv2. I dont have the old and broken opencv though, and it feels like your system is confused as to which to load. If you have sudo rights you could try removing it (you can always reinstall it if you want it back); as far as I understand, the command sudo apt-get remove libopencv-* libcv-* should be enough for this.
  • Mårten W
    Mårten W almost 10 years
    Please note that not everyone has write access outside their home directory (this is especially true on campus computers). Installing at /usr/local/lib typically would require sudo rights.
  • David DeWert
    David DeWert over 8 years
    This worked for me as well on Ubuntu 14.04. I can now use cv2.sift in Python.
  • lysdexia
    lysdexia over 8 years
    cv2.xfeatures2d.SIFT_create() <-- thanks for pointing that out.
  • jAYANT YADAV
    jAYANT YADAV over 7 years
    what about cv2.DescriptorExtractor_create("SIFT")? What will be its substitute?
  • piepi
    piepi over 6 years
    @lysdexia How did you get ` cv2.xfeatures2d.SIFT_create()` working? It is still giving me AttributeError: 'module' object has no attribute 'xfeatures2d' . How did you install opencv and opencv_contriv? I did pip install python-opencv and next steps from the page linked in the OP.
  • piepi
    piepi over 6 years
    This worked like a charm. Don't know why this is not upvoted.
  • piepi
    piepi over 6 years
    This seems a rather lengthy process, on the other hand doing pip install python-opencv and pip install opencv-contrib-python solves the issues immediately.
  • Michael Brown
    Michael Brown over 6 years
    I too don't see xfeatures2d as available in the current version of OpenCV
  • King Alawaka
    King Alawaka about 6 years
    I installed an config referring this learnopencv.com/install-opencv3-on-ubuntu and change to this cv2.xfeatures2d.SIFT_create() do the trick :D
  • Darrel Holt
    Darrel Holt over 5 years
    You can do pretty much this same process with openCV 3.4.3 (I did 3.4.3.18), but you need to have the opencv-contrib source as well. Add the following flags to the cmake command: -D OPENCV_ENABLE_NON_FREE=ON and -D OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-3.4.3/modu‌​les. This is what I had when building it on raspbian 4.14.70-v7+ (note I have examples installed too): cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.3/modules -D BUILD_EXAMPLES=ON -D OPENCV_ENABLE_NONFREE=ON ..
  • whiteShadow
    whiteShadow over 5 years
    When I do that, I get: 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'. I guess the downloaded version is not compiled with OPENCV_ENABLE_NONFREE
  • A.Ametov
    A.Ametov about 5 years
    I have just settled on the Ubuntu and checked installation steps above with pip3 instead of pip. All works great for version 3.4.2.16. Version 4.0.0.21 restricts using SIFT and requires compilation from the source for using SIFT.
  • Falcon
    Falcon about 5 years
    As of April 2019, the latest version (v4.0.0.21) complains (@whiteShadow pointed out). You can install an older version pip install -U opencv-contrib-python==3.4.0.12 to avoid this.
  • Sau001
    Sau001 over 4 years
    As of Sep 2019, @Falcon's suggestion worked for me. Basically uninstall opencv-python and install the version 3.4.2.16 of opencv-contrib-phython
  • mLstudent33
    mLstudent33 over 4 years
    Thank you sir! This is a huge contribution
  • john ktejik
    john ktejik over 4 years
    This. This is all it takes at least on Windows.
  • Apollo
    Apollo about 4 years
    As of February 2020 this does not work. Lowest possible version is 3.4.8.29, and the same error mentioned by @whiteShadow is thrown.
  • whiteShadow
    whiteShadow about 4 years
    I heard that patent for SIFT patent will expire this month or next month, maybe it will come back in OpenCV.
  • Ali Ihsan Elmas
    Ali Ihsan Elmas about 4 years
    @Apollo found any solution?
  • Apollo
    Apollo about 4 years
    @AliİhsanElmas Yep, there's basically no other way than manually building everything. Check my answer : stackoverflow.com/questions/18561910/…
  • Whois7pi
    Whois7pi almost 4 years
    It worked for me just needed to modify it for python3 in RPI 4. But not working in windows. In windows it's saying that version not found: "python3 -m pip install opencv-contrib-python==3.4.2.16"