Install opencv3.4.1(or newer version) on windows
1. Double click on the opencv-3.4.2-vc14_vc15.exe and extract it to your favorite folder(pic_00)
Pic00 |
2. Open the folder you extract(assume you extract it to /your_path/opencv_3_4_2). You will see a folder call "opencv" .
Pic01 |
Create a new project by Qt Creator
4. Create a new project
Pic02 |
Pic03 |
6. Enter the path of the folder and name of the project.
Pic04 |
8. You will see a page ask you to select your kits, kits is a tool QtCreator use to group different settings like device, compiler, Qt version etc.
Pic05 |
10. If you see a screen like this, that means you are success.
Pic06 |
11. Write codes to read an image by opencv
#include <iostream> #include <opencv2/core.hpp> #include <opencv2/highgui.hpp> //propose of namespace are //1. Decrease the chance of name collison //2. Help you organizes your codes into logical groups //Without declaring using namespace std, everytime when you are using //the classes, functions in the namespace, you have to call with the //prefix "std::". using namespace cv; using namespace std; /** * main function is the global, designated start function of c++. * @param argc Number of the parameters of command line * @param argv Content of the parameters of command line. * @return any integer within the range of int, meaning of the return value is * defined by the users */ int main(int argc, char *argv[]) { if(argc != 2){ cout<<"Run this example by invoking it like this: "<<endl; cout<<"./step_02.exe lena.jpg"<<endl; cout<<endl; return -1; } //If you execute by Ctrl+R, argv[0] == "step_02.exe", argv[1] == lena.jpg cout<<argv[0]<<","<<argv[1]<<endl; //Open the image auto const img = imread(argv[1]); if(!img.empty()){ imshow("img", img); //Show the image on screen waitKey(); //Do not exist the program until users press a key }else{ cout<<"cannot open image:"<<argv[1]<<endl; return -1; } return 0; //usually we return 0 if everything are normal }
How to compile and link the opencv lib with the help of Qt Creator and qmake
Before you can execute the app, you will need to compile and link to the libraries of opencv. Let me show you how to do it. If you missed steps a and b, you will see a lot of error messages like Pic07 or Pic09 show.
12. Tell the compiler, where are the header files, this could be done by adding following command in the step_02.pro.
INCLUDEPATH += your_install_path_of_opencv/opencv/opencv_3_4_2/opencv/build/include
The compiler will tell you it can't locate the header files if you do not add this line(see Pic07).
Pic07 |
If your INCLUDEPATH is correct, QtCreator should be able to find the headers and use the auto complete to help you type less words(Pic08).
Pic08 |
13. Tell linker which libraries of the opencv it should link to by following command.
LIBS += your_install_path_of_opencv/opencv/opencv_3_4_2/opencv/build/x64/vc14/lib/opencv_world342.lib
Without this step, you will see the errors of "unresolved external symbols"(Pic08).
Pic09 |
Pic10 |
Click the icon surrounded by the red region and change it from debug to release. Why do we do that? Because
- Release mode is much more faster than debug mode in many cases
- The library we link to is build as release library, do not mixed debug and release libraries in your project unless you are asking for trouble
Execute the app
After we compile and link the app, we already have the exe in the folder(in the folder show at Pic11).Pic11 |
We are almost done now, just few more steps the app could up and run.
13. Copy the dll opencv_world342.dll and opencv_ffmpeg342_64.dll(they place in /your_path/opencv/opencv_3_4_2/opencv/build/bin) into a new folder(we called it global_dll).
14. Add the path of this folder into system path. Without step 13 and 14, the exe wouldn't be able to find the dll when we execute the app, and you may see following error when you execute the app from command line(Pic12). I recommend you use the tool--Rapid environment editor(Pic13) to edit your path on windows.
Pic12 |
Pic13 |
Pic14.jpg |
Pic15 |
These are easy but could be annoying at first. I hope this post could leverage your frustration. You can find the source codes located at github.