linux_eclipse.rst 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
.. _Linux_Eclipse_Usage:

Using OpenCV with Eclipse (plugin CDT)
****************************************

.. note::
   Two ways, one by forming a project directly, and another by CMake

Prerequisites
===============

1. Having installed `Eclipse <http://www.eclipse.org/>`_ in your workstation (only the CDT plugin for C/C++ is needed). You can follow the following steps:

14
   * Go to the Eclipse site
15 16 17 18 19 20 21 22

   * Download `Eclipse IDE for C/C++ Developers <http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2>`_ . Choose the link according to your workstation.

#. Having installed OpenCV. If not yet, go :ref:`here <Linux-Installation>`.

Making a project
=================

23
1. Start Eclipse. Just run the executable that comes in the folder.
24 25 26 27 28 29 30

#. Go to **File -> New -> C/C++ Project**

   .. image:: images/a0.png
      :alt: Eclipse Tutorial Screenshot 0
      :align: center

31
#. Choose a name for your project (i.e. DisplayImage). An **Empty Project** should be okay for this example.
32 33 34 35 36

   .. image:: images/a1.png
      :alt: Eclipse Tutorial Screenshot 1
      :align: center

37
#. Leave everything else by default. Press **Finish**.
38 39 40 41 42 43 44 45 46 47

#. Your project (in this case DisplayImage) should appear in the **Project Navigator** (usually at the left side of your window).

   .. image:: images/a3.png
      :alt: Eclipse Tutorial Screenshot 3
      :align: center


#. Now, let's add a source file using OpenCV:

48
   * Right click on **DisplayImage** (in the Navigator). **New -> Folder** .
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

     .. image:: images/a4.png
        :alt: Eclipse Tutorial Screenshot 4
        :align: center

   * Name your folder **src** and then hit **Finish**

   * Right click on your newly created **src** folder. Choose **New source file**:

   * Call it **DisplayImage.cpp**. Hit **Finish**

     .. image:: images/a7.png
        :alt: Eclipse Tutorial Screenshot 7
        :align: center

#. 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):

   .. code-block:: cpp

68
      #include <opencv2/opencv.hpp>
69 70 71 72 73 74 75 76 77

      using namespace cv;

      int main( int argc, char** argv )
      {
        Mat image;
        image = imread( argv[1], 1 );

        if( argc != 2 || !image.data )
78
          {
79
            printf( "No image data \n" );
80
            return -1;
81 82
          }

83
        namedWindow( "Display Image", WINDOW_AUTOSIZE );
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        imshow( "Display Image", image );

        waitKey(0);

        return 0;
      }

#. 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:

      a. In **GCC C++ Compiler**, go to **Includes**. In **Include paths(-l)** you should include the path of the folder where opencv was installed. In our example, this is ``/usr/local/include/opencv``.

         .. image:: images/a9.png
            :alt: Eclipse Tutorial Screenshot 9
            :align: center

         .. note::
104
            If you do not know where your opencv files are, open the **Terminal** and type:
105 106 107 108 109 110 111 112 113

            .. code-block:: bash

               pkg-config --cflags opencv

            For instance, that command gave me this output:

            .. code-block:: bash

114
               -I/usr/local/include/opencv -I/usr/local/include
115 116 117 118 119 120


      b. 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:
         ::
121

122
            /usr/local/lib
123

124 125 126
         Then in **Libraries(-l)** add the OpenCV libraries that you may need. Usually just the 3 first on the list below are enough (for simple applications) . In my case, I am putting all of them since I plan to use the whole bunch:


127 128
         opencv_core
         opencv_imgproc
129
         opencv_highgui
130 131
         opencv_ml
         opencv_video
132
         opencv_features2d
133 134
         opencv_calib3d
         opencv_objdetect
135
         opencv_contrib
136
         opencv_legacy
137 138 139 140
         opencv_flann

         .. image:: images/a10.png
             :alt: Eclipse Tutorial Screenshot 10
141 142
             :align: center

143 144 145
         If you don't know where your libraries are (or you are just psychotic and want to make sure the path is fine), type in **Terminal**:

         .. code-block:: bash
146

147 148 149 150 151
            pkg-config --libs opencv


         My output (in case you want to check) was:
         .. code-block:: bash
152 153

            -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
154 155 156

         Now you are done. Click **OK**

157
    * Your project should be ready to be built. For this, go to **Project->Build all**
158

159
      In the Console you should get something like
160 161 162

      .. image:: images/a12.png
         :alt: Eclipse Tutorial Screenshot 12
163
         :align: center
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

      If you check in your folder, there should be an executable there.

Running the executable
========================

So, now we have an executable ready to run. If we were to use the Terminal, we would probably do something like:

.. code-block:: bash

   cd <DisplayImage_directory>
   cd src
   ./DisplayImage ../images/HappyLittleFish.png

Assuming that the image to use as the argument would be located in <DisplayImage_directory>/images/HappyLittleFish.png. We can still do this, but let's do it from Eclipse:


181
#. Go to **Run->Run Configurations**
182

183
#. Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Application a couple of times). Select the name (in this case **DisplayImage Debug**).
184 185 186 187 188

#. 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**:

   .. image:: images/a14.png
      :alt: Eclipse Tutorial Screenshot 14
189
      :align: center
190 191 192 193 194

#. Click on the **Apply** button and then in Run. An OpenCV window should pop up with the fish image (or whatever you used).

   .. image:: images/a15.jpg
      :alt: Eclipse Tutorial Screenshot 15
195
      :align: center
196 197 198 199 200 201 202 203 204 205 206 207

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

==================================================
V2: Using CMake+OpenCV with Eclipse (plugin CDT)
==================================================

Say you have or create a new file, *helloworld.cpp* in a directory called *foo*:

.. code-block:: cpp


208 209 210
   #include <opencv2/opencv.hpp>
   using namespace cv;

211 212
   int main ( int argc, char **argv )
   {
213 214 215 216
     Mat img(480, 640, CV_8U);
     putText(img, "Hello World!", Point( 200, 400 ), FONT_HERSHEY_SIMPLEX | FONT_ITALIC, 1.0, Scalar( 255, 255, 0 ));
     imshow("My Window", img);
     waitKey();
217 218 219 220 221 222 223 224 225 226 227 228 229 230
     return 0;
   }

1. Create a build directory, say, under *foo*: ``mkdir /build``.  Then ``cd build``.

#. Put a *CmakeLists.txt* file in build:

.. code-block:: bash

   PROJECT( helloworld_proj )
   FIND_PACKAGE( OpenCV REQUIRED )
   ADD_EXECUTABLE( helloworld helloworld.cxx )
   TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} )

231
#. Run: ``cmake-gui ..`` and make sure you fill in where opencv was built.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

#. Then click ``configure`` and then ``generate``. If it's OK, **quit cmake-gui**

#. Run ``make -j4``   *(the ``-j4`` is optional, it just tells the compiler to build in 4 threads)*. Make sure it builds.

#. Start ``eclipse`` . Put the workspace in some directory but **not** in ``foo`` or ``foo\\build``

#. Right click in the ``Project Explorer`` section. Select ``Import``  And then open the ``C/C++`` filter. Choose *Existing Code* as a Makefile Project``

#. Name your project, say *helloworld*. Browse to the Existing Code location ``foo\\build`` (where you ran your cmake-gui from). Select *Linux GCC* in the *"Toolchain for Indexer Settings"* and press *Finish*.

#. Right click in the ``Project Explorer`` section. Select ``Properties``. Under ``C/C++ Build``, set the *build directory:* from something like ``${workspace_loc:/helloworld}`` to ``${workspace_loc:/helloworld}/build`` since that's where you are building to.

 a. You can also optionally modify the ``Build command:`` from ``make`` to something like ``make VERBOSE=1 -j4`` which tells the compiler to produce detailed symbol files for debugging and also to compile in 4 parallel threads.

#. Done!