[go: up one dir, main page]

100% found this document useful (1 vote)
114 views5 pages

The Opencv Tutorials, Release 2.4.13.7: #Include #Include

This document provides instructions for setting up OpenCV with the Eclipse integrated development environment (IDE) on Linux using CMake. It describes how to create a C/C++ project in Eclipse for a sample OpenCV application, configure the project to find the OpenCV headers and libraries, build and run the application. It also provides an alternative method using CMake directly from the command line instead of Eclipse.

Uploaded by

tyborman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
114 views5 pages

The Opencv Tutorials, Release 2.4.13.7: #Include #Include

This document provides instructions for setting up OpenCV with the Eclipse integrated development environment (IDE) on Linux using CMake. It describes how to create a C/C++ project in Eclipse for a sample OpenCV application, configure the project to find the OpenCV headers and libraries, build and run the application. It also provides an alternative method using CMake directly from the command line instead of Eclipse.

Uploaded by

tyborman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The OpenCV Tutorials, Release 2.4.13.

• 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

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):
#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )


{
Mat image;
image = imread( argv[1], 1 );

if( argc != 2 || !image.data )


{
printf( "No image data \n" );
return -1;
}

namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );


imshow( "Display Image", image );

waitKey(0);

return 0;
}

14 Chapter 1. Introduction to OpenCV


The OpenCV Tutorials, Release 2.4.13.7

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

Note: If you do not know where your opencv files are, open the Terminal and type:

pkg-config --cflags opencv

For instance, that command gave me this output:

-I/usr/local/include/opencv -I/usr/local/include

(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:

/usr/local/lib

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:
opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d
opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann

1.3. Using OpenCV with Eclipse (plugin CDT) 15


The OpenCV Tutorials, Release 2.4.13.7

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:

pkg-config --libs opencv

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


-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
Now you are done. Click OK
• Your project should be ready to be built. For this, go to Project->Build all
In the Console you should get something like

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:

16 Chapter 1. Introduction to OpenCV


The OpenCV Tutorials, Release 2.4.13.7

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

Assuming that the image to use as the argument would be located in <DisplayIm-
age_directory>/images/HappyLittleFish.png. We can still do this, but let’s do it from Eclipse:
1. Go to Run->Run Configurations
2. Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Applica-
tion a couple of times). Select the name (in this case DisplayImage Debug).
3. 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:

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

5. 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:
#include <cv.h>
#include <highgui.h>
int main ( int argc, char **argv )
{
cvNamedWindow( "My Window", 1 );
IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
CvFont font;
double hScale = 1.0;
double vScale = 1.0;
int lineWidth = 1;
cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
hScale, vScale, 0, lineWidth );

1.3. Using OpenCV with Eclipse (plugin CDT) 17


The OpenCV Tutorials, Release 2.4.13.7

cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,


cvScalar( 255, 255, 0 ) );
cvShowImage( "My Window", img );
cvWaitKey();
return 0;
}

1. Create a build directory, say, under foo: mkdir /build. Then cd build.
2. Put a CmakeLists.txt file in build:
PROJECT( helloworld_proj )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( helloworld helloworld.cxx )
TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} )

1. Run: cmake-gui .. and make sure you fill in where opencv was built.
2. Then click configure and then generate. If it’s OK, quit cmake-gui
3. Run make -j4 (the ‘‘-j4‘‘ is optional, it just tells the compiler to build in 4 threads). Make sure it builds.
4. Start eclipse . Put the workspace in some directory but not in foo or foo\\build
5. Right click in the Project Explorer section. Select Import And then open the C/C++ filter. Choose Existing
Code as a Makefile Project‘‘
6. 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.
7. Right click in the Project Explorer section. Select Properties. Under C/C++ Build, set the build direc-
tory: from something like ${workspace_loc:/helloworld} to ${workspace_loc:/helloworld}/build
since that’s where you are building to.
1. 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.
1. Done!

Installation in Windows

The description here was tested on Windows 7 SP1. Nevertheless, it should also work on any other relatively modern
version of Windows OS. If you encounter errors after following the steps described below, feel free to contact us via
our OpenCV Q&A forum. We’ll do our best to help you out.

Note: To use the OpenCV library you have two options: Installation by Using the Pre-built Libraries or Installation
by Making Your Own Libraries from the Source Files. While the first one is easier to complete, it only works if you
are coding with the latest Microsoft Visual Studio IDE and doesn’t take advantage of the most advanced technologies
we integrate into our library.

Installation by Using the Pre-built Libraries

1. Launch a web browser of choice and go to our page on Sourceforge.


2. Choose a build you want to use and download it.

18 Chapter 1. Introduction to OpenCV

You might also like