Tuesday, February 7, 2012

Compiling a simple OpenCv program in Ubuntu

Here is a couple links that could help you in case you run into problems:
opencv.org and willowgarage.com

Update: Here I found a script that takes care of the whole installation in Ubuntu. You might want to try that first.
https://help.ubuntu.com/community/OpenCV

Otherwise:
First you need to install the required packages in your system.
You can use this script to do that. Feel free to edit it in case doesn't work for you.
Remember to use chmod 755 to change the execution rights of the file


1. Create a folder for opencv
mkdir opencv
cd opencv

2. Download the code from
git clone git://code.opencv.org/opencv.git
or
git clone https://github.com/Itseez/opencv.git

3. Create a release folder to store built files
cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

(Remember that if you have a GPU installed you want to add the following parameters: -D WITH_CUDA=YES -D CUDA_TOOLKIT_ROOT_DIR="/usr/local/cuda/bin")
-Optionally you can add other options to the cmake line such as -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON

4. Run make inside release
make
sudo make install



5. Create a simple project using your favorite IDE. Here I just used vi:
vim image-display.c

6. Create a simple program such as:

#include "highgui.h"

int main( int argc, char** argv ) {
// cvLoadImage determines an image type and creates datastructure with appropriate size
IplImage* img = cvLoadImage( argv[1],1);

// create a window. Window name is determined by a supplied argument
cvNamedWindow( argv[1], CV_WINDOW_AUTOSIZE );
// Display an image inside and window. Window name is determined by a supplied argument
cvShowImage( argv[1], img );
// wait indefinitely for keystroke
cvWaitKey(0);

// release pointer to an object
cvReleaseImage( &img );
// Destroy a window
cvDestroyWindow( argv[1] );
}


7. gcc -ggdb `pkg-config --cflags opencv` image-display.c -o image-display `pkg-config --libs opencv`

8. run the program ./image-display "cat.jpg"



No comments: