OpenCV with Eclipse

OpenCV is an open source "computer vision" library, which means that it can be used to extract information from an image, or a series of images (like a video). A computer vision library has many uses, but my favorite is in the field of robotics.

Eclipse was originally developed for the Java Programming Language, but using the Eclipse CDT it can be used for C++ development as well. Eclipse is cross platform, and has a huge following in both the open source and the corporate communities.

I am not going to go into details about installing OpenCV, installing Eclipse, nor about installing the CDT which we will be using. There are other and better tutorials for doing that. I will cover how to set up a simple OpenCV project in Eclipse, so that studiers can get writing code as soon as possible.

To install OpenCV, check these links:

http://opencv.willowgarage.com/wiki/InstallGuide_Linux

http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=1...

http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port

To install Eclipse, check these links:

http://www.cs.umd.edu/class/spring2006/cmsc132/EclipseTutorial/install.h...

http://www.cs.umd.edu/class/fall2004/cmsc131/EclipseTutorial/install.htm...

*NOTE: If you are using an operating system with software repositories that include Eclipse, use the repository to install, as it will be much cleaner and easier.

To install the Eclipse CDT, check this link:

http://www.eclipse.org/cdt/downloads.php

At this point, I will assume that you have OpenCV, Eclipse, and the Eclipse CDT already installed. Now it is time to create our project. Start up Eclipse. You should see something like this:

Now go to File->New->Project..., and you will get this dialog:

Select Managed Make C++ Project under the C++ tree item, and click Next. You will now be prompted to enter the name of the project:

Enter the name of the project, and click next.

 

Make sure you have "Executable" selected as your project type. Click Next again to see the Additional Project Settings page, and then click Finish.

You will be asked if you want to open the C/C++ perspective. Click Yes.

 

You should now be back at the main Eclipse window. Make sure that your new project is selected in the C/C++ Projects tab, and the go to Project->Properties.

Select C/C++ Build from the menu at the far left, and then go to the Tool Settings tab, and select Directories under GCC C++ Compiler. On the right, add a new Include Path at /usr/include/opencv (or wherever you have the opencv include files installed). Now select Libraries under GCC C++ Linker. Add 'cv' and 'highgui' to the libraries, and add a library path to /usr/local/lib (or wherever your OpenCV library files are installed).

 

Do add these paths and libraries for both the Debug and Release builds of your project. Click OK to continue. Now right click on your project listed on the left, and select New->Source Folder. Enter 'src' as the folder name, and click Finish. Now right click on the newly created folder and select New->Source File. Name the new file 'main.cpp' and click Finish. You can now enter code into your main.cpp file. For a quick test, just enter this code:

Here is the code:

/*

http://nashruddin.com/opencv-examples-for-operation-on-images.html/2

*/

#include <stdio.h>

#include "cv.h"

#include "highgui.h"

int main( int argc, char** argv )

{

    IplImage *img = 0;

    if( argc < 2 ) {

        fprintf( stderr, "Usage: loadimg <filename>\n" );

        return 1;

    }

    img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );

    if( img == 0 ) {

        fprintf( stderr, "Cannot load file %s!\n", argv[1] );

        return 1;

    }

    cvNamedWindow( "image", CV_WINDOW_AUTOSIZE );

    cvShowImage( "image", img );

    cvWaitKey(0);

    cvDestroyWindow( "image" );

    cvReleaseImage( &img );

    return 0;

}

To compile, simply save the project. If all went okay, you should have no errors. If you have errors, check to make sure that your include and library paths are correct. To run the program, you will have to set up a run configuration. Don't worry, this is easy. Just go to Run->Run.. and you will get the Run dialog. Double click C/C++ Local Application item on the left, and you should see your project appear under it. Select this project, and click the Browse button next to the C/C++ Application field. Select your project's executable.

For the Image sample program to run, you will need an image file called 'lena.jpg' located in your project's src directory. You can copy this file from the OpenCV sample folder, or create your own. If you want to get fancy, you can re-edit your run configuration to use a file you pass in from the command line. Click Run to see your program execute.

Reference: