Use OpenCV in Eclipse CDT

Install OpenCV (see previous posts for Ubuntu 10.10, Ubuntu 11.10, Ubuntu 12.04 and OpenCV 2.4 in Ubuntu 12.04).

Install Eclipse CDT:

sudo apt-get install eclipse-cdt

Open Eclipse CDT and select a workspace (it’s just a path to store projects together). Create a new “C++ Project”, give it a name and select “Empty Project” on “Project type” and “Linux GCC” on “Toolchains”.

Right click on the project folder and select “Properties”. Go to “C/C++ Build->Settings”. Then, inside “Tool Settings” tab, go to “GCC C++ Compiler->Includes” and fill “Include paths (-l)” with the path of your OpenCV include installation. If you installed OpenCV from the source code it will possibly be “/usr/local/include/opencv2” (or the path you entered in CMAKE_INSTALL_PREFIX when configuring with CMake), but if you installed from the repositories it should be “/usr/include/opencv2“.

Now go to “GCC C++ Linker->Libraries” and fill “Libraries (-l)” with (at least) those OpenCV libraries:

opencv_core
opencv_highgui

And “Library search paths (-L)” with the path location of your OpenCV installation libraries. As before, if you installed from the source it should be “/usr/local/lib” (same here with CMAKE_INSTALL_PREFIX) and if you installed with repositories “/usr/lib“.

Now it’s time to code. Go to “File->New->Source file” and create a new file called “main.cpp“. Fill it with:

#include "opencv.hpp"

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

	IplImage* img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 3 );

	cvCircle( img, cvPoint( 320, 240 ), 100, cvScalar( 255, 0, 0 ), 5 );

	cvNamedWindow( "OpenCV Window", CV_WINDOW_NORMAL );
	cvShowImage( "OpenCV Window", img );

	cvWaitKey(0);

	cvDestroyWindow( "OpenCV Window" );
	cvReleaseImage( &img );

	return 0;
}

This code will just show a black window with a blue circle in it and end when you press any key. Not very fancy but we just want to check if all runs OK.

Eclipse CDT comes with an option to build automatically, but I always disable it deselecting “Project->Build Automatically”. Build all by pressing Control+B or going to “Project->Build All”. Before debugging/running our application we need to configure the debugging/running environment. Go to “Run->Debug Configurations…” and do a double click on “C/C++ Application”. It will create a new debug configuration, but “C/C++ Application” parameter may not be automatically configured (this happend to me), so use “Browse…” button to select a “Project”, select your current project, and now (if your application binary program does exist) all should be OK to push “Debug” button.

You should now be able to run and debug your OpenCV application. Using any other library with Eclipse CDT is pretty much the same.

You might also like

Install OpenCV 2.3.1 in Ubuntu 12.04
OpenCV 2.3.1 is now the last version in repositories so you just need to run:sudo apt-get install...

Install OpenCV 2.3.1 in Ubuntu 12.04
OpenCV 2.3.1 is now the last version in repositories so you just need to run:sudo apt-get install...

Install OpenCV 2.3.1a in Ubuntu 11.10
If you want to install OpenCV 2.1 you can just use apt-get to do the work: sudo apt-get install libcv-dev...

Install OpenCV 2.3.1a in Ubuntu 11.10
If you want to install OpenCV 2.1 you can just use apt-get to do the work: sudo apt-get install libcv-dev...

Leave a comment ?

12 Comments.

  1. Sergio Corral

    I get two errors by the time I finish evrything and I can’t figure out why.

    Error 1: fatal error: opencv2/photo/photo.hpp: No such file or directory.

    Error 2: make:***[main.o] Error 1

    Any help will be appreciated.

    • You’ll have to give me some more info to help you. How did you configure Eclipse CDT, were do you have your OpenCV installed, what does your code do, etc.

      The first error look if that photo.hpp file is really were you told in the “Include paths (-I) option. There should be a file with that name in “/usr/include/opencv2/photo/photo.hpp” or “/usr/local/include/opencv2/photo/photo.hpp”, those are the usual locations. But I don’t have that “photo” folder in my OpenCV installation…

      The second error is nothing by it self. Look for the real error somewhere before it.

      • you need find the opencv.hpp file and open it,then //#include “opencv2/photo/photo.hpp” because you opencv or opencv2 no photo ,,,,

  2. Sergio Corral

    The photo error is what really caught me off course. The folder you are discribing is not in the opencv2 folder (I don’t know why.) All I did to eclipse was add C/C++ on it.

    Also, for the include path, “/usr/local/include/opencv2” and “/usr/include/opencv2“ didn’t work for me, so I put “/home/programming/OpenCV-2.4.0/include/opencv” instead, which is where the opencv2 folder is located and it worked for me but I’m not sure if that is what is causing the error.

    Thank You.

  3. Hola, Tengo un problema, no me muestra ninguna imagen al ejecutar, pero lo compila sin ningún error. durante el lauching solo muestra esto en la consola
    “Info: Nothing to build for …….” luego al terminar desaparece y no me muestra nada…
    Sabes que puede estar faltando????? Te voy a agradecer la respuesta.

    • Pues diría que lo que pasa es que no estás dándole a ejecutar, sino a compilar una y otra vez. Por eso el mensaje que dice “Nada que construir”, porque ya está compilado. Asegúrate de que estás haciendo click sobre ejecutar y no sobre compilar.

      Espero que te sirva la respuesta.

  4. nice! funciona, ahora a buscar ejemplos..

  5. I have that error
    launch failed. Binary not found
    How can i solve it??

  6. Hi,
    I followed all the above steps but still i got following errors!

    Description Resource Path Location Type
    cannot find -l libopencv_ml Demotest C/C++ Problem
    cannot find -llibopencv_calib3d Demotest C/C++ Problem
    cannot find -llibopencv_core Demotest C/C++ Problem
    cannot find -llibopencv_highgui Demotest C/C++ Problem
    cannot find -llibopencv_legacy Demotest C/C++ Problem
    make: *** [Demotest] Error 1 Demotest C/C++ Problem

  7. Avaneet Ranjan

    My confusion is every time I have to write a new project in eclipse will I have to include all the opencv libraries again and again?

    help appreciated!
    thanks

    • May be there is another easier way to do this but I don’t know. This is just a couple of minutes 😉

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.