Reference: https: //www.jianshu.com/p/c244c9d01fe0
Many of which are in front of copy, install qt cmake / opencv I will not explain the above sites have introduced
Compile OpenCV
Run cmake-gui.exe, were selected and compile source path of the output path OpenCV
Click Configure, configured as follows, and then next
Compiler path configuration, then finish
C: C:/Qt/Qt5.11.1/Tools/mingw530_32/bin/gcc.exe C++: C:/Qt/Qt5.11.1/Tools/mingw530_32/bin/g++.exe
After the first configure complete, check WITH_QT and WITH_OPENGL, click configure again
After the second configure complete, CMAKE_BUILD_TYPE modified to Release, click configure again
After the third configure complete, click generate, after the end of generation, open cmd, enter the build output directory, the compiler instruction execution (-j represents a multi-threaded job, the computer is a few core CPU, the latter parameter can be set to how much you can to save compilation time)
d: cd OpenCV cd build-Qt mingw32-make -j 4 mingw32-make install
The above question, please refer to the above URL.
Here is the test code:
QWindow a new project called TestOpenCV, modify the project configuration file TestOpenCV.pro, at the end of the file contains the file path and increase the required library files
INCLUDEPATH += D:\openCV\opencv\build\include
LIBS += D:\openCV_out\lib\libopencv_*.a
根据自己实际情况找一下。大佬的网址写的是添加的.dll,应该换成.a文件
在mainwindow.cpp中
#include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // read an image cv::Mat image = cv::imread("D:\\1.jpg", 1); // create image window named "My Image" cv::namedWindow("My Image"); // show the image on window cv::imshow("My Image", image); } MainWindow::~MainWindow() { delete ui; }
上面参考的网站中大佬的文件路径写错了,导致程序崩溃。
主要是大佬的思路,我只是略做修改,感谢大佬的分享。