The Case of OpenCV and the Missing SURF

I have been wanting to have a play with the OpenCV computer vision framework on Python for a while and finally got some time to some experimenting, I am looking to have the computer recognise LEGO parts, after much research and mucking about it seems I should be using cv2.SURF and/or cv2.SIFT for what I want to do. However on Fedora 19 these are not included in the distribution RPM as they are nonfree in that they are not open source. Attempts to use SIFT or SURF result in the following error:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
2.4.6.1
>>> i = cv2.SURF()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
>>>
This is annoying as OpenCV will now need to be re-installed the old fashioned way, but there are instructions on the OpenCV site and I will be using them here with additional information I have discovered while following them.

Change to root and add the rpmfusion.org free and nonfree repositories as ffmpeg and libv41 are unavailble from Fedoras, followed by an update (it seems that Fedora 19 always has something to update):

& sudo su
# yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
# yum update

Remove the existing OpenCV packages:

# yum erase opencv*

There are a few packages to install in preparation to compiling the code, you may have some of all of these already but yum will skip those. Install the mandatory packages, these are for compiling the source and using GTK for the GUI:

# yum install cmake python-devel numpy gcc gcc-c++ gtk2-devel libdc1394-devel libv4l-devel ffmpeg-devel gstreamer-plugins-base-devel git wget

Install the optional dependencies, I have gone for the install everything approach, they may be useful later:

# yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel tbb-devel eigen3-devel python-sphinx texlive

Now, we are ready to get the latest source, here there are two ways to do this, install the latest development version using GIT, or download the latest stable version. My preference is to use the latest stable version.

With GIT, exit back to yourself from root, change to your home directory and download OpenCV:

# exit
$ cd ~
$ git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
$ cd opencv
$ mkdir build
$ cd build

Or using the latest build, at time of writing this is v2.4.7. Get this via the downloads page and save it to your home directory:

# exit
$ cd ~
$ tar -zxvf opencv-2.4.7.tar.gz
$ cd opencv-2.4.7
$ mkdir build
$ cd build

Now configure:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_V4L=ON D WITH_OPENGL=ON -D WITH_OPENCL=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

When complete you should check that your options agree with those displayed. There are many others available. Support for other programming languages may be missed out if they are not already installed. In my case those for Java, if I were to try OpenCV in Java I would need to install the appropriate Java packages using yum, then recompile OpenCV.

Now build and install, this can take a while:

$ make
$ sudo make install

You now need to move the module to anywhere on the Python Path, to find this:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

The directory /usr/lib/python2.7/site-packages looks suitable:

$ sudo mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages

And add your new installation to the PYTHONPATH, and add the export to the end of your .bashrc so it survives a reboot:

$ export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
$ echo export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages >> ~/.bashrc

Now to test:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
2.4.7
>>> i = cv2.SURF()
>>>

Sorted. Now, perhaps, I can get on with what I actually want to do….

Sources:

2 thoughts on “The Case of OpenCV and the Missing SURF”

  1. When i entered make i got this error

    [ 17%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
    Linking CXX shared library ../../lib/libopencv_videoio.so
    /lib/libbz2.so.1: error adding symbols: File in wrong format
    collect2: error: ld returned 1 exit status
    modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:271: recipe for target ‘lib/libopencv_videoio.so.3.0.0’ failed
    make[2]: *** [lib/libopencv_videoio.so.3.0.0] Error 1
    CMakeFiles/Makefile2:3997: recipe for target ‘modules/videoio/CMakeFiles/opencv_videoio.dir/all’ failed
    make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
    Makefile:137: recipe for target ‘all’ failed
    make: *** [all] Error 2

    what do i do?

    1. It looks like you are using opencv version 3, I do not know the requirements for that. Try installing version 2.4 as it says in the posting.

Leave a Reply

Your email address will not be published. Required fields are marked *