5 May 2011

Setting up OpenCV 2.2 on Ubuntu 10.10

Building and installing OpenCV 2.2 on Ubuntu 10.10.

1. First, install the dependencies from the repositories:
    Command:
 sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev 

2.  Download the source code:
     Command:
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/OpenCV-2.2.0.tar.bz2

3. Extract, create the build directory:
    Commands:
tar xfv OpenCV-2.2.0.tar.bz2
rm OpenCV-2.2.0.tar.bz2
cd OpenCV-2.2.0
mkdir opencv.build
cd opencv.build

4. Configure, make and install:
    Commands:
 cmake ..
 make
 sudo make install

5. To configure the library, edit the following file (might be empty):

sudo gedit /etc/ld.so.conf.d/opencv.conf
and add the line
/usr/local/lib

6. Then run:
    Command:
sudo ldconfig

7. Edit the file:
Command:
sudo gedit /etc/bash.bashrc
and add the following lines at the PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

8. Now, edit the file:
Command:
sudo gedit ~/.bashrc
and add the following lines at the end:
export LD_LIBRARY_PATH=/home/<username>/OpenCV-2.2.0/lib
export PKG_CONFIG_PATH=/home/<username>/OpenCV-2.2.0/lib/pkgconfig

9. To check the path where opencv & other lib files are stored, do:

    $ pkg-config --cflags opencv
(output will come as)
-I/usr/include/opencv

    $ pkg-config --libs opencv
(output will come as)
-lcxcore -lcv -lhighgui -lcvaux -lml

These paths are needed to compile your opencv programs, as given in the next step.

10. To compile & run:

    $ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.cpp
    $ ./a.out <image name>

11. Now lets simplify the above big command by making a shortcut for it:
go to your local home directory(cd /home/) and open the .bashrc file using gedit(the file will be hidden). Append the following to the file:

    alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"

and save. Close the terminal and open it again.(as this process requires relogin of the terminal)

12. Now, go to directory containing a sample program & do

    $ gcv filename.c && ./a.out
    or
    $ gcv filename.c
    $ ./a.out input_img.jpg

As you can see the commands now become similar to $cc filename.c, $./a.out which are used normally for compiling and executing C programs.

13. Check whether all lib files are installed-
$ apt-cache search opencv
returns:
libcv-dev - development files for libcv
libcv0.9-0c2 - computer vision library
libcvaux-dev - development files for libcvaux
libcvaux0.9-0c2a - computer vision extension library
libhighgui-dev - development files for libhighgui
libhighgui0.9-0c2 - computer vision GUI library
opencv-doc - OpenCV documentation and examples
14. Sample testing code:
#include < opencv/cv.h >  /* required to use OpenCV */
#include < opencv/highgui.h > /* required to use OpenCV's highgui */
#include < stdio.h >
        int main(int argc, char *argv[])
       {
           IplImage* img=0;                  /* pointer to an image */
           printf("Hello\n");
           if(argv[1] != 0)
             img = cvLoadImage(argv[1], 0); // 1 for color
           else
             printf("Enter filename\n");
           if(img != 0)
           {
                cvNamedWindow("Display", CV_WINDOW_AUTOSIZE); // create a         window
                cvShowImage("Display", img); // show image in window
                cvWaitKey(0); // wait until user hits a key
                cvDestroyWindow("Display");
            }
            else
               printf("File not found\n");
            return 0;
         }






No comments:

Post a Comment