We have 2 ways in order to install OpenCV on ours Ubuntu machine.
Here I explain both of them:

  1. from repositories
  2. from sources
  3. changes for Ubuntu 11.10

Then we can check if the python wrapper is properly installed.




INSTALL FROM REPOSITORIES

Prerequisites

  • C++ compiler like g++ (sudo apt-get install g++)
  • GTK+ 2.0 or higher (sudo apt-get install libgtk2.0-dev)


Procedure

  1. Open a terminal and type “sudo apt-get install” for all these packages
    libcv2.1
    libcv-dev
    libcvaux2.1
    libcvaux-dev
    libhighgui2.1
    libhighgui-dev
    opencv-doc
    python-opencv
  2. Then, always in a terminal, type:
    export LD_LIBRARY_PATH=/home/opencv/lib
    export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

    The above ones are default paths for the opencv libraries.

  3. To check the path where opencv & other lib files are stored (needed to compile your opencv programs) type (and check the output):
    $ pkg-config --cflags opencv
             -I/usr/include/opencv
    
    $ pkg-config --libs opencv
             -lcxcore -lcv -lhighgui -lcvaux -lml
    
  4. Create a test file “hello.c” and type this code in it:
    #include <stdio.h>
    #include "cv.h"
    #include "highgui.h"
    
    int main( int argc, char** argv )
    {
        /* data structure for the image */
        IplImage *img = 0;
    
        /* check for supplied argument */
        if( argc < 2 ) {
            fprintf( stderr, "Usage: loadimg <filename>\n" );
            return 1;
        }
    
        /* load the image,
        use CV_LOAD_IMAGE_GRAYSCALE to load the image in grayscale */
        img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );
    
        /* always check */
        if( img == 0 ) {
            fprintf( stderr, "Cannot load file %s!\n", argv[1] );
            return 1;
        }
    
        /* create a window */
        cvNamedWindow( "image", CV_WINDOW_AUTOSIZE );
    
        /* display the image */
        cvShowImage( "image", img );
    
        /* wait until user press a key */
        cvWaitKey(0);
    
        /* free memory */
        cvDestroyWindow( "image" );
        cvReleaseImage( &img );
    
        return 0;
    }
  5. Compile it
    g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c -o hello
  6. Then run it
    ./hello img.jpg

    where “img” is the name of any image within the same folder of the C script.
    If this not run, try

    export PATH=$HOME/usr/bin/:$PATH

    and go to step2 again


  7. In order to simplify the command used for compile the script, we can create a shortcut
    • go to your local home directory:
      cd /home/
    • open the .bashrc file (it will be hidden):
      vi .bashrc
    • append this line:
      alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"
    • save and clode the file, then close the terminal and open it again
  8. Now go to directory containing a sample program and type:
    gcv hello.c -o hello
    ./hello img.jpg




INSTALL FROM SOURCE

Further prerequisite: CMake 2.6 or later

Procedure

  1. Get the latest stable OpenCV version downloading the source tarball (OpenCV-x.y.z.tar.bz2)
  2. Create a folder named “opencv” (for example in the folder “/projects”) and extract in it the content of the tarball
  3. Then open a terminal and type:
    cd ~/projects/opencv # the directory containing INSTALL, CMakeLists.txt etc.
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
    
  4. Then, build the library and install it to the target directory:
    make
    sudo make install
    




CHANGES FOR UBUNTU 11.10

If you get some library errors, like the following:

./hello: error while loading shared libraries: libcv.so.2.1: wrong ELF class: ELFCLASS64

you can solve the problem typing:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
sudo ldconfig

and then recompiling the script in this way:

g++ hello.c -o hello `pkg-config --cflags --libs opencv`




CHECK THE PYTHON WRAPPER

In order to check if the python wrapper is properly installed create a new file named “hello_python.py”

import cv
im = cv.LoadImage( "sample_image.jpg" )
cv.ShowImage( "sample1", im )
cv.WaitKey()

Then open a terminal and type:

python hello_python.py