Sunday, 5 February 2012

Installing OpenCV on Ubuntu



Before I begin, I am using Ubuntu 10.10 (Maverick). I am certain that this process will work with most versions of Ubuntu, but still, if it doesn't work with your specific version, then message me. To check the version of Ubuntu that you are running, open the terminal and type lsb_release -a and press Enter.

Prerequisites:

  1. A C++ compiler. g++ is most suitable. To check the version of g++, type  g++ --v  in the terminal. Mine is version 4.4.5. If g++ isn't installed, run the following command in the terminal :

    $ sudo apt-get install g++

  2. A Graphical User Interface library, GTK+ ver 2.0 or higher. To check your version of GTK+, run this on the terminal :

    $ dpkg -l | grep libgtk

    If the required version of GTK+ is not installed, run the following command :

    $ sudo apt-get install libgtk2.0-dev

If both the above requirements are already installed, then go to System -> Administration -> Synaptic Package Manager and search for "opencv". Install the main package and these library files :
libcv
libcv-dev
libcvaux
libcvaux-dev
libhighgui
libhighgui-dev

Optional : python-opencv - This contains the Python bindings for OpenCV. Install it if you want to program in Python.
opencv-doc - This contains the documentation. Not necessary to get OpenCV working, but useful nonetheless. 

Now set the default path for OpenCV libraries by executing the following commands on the terminal :

$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

To check the path where the OpenCV library files are stored, execute these on the terminal :

$ pkg-config --cflag opencv

The output will be -I/usr/include/opencv

$ pkg-config --libs opencv

The output will be -lcxcore -lcv -lhighgui -lcvaux -lml

Open gedit and type a simple program to test whether the installation is working. I provide a sample code taken from the book Learning OpenCV:


#include <highgui.h>

int main(int argc, char *argv[]) {
  IplImage* img = cvLoadImage(argv[1]);
  cvNamedWindow("Example", 0);
  cvShowImage("Example",img);
  cvWaitKey(0);
  cvReleaseImage(&img);
  cvDestroyWindow("Example");    
  }

I saved this as cv_example.c

Now to compile this, type the following command in the terminal :

$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lml cv_example.c

The output is in the a.out file in the same directory. As can be seen from the code, you need to provide an image file as argument and this file should be in the same directory. I used a file by the name "mustang.jpg"

$ ./a.out mustang.jpg

If all has gone well, you will find a window by the name Example that displays your .jpg file. 

I hope this helps. If you face any difficulties, contact me.

No comments:

Post a Comment