Installing OpenCV

OpenCV is a library of programming functions for real time computer vision, further details here. This is a tutorial for building OpenCV from source on a Linux operating system without having root permissions. We will also build git, cmake, python, libtiff and fix a lot of common misbehaviours.

Requirements already met by operating system: ftp, g++, build-essential, libgtk2.0, libgtk2.0-dev, libjpeg (optional), libjasper (optional), libpng (optional)
Unmet requirements: cmake, python2.5 with python-dev (only had 2.4 on my machine), libtiff (optional)

We would also need a git client to clone a repository.

Let’s suppose we are going to install everything under /home/me/local, a folder in our home dir, where we have all the rights.

# download source archive for git
$ wget http://www.kernel.org/pub/software/scm/git/git-1.7.3.tar.gz
# extract it
$ tar zxvf git-1.7.3.tar.gz
$ cd git-1.7.3
# configure install scripts (Makefile-s), setting the install destination
# where we have full rights
$ ./configure --prefix=/home/me/local
# build the binaries and libraries
$ make
# and place them at destination
$ make install
# git is now installed and available using /home/me/local/bin/git
$ cd ..

Next stop: cmake.

$ wget http://www.cmake.org/files/v2.8/cmake-2.8.3.tar.gz
$ tar zxvf cmake-2.8.3.tar.gz
$ cd cmake-2.8.3.tar.gz
$ ./bootstrap --prefix=`pwd`/../local/
# `pwd` will get expanded to present working directory, it's a shortcut trick :)
$ gmake
$ make install
$ cd ..

I’d like to use my application with some tiff input data, so I’ll need to install libtiff. The source archive is available on an ftp server.

$ ftp ftp.remotesensing.org
ftp> User: anonymouse
ftp> Password: [your email address]
ftp> cd libtiff
ftp> get tiff-3.9.4.tar.gz
ftp> Ctrl+D
$ tar tiff-3.9.4.tar.gz
$ cd tiff-3.9.4
$ ./configure  --prefix=`pwd`/../local
$ make; make install; cd ..

The final part we need to do is install a newer Python.

$ wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
$ tar zxvf Python-2.6.6.tgz
$ cd Python-2.6.6
# i prefer putting python in a sperate subdir:
$ ./configure --prefix=`pwd`/../local/python
$ make; make install
$ cd ..

All done now. Let’s compile OpenCV!

$ wget http://fastbull.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.1/OpenCV-2.1.0.tar.bz2
$ tar jxvf OpenCV-2.1.0.tar.bz2
$ cd OpenCV-2.1.0
$ mkdir release
$ cd release
# add the our newly installed libs into system path:
$ export LD_LIBRARY_PATH=/home/me/local/lib:$LD_LIBRARY_PATH
# we need to run make, specifying the new python paths
$ ../../local/bin/cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/me/local -D BUILD_PYTHON_SUPPORT=ON -D PYTHON_EXECUTABLE=/home/me/local/python/bin/python -D PYTHONINTERP_FOUND=1 ..
$ make

Here is where I received an error: ‘Py_ssize_t’ does not name a type, at 100%, while compiling cvv.o. I figured out cvv.cpp is using old header from python2.4 instead the new one we have just installed. We have two options: either modifying the header inclusion path or editing the #include directive. I opted for the second one since that’s easier and I only have one source file using that header. You can google for the first solution if you want to. I just edited cvv.cpp, changing #include Python.h
to #include “/home/me/local/python/include/python2.6/Python.h”.

Later Edit: I remembered I had installed Python in a separate dir, all I that had to be done was including that dir into LD_LIBRARY_PATH, just as I had done with the first one.
I used “find” to get the path to my Python.h:

$ find /home/me/local -name Python.h
/home/me/local/python/python2.6/Python.h

After that I reran make and make install in OpenCV/release/ and a test script to see everything is working fine:

$ make
$ make install
$ cd bin/
$ ./cxcoretest
# also it would be a good idea to make the package available to environment:
# first search for it:
$ find /home/me/local/ -name opencv.pc
/home/me/local/lib/pkgconfig/opencv.pc
$ export PKG_CONFIG_PATH=/home/me/local/lib/pkgconfig/:$PKG_CONFIG_PATH
$ cd ../../..
$ pwd
/home/me/

I wanted to compile and run a simple OCR application that I found on github. Credits go to jagd.

$ local/bin/git clone git://github.com/jagd/schnell-ocr.git
$ cd schnell-ocr/
$ make
$ cd tools/
$ ./tester test_image.jpg

Final notes:

  • The “export” commands I used to set environment variables are not persistent. They expire with your session. You may want to batch them in a shell script. Later edit: ~/.bashrc that is. .bashrc is the script that gets executed each time your user starts a new session.
  • My “cc” compiler didn’t support c++ code. I had to set CC=g++ in the top of the Makefiles of that github repository.
  • Since that application uses gtk (graphical interface), you should redirect X server of the remote host by connecting using “ssh -X [remote_host]”.
  • If you happen to get “Illegal instruction” while running applications using OpenCV, you should disable some CPU performance tunning in OpenCV makefiles (Actually cmake files). Edit OpenCV/release/CMakeCache.txt, setting ENABLE_SSE2:BOOL=OFF. Then rerun cmake, make, make install. This is very common with old AMD CPUs (like Athlon XP).

I hope I made your life easier while debugging sources that do not compile. Good luck and may Linux be your friend, not your worst nightmare.


  • http://monex.to/ Monex

    ..Note ..Redhat systems are notorious for shipping with old python versions.that cannot easily be replaced. usr local and then push the new python to the top.of the search PATH for example ..ln -s usr local bin python2.5 bin python.export PATH bin PATH…… …General note .Most python libraries are installed with a simple python setup.py install command.and will end up in the site-packages folder of your local python installation.

  • Junaids Baber

    Very good tutorial. 

    • http://www.mesimian.com Mihnea Simian

      Thanks!

  • Karl

    “If you happen to get “Illegal instruction” while
    running applications using OpenCV, you should disable some CPU
    performance tunning in OpenCV makefiles (Actually cmake files). Edit
    OpenCV/release/CMakeCache.txt, setting ENABLE_SSE2:BOOL=OFF. Then rerun cmake, make, make install. This is very common with old AMD CPUs (like Athlon XP).”

    thank you thank you thank you thank you!!!!

    I know this is article is a bit old, but I have been pulling my hair out for days. After disabled all SSE it seems to be working. I am installing on a very old single core celeron. (ASUS eee 900 to drive an Arduino uno through usb)

    • http://www.mesimian.com Mihnea Simian

      I remember I wasn’t able to run a Bomberman 90’s game because the EEEPC-s lack some DOS cpu instructions. Virtualization did the trick (and it is always an option when dealing with old hardware). I’m glad that worked for you, good luck with your project and maybe link your result here!

  • Yonas

    Hi Simian,
    i sucessfully install OpenCv 2.4.7 on my old Dell Latitude C840 laptop (the OS is Lubuntu 12.04). i tried to disable all SSE in CMakeList.txt file, and then returned to cmake,make, and make install. but after all i still get the same error “Illegal instruction (core dumped)”. do i have to remove OpenCV and reinstall it. please can you help me.

  • Yonas

    It was my mistake man i have to edit CMakeCache.txt instead of CMakeList.txt. after that it works. than you! than you! really than you very much

    • http://www.mesimian.com Mihnea Simian

      Hello!
      You’re welcome! yes, you should have run “make clean” or “make distclean” before retrying install.

  • niya

    hi simian i installed opencv successfully but wen i did this ./cxcoretest it is saying that no such directory present… so instead i tried ./opencv_test_core after that command i got 1 failure which said

    1 test from Core_globbing
    [ RUN ] Core_globbing.accuracy
    /home/internal/geomatics/sivakumar/VT/SR/opencv-2.4.6.1/modules/core/test/test_io.cpp:466: Failure
    Expected: (lenas.size()) > (pngLenas.size()), actual: 0 vs 0
    [ FAILED ] Core_globbing.accuracy (1 ms)
    [———-] 1 test from Core_globbing (1 ms total)

    after that i individually tested ./opencv_test_highgui
    even after tat i got the following errors [———-] Global test environment tear-down
    [==========] 17 tests from 2 test cases ran. (6 ms total)
    [ PASSED ] 0 tests.
    [ FAILED ] 17 tests, listed below:
    [ FAILED ] VideoCapture_Reading_ReadFile.ReadFile/0, where GetParam() = “highgui/video/big_buck_bunny.avi”
    [ FAILED ] VideoCapture_Reading_ReadFile.ReadFile/1, where GetParam() = “highgui/video/big_buck_bunny.mov”
    [ FAILED ] VideoCapture_Reading_ReadFile.ReadFile/2, where GetParam() = “highgui/video/big_buck_bunny.mp4”
    [ FAILED ] VideoCapture_Reading_ReadFile.ReadFile/3, where GetParam() = “highgui/video/big_buck_bunny.mpg”
    [ FAILED ] VideoCapture_Reading_ReadFile.ReadFile/4, where GetParam() = “highgui/video/big_buck_bunny.wmv”
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/0, where GetParam() = (“python/images/QCIF_00.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/1, where GetParam() = (“python/images/QCIF_00.bmp”, true)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/2, where GetParam() = (“python/images/QCIF_01.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/3, where GetParam() = (“python/images/QCIF_01.bmp”, true)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/4, where GetParam() = (“python/images/QCIF_02.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/5, where GetParam() = (“python/images/QCIF_02.bmp”, true)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/6, where GetParam() = (“python/images/QCIF_03.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/7, where GetParam() = (“python/images/QCIF_03.bmp”, true)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/8, where GetParam() = (“python/images/QCIF_04.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/9, where GetParam() = (“python/images/QCIF_04.bmp”, true)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/10, where GetParam() = (“python/images/QCIF_05.bmp”, false)
    [ FAILED ] VideoWriter_Writing_WriteFrame.WriteFrame/11, where GetParam() = (“python/images/QCIF_05.bmp”, true)

    CAN U PLEASE HELP ME
    NONE OF THE PROGRAMS ARE RUNNING

  • Guess

    I just want to thank you for this tutorial. Thanks a lot!