Recently I was tempted to have a look on OpenCV project and Oreilly’s book “Learning OpenCV” This is a great book and it assumes some basic C programming skills. However, it is not specific to any platform when it comes to compiling and running program examples. Here is a very short start with Ubuntu 9.04

Let’s start with installation of some useful packages into our ubuntu system:

apt-get install libcv1 libcvaux1 libhighgui1 libcv-dev libcvaux-dev libhighgui-dev libavcodec-dev libavformat-dev libavutil-dev libavutil49 pkg-config g++

Once this is done we can start by compiling a first example in the book ( make sure that you have all quotes corect otherwise you will get errors like:
opencv.c:1:10: error: #include expects "FILENAME" or

actual example code:

#include "highgui.h"

int main(int argc, char** argv)
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );


now it’s time to save this code into file. For example let us save it into myopencv.c file.

to compile this code we can use command:

g++ -ggdb -I/usr/include/opencv -lhighgui myopnecv.c.c -o opencv_example

another way to compile is to use pkg-config
g++ -ggdb `pkg-config opencv --cflags --libs` myopnecv.c.c -o opencv_example
which is exactly the same as
g++ -ggdb -I/usr/include/opencv -lcxcore -lcv -lhighgui -lcvaux -lml myopnecv.c.c -o opencv_example

the library must be included for compilation otherwise this errors would occur:
myopnecv.c:In function `main':
myopnecv.c:(.text+0x25): undefined reference to `cvLoadImage'
myopnecv.c:(.text+0x3c): undefined reference to `cvNamedWindow'
myopnecv.c:(.text+0x4f): undefined reference to `cvShowImage'
myopnecv.c:(.text+0x5b): undefined reference to `cvWaitKey'
myopnecv.c:(.text+0x66): undefined reference to `cvReleaseImage'
myopnecv.c:(.text+0x72): undefined reference to `cvDestroyWindow'


error: too few arguments to function ‘cvLoadImage’

if your compilation was successful a opencv_example binary should appear in your directory. when running this binary supply an argument ( some picture ):

./opencv_example mypicture.jpg

the image should pop up on your screen.

Free Linux eBooks

Go to top