illustrate:
I found a lot of tutorials on the Internet, but opencv couldn't compile it. In the end, I stepped on a lot of pitfalls and summarized my experience to share with you.
This article does not describe much about the installation of Qt. If you have installed Qt, then you don’t need to install mingw and cmake tools. You only need to install opencv. opencv download address:
The environment used in this article is opencv 4.5.2 + Qt 5.15.2(mingw)
1. Install the opencv source code and prepare the environment:
Check environment variables:
Now it is necessary to add these three environment variables. I deleted the previous environment variable of mingw32, and the newly added 64 is recommended to be deleted, otherwise the compilation may fail.
2. Start to generate makefile:
Open the cmake tool that comes with Qt
Add the value, if you don't add it, it is very likely that the compilation will fail because of the version problem. The specific error link is:
Linking CXX shared library …\bin\libopencv_core451.dll (error at this step)
After clicking configure, set some things:
Start config after clicking finish
When the config step is completed, there will be a large area of red, don't panic
Continue to click configure, it will not be red at this time (you can clear the search box to have a look), and then click Generate to generate the makefile.
When Generate is complete, you can close cmake
3. Compile:
Enter the folder we just created, and after the above operation, the folder already has content, and then we will compile this folder.
Right click on shifi+blank space in the current folder to open power shell
mingw32-make -j8 (j8 means compiling with 8 threads, if your computer performance is good, you can use j16)
This process takes quite a long time.
Start compilation like this:
There will be a lot of warning messages during compilation, just ignore them, no need to do anything
Compilation is done like this:
Do not close the window when the compilation is complete, use the install command to start the installation
mingw32-make install
Add environment variables after installation
Note that this opencv-build folder is the folder we just created
4. use
Remember remember! ! ! Use the mingw64 compilation suite! ! ! , otherwise the main function of the program will not be able to enter, and an error will be reported directly.
I tried both debug and release versions, no problem.
Add library to Qt pro file
INCLUDEPATH += C:\opencv\opencv\opencv-build\install\include
LIBS += C:\opencv\opencv\opencv-build\install\x64\mingw\bin\libopencv_world452.dll
Call the library file:
Mat image = imread("C:\\Users\\WuYue\\Desktop\\img.jpg",1);//本地图片路径
namedWindow("my image",WINDOW_AUTOSIZE);//新建自适应大小的窗体
imshow("my image",image);//显示窗体
Effect:
So far, the installation is complete. Let me make a statement. The logic of this article is not original. After I encountered this problem at work, I started to try, stepped on the pit, and summarized the process. I mainly use the following two articles:
https://blog.csdn.net/qq_42670606/article/details/122325528
https://blog.csdn.net/weixin_40011280/article/details/117766191