Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / introduction / linux_eclipse / linux_eclipse.markdown
1 Using OpenCV with Eclipse (plugin CDT) {#tutorial_linux_eclipse}
2 ======================================
3
4 @prev_tutorial{tutorial_linux_gcc_cmake}
5 @next_tutorial{tutorial_windows_install}
6
7 Prerequisites
8 -------------
9 Two ways, one by forming a project directly, and another by CMake Prerequisites
10 -#  Having installed [Eclipse](http://www.eclipse.org/) in your workstation (only the CDT plugin for
11     C/C++ is needed). You can follow the following steps:
12     -   Go to the Eclipse site
13     -   Download [Eclipse IDE for C/C++
14         Developers](http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2) .
15         Choose the link according to your workstation.
16 -#  Having installed OpenCV. If not yet, go @ref tutorial_linux_install "here".
17
18 Making a project
19 ----------------
20
21 -#  Start Eclipse. Just run the executable that comes in the folder.
22 -#  Go to **File -\> New -\> C/C++ Project**
23
24     ![](images/a0.png)
25
26 -#  Choose a name for your project (i.e. DisplayImage). An **Empty Project** should be okay for this
27     example.
28
29     ![](images/a1.png)
30
31 -#  Leave everything else by default. Press **Finish**.
32 -#  Your project (in this case DisplayImage) should appear in the **Project Navigator** (usually at
33     the left side of your window).
34
35     ![](images/a3.png)
36
37 -#  Now, let's add a source file using OpenCV:
38     -   Right click on **DisplayImage** (in the Navigator). **New -\> Folder** .
39
40         ![](images/a4.png)
41
42     -   Name your folder **src** and then hit **Finish**
43     -   Right click on your newly created **src** folder. Choose **New source file**:
44     -   Call it **DisplayImage.cpp**. Hit **Finish**
45
46         ![](images/a7.png)
47
48 -#  So, now you have a project with a empty .cpp file. Let's fill it with some sample code (in other
49     words, copy and paste the snippet below):
50     @code{.cpp}
51     #include <opencv2/opencv.hpp>
52
53     using namespace cv;
54
55     int main( int argc, char** argv )
56     {
57       Mat image;
58       image = imread( argv[1], 1 );
59
60       if( argc != 2 || !image.data )
61         {
62           printf( "No image data \n" );
63           return -1;
64         }
65
66       namedWindow( "Display Image", WINDOW_AUTOSIZE );
67       imshow( "Display Image", image );
68
69       waitKey(0);
70
71       return 0;
72     }
73     @endcode
74 -#  We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are.
75     For this, do the following:
76
77     -   Go to **Project--\>Properties**
78     -   In **C/C++ Build**, click on **Settings**. At the right, choose the **Tool Settings** Tab.
79         Here we will enter the headers and libraries info:
80         -#  In **GCC C++ Compiler**, go to **Includes**. In **Include paths(-l)** you should
81             include the path of the folder where opencv was installed. In our example, this is
82             /usr/local/include/opencv.
83
84             ![](images/a9.png)
85
86             @note If you do not know where your opencv files are, open the **Terminal** and type:
87             @code{.bash}
88             pkg-config --cflags opencv
89             @endcode
90             For instance, that command gave me this output:
91             @code{.bash}
92             -I/usr/local/include/opencv -I/usr/local/include
93             @endcode
94
95         -#  Now go to **GCC C++ Linker**,there you have to fill two spaces:
96
97             First in **Library search path (-L)** you have to write the path to where the opencv libraries
98             reside, in my case the path is: :
99
100                 /usr/local/lib
101
102             Then in **Libraries(-l)** add the OpenCV libraries that you may need. Usually just the 4 first
103             on the list below are enough (for simple applications) . In my case, I am putting all of them
104             since I plan to use the whole bunch:
105
106             opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_ml opencv_videoio opencv_video opencv_features2d
107             opencv_calib3d opencv_objdetect opencv_flann
108
109             ![](images/a10.png)
110
111             If you don't know where your libraries are (or you are just psychotic and want to make sure
112             the path is fine), type in **Terminal**:
113             @code{.bash}
114             pkg-config --libs opencv
115             @endcode
116             My output (in case you want to check) was:
117             @code{.bash}
118             -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann
119             @endcode
120             Now you are done. Click **OK**
121
122 -   Your project should be ready to be built. For this, go to **Project-\>Build all**
123
124     In the Console you should get something like
125
126     ![](images/a12.png)
127
128     If you check in your folder, there should be an executable there.
129
130 Running the executable
131 ----------------------
132
133 So, now we have an executable ready to run. If we were to use the Terminal, we would probably do
134 something like:
135 @code{.bash}
136 cd <DisplayImage_directory>
137 cd src
138 ./DisplayImage ../images/HappyLittleFish.png
139 @endcode
140 Assuming that the image to use as the argument would be located in
141 \<DisplayImage_directory\>/images/HappyLittleFish.png. We can still do this, but let's do it from
142 Eclipse:
143
144 -#  Go to **Run-\>Run Configurations**
145 -#  Under C/C++ Application you will see the name of your executable + Debug (if not, click over
146     C/C++ Application a couple of times). Select the name (in this case **DisplayImage Debug**).
147 -#  Now, in the right side of the window, choose the **Arguments** Tab. Write the path of the image
148     file we want to open (path relative to the workspace/DisplayImage folder). Let's use
149     **HappyLittleFish.png**:
150
151     ![](images/a14.png)
152
153 -#  Click on the **Apply** button and then in Run. An OpenCV window should pop up with the fish
154     image (or whatever you used).
155
156     ![](images/a15.jpg)
157
158 -#  Congratulations! You are ready to have fun with OpenCV using Eclipse.
159
160 ### V2: Using CMake+OpenCV with Eclipse (plugin CDT)
161
162 Say you have or create a new file, *helloworld.cpp* in a directory called *foo*:
163 @code{.cpp}
164 #include <opencv2/opencv.hpp>
165 using namespace cv;
166
167 int main ( int argc, char **argv )
168 {
169   Mat img(480, 640, CV_8U);
170   putText(img, "Hello World!", Point( 200, 400 ), FONT_HERSHEY_SIMPLEX | FONT_ITALIC, 1.0, Scalar( 255, 255, 0 ));
171   imshow("My Window", img);
172   waitKey();
173   return 0;
174 }
175 @endcode
176 -#  Create a build directory, say, under *foo*: mkdir /build. Then cd build.
177 -#  Put a `CmakeLists.txt` file in build:
178 @code{.bash}
179 PROJECT( helloworld_proj )
180 FIND_PACKAGE( OpenCV REQUIRED )
181 ADD_EXECUTABLE( helloworld helloworld.cxx )
182 TARGET_LINK_LIBRARIES( helloworld \f${OpenCV_LIBS} )
183 @endcode
184 -#  Run: cmake-gui .. and make sure you fill in where opencv was built.
185 -#  Then click configure and then generate. If it's OK, **quit cmake-gui**
186 -#  Run `make -j4` (the -j4 is optional, it just tells the compiler to build in 4 threads). Make
187     sure it builds.
188 -#  Start eclipse. Put the workspace in some directory but **not** in foo or `foo\build`
189 -#  Right click in the Project Explorer section. Select Import And then open the C/C++ filter.
190     Choose *Existing Code* as a Makefile Project.
191 -#  Name your project, say *helloworld*. Browse to the Existing Code location `foo\build` (where
192     you ran your cmake-gui from). Select *Linux GCC* in the *"Toolchain for Indexer Settings"* and
193     press *Finish*.
194 -#  Right click in the Project Explorer section. Select Properties. Under C/C++ Build, set the
195     *build directory:* from something like `${workspace_loc:/helloworld}` to
196     `${workspace_loc:/helloworld}/build` since that's where you are building to.
197
198     -#  You can also optionally modify the Build command: from make to something like
199         `make VERBOSE=1 -j4` which tells the compiler to produce detailed symbol files for debugging and
200         also to compile in 4 parallel threads.
201
202 -#  Done!