Sunday, 1 January 2012

Setting up OpenCV 2.3 and Eclipse on Ubuntu 11.10

This post seeks to help you in installing OpenCV and Eclipse and setting them up on Ubuntu 11.10 Oneiric so that you can begin development straight away.

What is OpenCV? OpenCV is a library of programming functions (in C and some C++ classes) mainly aimed at real time computer vision and image processing. It is a cross platform library, first developed by Intel and now supported by Willow Garage, and is free for use under the open source BSD license.

What is Eclipse? Eclipse is a multi-language software development platform comprising an IDE and lots of plug ins to help you develop applications in several widely used programming languages.

This walk through is aimed at helping you to set up OpenCV 2.3 (the latest version as of January 2012), and Eclipse CDT (Eclipse with the C Development Toolkit) on Ubuntu 11.10.

Ubuntu 11.10 does come with OpenCV 2.1 in the repositories, but we want OpenCV 2.3 ... so we first begin with adding a PPA for OpenCV 2.3.

Head over to the terminal and type

$ sudo add-apt-repository ppa:gijzelaar/cuda
$ sudo add-apt-repository ppa:gijzelaar/opencv2.3
$ sudo apt-get update

Now, do the usual apt-get to get OpenCV. Note that the usual Ubuntu repos have a package called libcv-dev, however with the new PPA we are looking for a package called libopencv-dev.

$ sudo apt-get install libopencv-dev

This installs OpenCV on your computer. You will find the package files in /usr/include/opencv/

Now for Eclipse. You can do the usual apt-get (sudo apt-get install eclipse) to get Eclipse or you can download from the official website. I recommend the former. At the end you should have the following packages installed on your machine. ( I further recommend you install Synaptic Package Manager (sudo apt-get install synaptic) and then check for the individual packages.)


Now you should be able to launch Eclipse. So do that and then create a new C or C++ project.

We shall write an introductory OpenCV program in C++ in this project, so we name the project ImageDisplay. (Note that I have created a C++ project and not a C project. While Ubuntu comes with the default gcc compiler for C, it does not have the GNU C++ compiler, g++ by default. In case you also wish to use C++ instead of C, do a sudo apt-get install g++ to install the g++ compiler on your computer).

We now have a C++ project called ImageDisplay. To enable us to use the OpenCV libraries in this project we need to include these libraries. To do that, right click on the ImageDisplay project in the Project Explorer on the left and select Properties. Go to C/C++ build in the left menu and then Settings, in the dialog box that appears.

The Configuration bar should be set to Debug [ Active ]. In the Tool Setting tab, select Directories in the GCC C++ Compiler menu and add the path


Now go to Libraries under GCC C++ Linker and add the libraries that you would need. Typically you would definitely need opencv_core and opencv_highgui. The other libraries that you can add are


Lastly, in the Library search path, add /usr/lib.

Make sure that you add these paths and directories for the Release build as well (in the Configuration bar).

Now we are all set to write our first OpenCV program.

Before that let us once run through what all we've done.

  • Installed OpenCV.
  • Installed Eclipse.
  • Installed G++.
  • Created a project called ImageDisplay.
  • Configured the libraries and paths with our project.
Now for the program.

Create a file within your ImageDisplay project called main.cpp. Also, copy an image (.jpg format) to your project workspace folder. (You had set your workspace folder when you had started Eclipse. If you do not remember, then it mostly is in your home folder, and it's called workspace. There'll be a folder within the folder corresponding to you current project called ImageDisplay. So copy the image there. Call it image.jpg)

Double click main.cpp in the Project Explorer and write the following piece of code. If all goes well, when you compile and run this program, you should see a window and your image.jpg should be displayed in it.
Just replace the quotes in the include statements with angular brackets.

#include "cstdlib"
#include "cmath"
#include "cv.h"
#include "highgui.h"

int main(int argc, char *argv[])


cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );

IplImage* img = 0;

cvShowImage("Example1", img );
cvReleaseImage(&img );

return 0;


Now save this file (Ctrl+S) and compile the code (ie "build" the project with Ctrl+B). Now click on the green arrow in the Eclipse toolbar to run the code.

You should see a window pop up called Example1 and your image.jpg should be in it.

You might face an error during compile time / run time that looks something like this :
Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap",
This is because you do not have a package in your system. To install it, just head over to the terminal and type
sudo apt-get install gtk2-engines-pixbuf
That should be it. If you face any problem reread this post again and follow it carefully. Else of course, you can drop me a mail.


Also a very Happy New Year. Why I don't get all gaga on New Year's can be summed up nicely in the words of Mark Twain.
New Year's is a harmless annual institution, of no particular use to anybody save as a scapegoat for promiscuous drunks, and friendly calls, and humbug resolutions, and we wish you to enjoy it with a looseness suited to the greatness of the occasion
So there.

Post a Comment