pythonopencvsiftsurf

Can't use SURF, SIFT in OpenCV


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.


Solution

  • 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.