OpenCV supports Qt C++ with two compilation methods, MinGW and MSVC.
MSVC: MSVC tutorial link
The following implementation is MinGW: win10 system Qt5.12.9 configuration OpenCV4.5.1 library tutorial Qt, OpenCV, Cmake detailed download and installation tutorial
quotes the original author's experience after trying Improvement instructions: https://blog.csdn.net/weixin_42322013/article/details/ 88808230
software preparation
The first thing to say is that it is better to install the latest software. You don’t need to consider the version correspondence of various software. If they are all the latest versions, they must be compatible with each other. The following specifically describes the required software.
64位win10系统
Qt 5.12.9
Cmake 3.18.0
OpenCV 4.5.1
The following narrative sequence is the installation and configuration sequence
Qt
Download
Domestic mirror websites
Here are a few well-known domestic Qt mirror websites, mainly from various universities:
University of Science and Technology of China: http://mirrors.ustc.edu.cn/qtproject/
Tsinghua University: https://mirrors.tuna.tsinghua.edu.cn/qt/
Beijing Institute of Technology: http://mirror.bit. edu.cn/qtproject/
China Internet Network Information Center: https://mirrors.cnnic.cn/qt/
Download it yourself, the installation is relatively simple, so ignore it first.
Cmake
Download
The latest version of Cmake so far is 3.14.0, link: https://cmake.org/download/ Click the link pointed by the red arrow on the following page to download
Installation
The installation steps are as follows:
The installation location can be customized
OpenCV
Download
The latest version of OpenCV so far is 4.5.1, link: https://opencv.org/releases.html
双击下载后的程序
Click on the sources folder here, and you will find that the files inside are the same as the files decompressed by opencv-4.5.1.zip above. Since we only need to use the things in the sources folder when configuring, you download Sources or Win pack are both available.
[Important]: After adding mingw to the environment variable, you must restart the computer to take effect
右击此电脑
点击属性
点击左上角高级系统设置
点击高级页面下的环境变量
点击系统变量中的Path
点击编辑
点击新建
D:\Qt\Qt5.12.9\Tools\mingw730_64\bin
将该路径加入即可
As shown in the figure: After all the environment variables are added, be sure to restart the computer
Compiling OpenCV
Enter C:\Program Files\CMake\bin, click cmake-gui.exe
Where is the source code: D:\OpenCV4.5.1\opencv\sources
Where to build the binaries: D:\OpenCV4.5.1\opencv\opencv-build(opencv-build这个文件夹要新建)
Click Configure
Keep the default MinGW Makefiles, click Specify native compilers, and then next
Compilers:
C D:/Qt/Qt5.12.9/Tools/mingw730_64/bin/gcc.exe
C++ D:/Qt/Qt5.12.9/Tools/mingw730_64/bin/g++.exe
Fortran不用管
Click Finish
这个时候如果遇到:(没遇到,忽略这个问题)
QT 编译Opencv 一致报错:
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
解决办法:如图所示勾选Advanced,对变量:CMAKE_MAKE_PROGRAM 设置value .QT目录下的mingw32-make.exe 即可
After Configuring done, there will be a large piece of red, don’t worry, just pull down and select WITH_OPENGL and WITH_QT, and then click Configure again
There will still be red
Change the Value with several red bars according to the above settings
Set Qt5Concurrent_DIR to D:\Qt\Qt5.12.1\5.12.1\mingw73_64\lib\cmake\Qt5Concurrent
Set Qt5Core_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5Core
Set Qt5Gui_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5Gui
Set Qt5OpenGL_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5OpenGL
Set Qt5Test_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5Test
Set Qt5Widgets_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5Widgets
Set Qt5_DIR to D:\Qt\Qt5.12.9\5.12.9\mingw73_64\lib\cmake\Qt5
After clicking Configure again, the red color disappears, and finally click Generate
After the Generate is completed, just close Cmake, and then enter the previously created folder D:\OpenCV4.5.1\opencv\opencv-build
Put the mouse in the folder interface, hold down the shift key, and click the right mouse button at the same time, click here to open the Powershell window (s), enter Windows Powershell, enter
mingw32-make -j 8
Press the Enter key to execute the command. The -j 8 here is to allow the CPU to perform multitasking, which can speed up the compilation. My computer is a 4-core CPU, if your configuration is higher, you can also choose a higher number
Once 100% complete, enter
mingw32-make install
Exit after completion.
In addition, add the following path to the environment variable according to the above method.
D:\OpenCV4.5.1\opencv\opencv-build\install\x64\mingw\bin
Test program: use Qt to display pictures
Open Qt and click New Project
By default, the Qt Widgets Application template is used. Just click Choose at the bottom right
to create the path and project name as shown in the figure. The next step
defaults to MinGW, the next step
defaults all, and the next step completes
.
INCLUDEPATH += D:\OpenCV4.5.1\opencv\opencv-build\install\include
LIBS += D:\OpenCV4.5.1\opencv\opencv-build\lib\libopencv_*.a
This is very important, many tutorials have made mistakes here
Add the following code to the red box position in mainwindow.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
Mat image=imread(“E:\Fighting.jpg”,1);//一定要使用绝对路径,其他可以回报错
namedWindow( “Display window”, WINDOW_AUTOSIZE );
imshow( “Display window”, image );
Ignore the red warning prompt that appears at this time, select the menu bar to build, and the red will disappear after executing qmake. Run the
program to display the window and picture
At this point, you're done!