October 11, 2011

Execute OpenCV Program in Linux (Ubuntu)



      In my last post, I discussed how to install OpenCV on Linux - Ubuntu. Today I will show you how to execute OpenCV programs on Linux - Ubuntu. Remember, you don't need Microsoft Visual Studio unlike OpenCV in Windows. After compiling the program, you get the executible file of your program.
      Remember to complete every step of installation procedure. Then only start to compile your program.

1. After installing all the packages, open a terminal & type this code:
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.

2. For testing:
Create a new folder & a file “hello.cpp” in it using any text editor & type this code in it:
#include < cv.h > /* required to use OpenCV */
#include < 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;
}

3. 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.

4. To compile & run:
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.cpp
./a.out img

where “img” is the name of any image within the same folder with extension.
You should be able to see “Hello” and the image in a different window.

-> If this runs, Congrats! now you can run any C/C++ program with opencv lib.
Else, try

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

and go to step 1 again.

5. 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)

6.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.

****************************
Some ways to check whether all lib files are installed-
1. $ 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

      In this way, you can compile and execute your OpenCV programs on Linux - Ubuntu