1 Using OpenCV with Eclipse (plugin CDT) {#tutorial_linux_eclipse}
2 ======================================
4 @prev_tutorial{tutorial_linux_gcc_cmake}
5 @next_tutorial{tutorial_windows_install}
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".
21 -# Start Eclipse. Just run the executable that comes in the folder.
22 -# Go to **File -\> New -\> C/C++ Project**
26 -# Choose a name for your project (i.e. DisplayImage). An **Empty Project** should be okay for this
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).
37 -# Now, let's add a source file using OpenCV:
38 - Right click on **DisplayImage** (in the Navigator). **New -\> Folder** .
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**
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):
51 #include <opencv2/opencv.hpp>
55 int main( int argc, char** argv )
58 image = imread( argv[1], 1 );
60 if( argc != 2 || !image.data )
62 printf( "No image data \n" );
66 namedWindow( "Display Image", WINDOW_AUTOSIZE );
67 imshow( "Display Image", image );
74 -# We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are.
75 For this, do the following:
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.
86 @note If you do not know where your opencv files are, open the **Terminal** and type:
88 pkg-config --cflags opencv
90 For instance, that command gave me this output:
92 -I/usr/local/include/opencv -I/usr/local/include
95 -# Now go to **GCC C++ Linker**,there you have to fill two spaces:
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: :
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:
106 opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_ml opencv_videoio opencv_video opencv_features2d
107 opencv_calib3d opencv_objdetect opencv_flann
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**:
114 pkg-config --libs opencv
116 My output (in case you want to check) was:
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
120 Now you are done. Click **OK**
122 - Your project should be ready to be built. For this, go to **Project-\>Build all**
124 In the Console you should get something like
128 If you check in your folder, there should be an executable there.
130 Running the executable
131 ----------------------
133 So, now we have an executable ready to run. If we were to use the Terminal, we would probably do
136 cd <DisplayImage_directory>
138 ./DisplayImage ../images/HappyLittleFish.png
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
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**:
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).
158 -# Congratulations! You are ready to have fun with OpenCV using Eclipse.
160 ### V2: Using CMake+OpenCV with Eclipse (plugin CDT)
162 Say you have or create a new file, *helloworld.cpp* in a directory called *foo*:
164 #include <opencv2/opencv.hpp>
167 int main ( int argc, char **argv )
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);
176 -# Create a build directory, say, under *foo*: mkdir /build. Then cd build.
177 -# Put a `CmakeLists.txt` file in build:
179 PROJECT( helloworld_proj )
180 FIND_PACKAGE( OpenCV REQUIRED )
181 ADD_EXECUTABLE( helloworld helloworld.cxx )
182 TARGET_LINK_LIBRARIES( helloworld \f${OpenCV_LIBS} )
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
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
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.
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.