Monday 27 July 2015

Using OpenCV with Eclipse (plugin CDT) on Ubuntu

OpenCV is written natively in C++ and has a templated interface that works seamlessly with STL containers. OpenCV library therefore, is extensively used with C++. Here, we will see how to use Eclipse with CDT Plugin on Ubuntu 14.04. We will use OpenCV-2.4.11 and Luna Eclipse for C/C++ developers. We start with downloading Eclipse and then configuring it with OpenCV libraries. We will also implement an a OpenCV program.


I will assume that you have the OpenCV-2.4.11 installed on your Ubuntu System. However, if you do not have it installed or you are facing problems getting it done, you can refer to Installing OpenCV-2.4.11 on Ubuntu.

Let's start!

Before we start, we're going to need to download Eclipse. I use OpenCV with C ++, Then I'm going to download the specific release to C / C ++ of Eclipse Luna: Eclipse IDE for C / C ++ Developers. Keep referring to the screenshots as you keep progressing through tutorial. The pictures will exemplify each step and surely simplify your life !


Downloading and Installing Eclipse

  1. Go to Downloads Eclipse.
  2. In Package Solutions, search for  Eclipse IDE for C / C ++ Developers.
  3. Select Linux 32 or 64 bits; (In my case, Linux 64-bit);
  4. Unzip in any folder of your choice;
  5. Done!You also can install Eclipse using sudo apt-get install eclipse-cdt

Making a C++ Project

  1. Start Eclipse. Just run the executable that comes in the folder.
  2. Go to File -> New -> C/C++ Project
  3. Choose a name for your project (i.e. ImageProcessing). An Empty Project should be okay for this example.
  4. Leave everything else by default. Press Finish.
  5. Your project (in this case ImageProcessing) should appear in the Project Navigator (usually at the left side of your window).
  6. Now, let’s add a source file using OpenCV:
    • Right click on ImageProcessing (in the Navigator). New -> Folder .
    • Name your folder src and then hit Finish
    • Right click on your newly created src folder. Choose New source file:
    • Call it ImageProcessing.cpp. Hit Finish
  7. So, now you have a project with a empty .cpp file. Let’s fill it with some sample code (in other words, copy and paste the snippet below):


  8. We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are. For this, do the following:
    • Go to Project–>Properties
    • In C/C++ Build, click on Settings. At the right, choose the Tool Settings Tab. Here we will enter the headers and libraries info:
      1. In GCC C++ Compiler, go to Includes.In Include paths(-l) you should include the path of folder where opencv was installed. In our example,this is  /usr/local/include/opencv.


      2. Now go to GCC C++ Linker,there you have to fill two spaces: First in Library search path (-L) you have to write the path to where the opencv libraries reside, in my case the path is:

        /usr/local/lib
      3. Then in Libraries(-l) add the OpenCV libraries that you may need.

        opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann


        Now you are done. Click OK
    • Your project should be ready to be built. For this, go to Project->Build all

Running the Executable

    • Go to Run->Run Configurations
    • Under C/C++ Application you will see the name of your executable + Debug. Select the name (in this case ImageProcessing Debug).
    • Now, in the right side of the window, choose the Arguments Tab. Write the path of the image file we want to open (path relative to the workspace/DisplayImage folder). Let’s use HappyLittleFish.png:


    • Click on the Apply button and then in Run. An OpenCV window should pop up with the image.


    • Congratulations! You are ready to have fun with OpenCV using Eclipse.

No comments:

Post a Comment