![]() |
OpenCV
4.5.2
Open Source Computer Vision
|
Prev Tutorial: Using OpenCV with gcc and CMake
Next Tutorial: Installation in Windows
Original author | Ana Huamán |
Compatibility | OpenCV >= 3.0 |
Two ways, one by forming a project directly, and another by CMake Prerequisites
Go to File -> New -> C/C++ Project
Choose a name for your project (i.e. DisplayImage). An Empty Project should be okay for this example.
Your project (in this case DisplayImage) should appear in the Project Navigator (usually at the left side of your window).
Right click on DisplayImage (in the Navigator). New -> Folder .
Call it DisplayImage.cpp. Hit Finish
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.
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 4 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_imgcodecs opencv_highgui opencv_ml opencv_videoio opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_flann
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:
My output (in case you want to check) was:
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.
So, now we have an executable ready to run. If we were to use the Terminal, we would probably do something like:
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:
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:
Click on the Apply button and then in Run. An OpenCV window should pop up with the fish image (or whatever you used).
Say you have or create a new file, helloworld.cpp in a directory called foo:
CmakeLists.txt
file in build: make -j4
(the -j4 is optional, it just tells the compiler to build in 4 threads). Make sure it builds.foo\build
foo\build
(where you ran your cmake-gui from). Select Linux GCC in the *"Toolchain for Indexer Settings"* and press Finish.${workspace_loc:/helloworld}
to ${workspace_loc:/helloworld}/build
since that's where you are building to.make VERBOSE=1 -j4
which tells the compiler to produce detailed symbol files for debugging and also to compile in 4 parallel threads.