Eclipse IDE for C/C++ Developers - OpenCV 4.2.0 配置
OpenCV 4.2.0 - Modules
https://docs.opencv.org/4.2.0/index.html
OpenCV 4.2.0 - Tutorials
https://docs.opencv.org/4.2.0/d9/df8/tutorial_root.html
0. Using OpenCV with Eclipse (plugin CDT)
OpenCV modules -> OpenCV Tutorials -> Introduction to OpenCV
https://docs.opencv.org/4.2.0/d7/d16/tutorial_linux_eclipse.html
Eclipse CDT (C/C++ Development Tooling)
1. Prerequisites
prerequisite [ˌpriːˈrekwəzɪt]:n. 先决条件,前提,必备条件
Eclipse IDE
https://www.eclipse.org/
OpenCV 4.2.0 installation in Linux
https://docs.opencv.org/4.2.0/d7/d9f/tutorial_linux_install.html
Eclipse IDE for C/C++ Developers
https://www.eclipse.org/downloads/packages/
2. Making a project
-
Start Eclipse.
-
Go to
File -> New -> C/C++ Project
-
Choose a name for your project (i.e.
DisplayImage
). AnEmpty Project
should be okay for this example.
-
Leave everything else by default. Press
Finish
. -
Your project (in this case
DisplayImage
) should appear in theProject Navigator
(usually at the left side of your window).
-
Now, let’s add a source file using OpenCV:
-
Right click on
DisplayImage
(in the Navigator).New -> Folder
.
-
Name your folder
src
and then hitFinish
-
Right click on your newly created
src
folder. ChooseNew source file
: -
Call it
DisplayImage.cpp
. HitFinish
- 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):
//============================================================================
// Name : using namespace cv;
// Author : Yongqiang Cheng
// Version : Feb 22, 2020
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <opencv2/opencv.hpp>
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", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
- We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are.
fatal error: opencv2/opencv.hpp: No such file or directory
- Go to
Project -> Properties
- In
C/C++ Build
, click onSettings
. At the right, choose theTool Settings
Tab. Here we will enter the headers and libraries info: - In
GCC C++ Compiler
, go toIncludes
. InInclude paths(-l)
you should include the path of the folder where opencv was installed.
/usr/local/include
/usr/local/include/opencv4
/usr/local/include/opencv4/opencv2
If you do not know where your opencv files are, open the Terminal and type:
pkg-config --cflags opencv // opencv 2.x, opencv 3.x
pkg-config --cflags opencv4 // opencv 4.x
For instance, that command gave me this output:
strong@foreverstrong:~$ pkg-config --cflags opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4
strong@foreverstrong:~$
- Now go to
GCC C++ Linker
, there you have to fill two spaces:
First inLibrary 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_gapi opencv_stitching opencv_face opencv_dnn_superres opencv_surface_matching opencv_quality opencv_reg opencv_stereo opencv_tracking opencv_videostab opencv_rgbd opencv_freetype opencv_dpm opencv_ccalib opencv_saliency opencv_hfs opencv_hdf opencv_line_descriptor opencv_structured_light opencv_viz opencv_phase_unwrapping opencv_xobjdetect opencv_objdetect opencv_superres opencv_optflow opencv_ximgproc opencv_sfm opencv_xfeatures2d opencv_plot opencv_xphoto opencv_photo opencv_fuzzy opencv_bgsegm opencv_video opencv_aruco opencv_datasets opencv_text opencv_ml opencv_cvv opencv_dnn_objdetect opencv_dnn opencv_bioinspired opencv_highgui opencv_videoio opencv_imgcodecs opencv_shape opencv_calib3d opencv_features2d opencv_flann opencv_img_hash opencv_imgproc opencv_core
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 // opencv 2.x, opencv 3.x
pkg-config --libs opencv4 // opencv 4.x
My output (in case you want to check) was:
strong@foreverstrong:~$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_face -lopencv_dnn_superres -lopencv_surface_matching -lopencv_quality -lopencv_reg -lopencv_stereo -lopencv_tracking -lopencv_videostab -lopencv_rgbd -lopencv_freetype -lopencv_dpm -lopencv_ccalib -lopencv_saliency -lopencv_hfs -lopencv_hdf -lopencv_line_descriptor -lopencv_structured_light -lopencv_viz -lopencv_phase_unwrapping -lopencv_xobjdetect -lopencv_objdetect -lopencv_superres -lopencv_optflow -lopencv_ximgproc -lopencv_sfm -lopencv_xfeatures2d -lopencv_plot -lopencv_xphoto -lopencv_photo -lopencv_fuzzy -lopencv_bgsegm -lopencv_video -lopencv_aruco -lopencv_datasets -lopencv_text -lopencv_ml -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_shape -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_img_hash -lopencv_imgproc -lopencv_core
strong@foreverstrong:~$
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
16:21:55 **** Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage" ./src/DisplayImage.o -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
16:21:58 Build Finished (took 2s.981ms)
If you check in your folder, there should be an executable there.
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ pwd
/home/strong/eclipse-work/DisplayImage/Debug
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ls -l ./DisplayImage
-rwxrwxr-x 1 strong strong 714808 Feb 22 16:21 ./DisplayImage
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
3. 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:
cd <DisplayImage_directory>
cd debug
./DisplayImage ../images/person.jpg
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ pwd
/home/strong/eclipse-work/DisplayImage/Debug
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ls -l ./DisplayImage
-rwxrwxr-x 1 strong strong 714808 Feb 22 16:21 ./DisplayImage
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ./DisplayImage ../images/person.jpg
QXcbConnection: XCB error: 148 (Unknown), sequence: 172, resource id: 0, major code: 140 (Unknown), minor code: 20
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
Assuming that the image to use as the argument would be located in <DisplayImage_directory>/images/person.png
. We can still do this, but let’s do it from Eclipse:
-
Go to
Run -> Run Configurations
-
Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Application a couple of times). Select the name (in this case
DisplayImage Debug
).
-
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).
-
Click on the
Apply
button and then in Run. An OpenCV window should pop up with the fish image (or whatever you used). -
Congratulations! You are ready to have fun with OpenCV using Eclipse.
QXcbConnection: XCB error: 148 (Unknown), sequence: 172, resource id: 0, major code: 140 (Unknown), minor code: 20
4. Example
//============================================================================
// Name : using namespace cv;
// Author : Yongqiang Cheng
// Version : Feb 22, 2020
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv)
{
Mat img(480, 640, CV_8U);
putText(img, "Hello World!", Point(200, 400), FONT_HERSHEY_SIMPLEX | FONT_ITALIC, 1.0, Scalar(255, 255, 0));
imshow("My Window", img);
waitKey();
return 0;
}
18:54:46 **** Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage" ./src/DisplayImage.o -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
18:54:49 Build Finished (took 3s.139ms)