Qt configuration OpenCV tutorial, the pro test has been tried (detailed version)

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/

insert image description here

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
insert image description here

Installation

The installation steps are as follows:
insert image description hereinsert image description hereinsert image description here
insert image description herethe installation location can be customized
insert image description here

OpenCV

Download

The latest version of OpenCV so far is 4.5.1, link: https://opencv.org/releases.html
insert image description here

双击下载后的程序

insert image description here

insert image description hereinsert image description here

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
insert image description here

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
insert image description here

Keep the default MinGW Makefiles, click Specify native compilers, and then next
insert image description here

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不用管

insert image description here

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 即可

insert image description here

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
insert image description here

There will still be red
insert image description here

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
insert image description here

After the Generate is completed, just close Cmake, and then enter the previously created folder D:\OpenCV4.5.1\opencv\opencv-build
insert image description here

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

insert image description here

Once 100% complete, enter

mingw32-make install

insert image description here
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
insert image description here

Test program: use Qt to display pictures

Open Qt and click New Project

insert image description here

By default, the Qt Widgets Application template is used. Directly click Choose at the bottom right
insert image description here
to create the path and project name as shown in the figure. The next step
insert image description here
is to default to MinGW, the next step is to
insert image description here
default all, and the next step is
insert image description here
to complete .
insert image description here

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
insert image description 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
insert image description here
program to display the window and picture
insert image description here

At this point, you're done!

Supongo que te gusta

Origin blog.csdn.net/weixin_43763292/article/details/112975207
Recomendado
Clasificación