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