QT installation OpenCv step process and related issues (based on QT Creator programming under Windows)

Preface

   The process of installing the OpenCV environment in QT is relatively complicated. If netizens want to develop OpenCV in QT, they need to patiently follow the steps I listed to install the environment step by step, which includes the pitfalls I encountered when installing the environment. , have all been listed for you in the steps. If any netizens don’t understand, you can comment or send me a private message!

1. Software preparation

1. Qt software
2. CMake
3. OpenCV

2. Environment construction

1. Install QT: version 5.12.4 (the version I use)
(1) Install on the D drive (choose any drive)
(2) Add the QT environment variable to the system environment variable PATH.
Note that you need to add the following two Add the QT environment variable to the system PATH:
D:\QTCreator_Windows\5.12.4\mingw73_64\bin
D:\QTCreator_Windows\Tools\mingw730_64\bin
2. Install the CMake
official website:
https://cmake.org/files/
No version required :
v3.12/ (the version I use)----cmake-3.12…4-win64-64.zip
(1) Install on the D drive (choose a drive at will, preferably in the same drive as QT)
( 2) Add the CMake environment variable to the system environment variable PATH
D:\Opencv_Window\cmake-3.12.4-win64-x64\bin

3. Install opencv
official website:
https://opencv.org/releases
Version:
OpenCV-3.4.9 (the version I use)—Select Windows–Automatically start downloading .exe (opencv-3.4.9-vc14_vc15.exe)
(1 ) Place opencv-3.4.9-vc14_vc15.exe on the D drive
(2) Double-click to run and decompress opencv-3.4.9-vc14_vc15.exe.
Two files will appear:
build file: This file is compiled under the Windows environment after compilation. Executable
sources file: This file is uncompiled source code. If you want to run opencv in QT, you need to recompile the code in this file (so at this time, you need to use the previously downloaded CMake to recompile the opencv source code)
( 3) cmake configures opencv
a. Open D:\Opencv_Window\cmake-3.12.4-win64-x64\bin\cmake-gui.exe
b. Add the sources file in opencv
where is the souce code: D:/Opencv_Window /opencv/sources
where is build the binaries: D:/Opencv_Window/opencv/rebuild_for_qt (Note: the rebuild_for_qt file is a file we created ourselves to store the compiled code) c. Click configure to start the configuration. In the pop-up
interface
:
》Specify the generator for the project, select: MinGw makefiles
》 Then select: Specify native compilers
》Click next
in the pop-up interface
》C: Select GCC under the QT path
: D:/QTCreator_Windows/Tools/mingw730_64/bin/gcc .exe
》C++: Select G++ under the QT path
: D:/QTCreator_Windows/Tools/mingw730_64/bin/g++.exe
》Click Finish to start configuration

在点击Finish配置时出现:
	由于找不到libwinpthread-1.dll,无法继续执行代码,重新安装可能会解决此问题
说明:QT的环境变量未添加到系统环境PATH中(我当时添加的时候是用了中文字符的冒号所以报错

》Configuring done appears after configuration
》Modify the configuration again
ENABLE_CXX11 (checked)
WITH_OPENGL (checked)
WITH_QT (checked)
WITH_IPP (unchecked)
Other choices can be defaulted
》Click Configure again
》Click Configure again
and select Name CMAKE_CONFIGURATION_TYPES, delete Debug under Value, leaving only Release.
Continue to click Configure

这时候会报错提示FFMPEG下载失败,无法连接到服务器,打开CMakeDownloadLog.txt查看详细信息----这会导致编译后的OpenCV无法用VideoCapture打开视频(报错内容:CMake Warning at cmake/OpenCVDownload.cmake:202 (message): FFMPEG: Download failed: 7;"Couldn't connect to server"For details please refer to the download log file: D:/Opencv_Window/opencv/rebuild_for_qt/CMakeDownloadLog.txt
Call Stack (most recent call first): 3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)cmake/OpenCVFindLibsVideo.cmake:227 (download_win_ffmpeg) CMakeLists.txt:683 (include)
查看CMakeDownloadLog.txt需要以下3个文件:
opencv_ffmpeg.dll文件
opencv_ffmpeg_64.dll文件
ffmpeg_version.cmake文件
            ***************** The above question is very important******************
The problem is solved as follows:

(1) View the D:\Opencv_Window\opencv\rebuild_for_qt\CMakeDownloadLog.txt text generated after CMake compilation . The text content is as follows (the solution is where I commented in the text content):

#use_cache "D:/Opencv_Window/opencv/sources/.cache" 
#do_copy "opencv_ffmpeg.dll" "168635a192fc2d2ae7d7d40150f0b349" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg.dll" "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg"
//上面是说需要将缺省的“opencv_ffmpeg.dll”文件拷贝到“D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg”路径下
#missing "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg/opencv_ffmpeg.dll"
#check_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/168635a192fc2d2ae7d7d40150f0b349-opencv_ffmpeg.dll"
//这一行是说可以将缺省的“opencv_ffmpeg.dll”文件以MD5编码命名(例如:168635a192fc2d2ae7d7d40150f0b349-opencv_ffmpeg.dll--该形式就是MD5编码命名,后面你只需要下载opencv_ffmpeg.dll文件然后在该文件前复制“168635a192fc2d2ae7d7d40150f0b349-”加上即可)之后拷贝到“D:/Opencv_Window/opencv/sources/.cache/ffmpeg/”路径下
#mismatch_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/168635a192fc2d2ae7d7d40150f0b349-opencv_ffmpeg.dll" "d41d8cd98f00b204e9800998ecf8427e"
#delete "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/168635a192fc2d2ae7d7d40150f0b349-opencv_ffmpeg.dll"
#cmake_download "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/168635a192fc2d2ae7d7d40150f0b349-opencv_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg.dll"
#try 1
# timeout on name lookup is not supported
#   Trying 0.0.0.0...
# TCP_NODELAY set
# connect to 0.0.0.0 port 443 failed: Address not available
# Failed to connect to raw.githubusercontent.com port 443: Address not available
# Closing connection 0
# 

#do_copy "opencv_ffmpeg_64.dll" "cbc25f8f2954475b127c7558558c18e6" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg_64.dll" "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg"
//上面是说需要将缺省的“opencv_ffmpeg_64.dll”文件拷贝到“D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg”路径下
#missing "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg/opencv_ffmpeg_64.dll"
#check_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/cbc25f8f2954475b127c7558558c18e6-opencv_ffmpeg_64.dll"
#mismatch_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/cbc25f8f2954475b127c7558558c18e6-opencv_ffmpeg_64.dll" "d41d8cd98f00b204e9800998ecf8427e"
//这一行是说可以将缺省的“opencv_ffmpeg.dll”文件以MD5编码命名之后拷贝到“D:/Opencv_Window/opencv/sources/.cache/ffmpeg/”路径下
#delete "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/cbc25f8f2954475b127c7558558c18e6-opencv_ffmpeg_64.dll"
#cmake_download "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/cbc25f8f2954475b127c7558558c18e6-opencv_ffmpeg_64.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/opencv_ffmpeg_64.dll"
#try 1
# timeout on name lookup is not supported
#   Trying 0.0.0.0...
# TCP_NODELAY set
# connect to 0.0.0.0 port 443 failed: Address not available
# Failed to connect to raw.githubusercontent.com port 443: Address not available
# Closing connection 0
# 

#do_copy "ffmpeg_version.cmake" "3b90f67f4b429e77d3da36698cef700c" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/ffmpeg_version.cmake" "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg"
//上面是说需要将缺省的“ffmpeg_version.cmake”文件拷贝到“D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg”路径下
#missing "D:/Opencv_Window/opencv/rebuild_for_qt/3rdparty/ffmpeg/ffmpeg_version.cmake"
#check_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/3b90f67f4b429e77d3da36698cef700c-ffmpeg_version.cmake"
#mismatch_md5 "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/3b90f67f4b429e77d3da36698cef700c-ffmpeg_version.cmake" "d41d8cd98f00b204e9800998ecf8427e"
//这一行是说可以将缺省的“ffmpeg_version.cmakel”文件以MD5编码命名之后拷贝到“D:/Opencv_Window/opencv/sources/.cache/ffmpeg/”路径下
#delete "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/3b90f67f4b429e77d3da36698cef700c-ffmpeg_version.cmake"
#cmake_download "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/3b90f67f4b429e77d3da36698cef700c-ffmpeg_version.cmake" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/548e3c997a80d65f710b9048f1d33371e3a203ac/ffmpeg/ffmpeg_version.cmake"
#try 1
# timeout on name lookup is not supported
#   Trying 0.0.0.0...
# TCP_NODELAY set
# connect to 0.0.0.0 port 443 failed: Address not available
# Failed to connect to raw.githubusercontent.com port 443: Address not available
# Closing connection 0
# 

(2) Download the following 3 files
opencv_ffmpeg.dll file
download link: https://raw.githubusercontent.com
opencv_ffmpeg_64.dll file
download link: https://raw.githubusercontent.com
ffmpeg_version.cmake file
download link: https:/ /raw.githubusercontent.com
(3) After downloading the above three files, save the three files in the MD5 naming method (already mentioned before) to "D:/Opencv_Window/opencv/sources/.cache/ffmpeg/" path
(4) Click Configure in CMake. After success, click generate to solve the problem.
4. Compile and install opencv
(1) Open the CMD terminal of Windows
and use the command to switch to the newly compiled one in the installed opencv path. rebuild_for_qt folder
namely:

>>D:
>>cd Opencv_Window\opencv\rebuild_for_qt 
注:window下的盘符切换与Linux有区别,先输入“D:”切换到D盘,然后再使用cd命令具体切换到哪个目录

(2) Enter the command to compile opencv (it takes about 10 minutes to compile),
namely:
    mingw32-make // Wait for the compilation to complete after input.
(3) After the compilation is completed, enter the command to download the opencv library,
that is:
    mingw32-make install // Re-install after success. The compiled opencv environment is added to the system PATH
(4) Add the new opencv environment to the PATH,
that is:

将:
D:\Opencv_Window\opencv\rebuild_for_qt\bin
D:\Opencv_Window\opencv\rebuild_for_qt\install\x64\mingw\bin
添加到系统PATH中

3. Configure QT project to verify opencv

1. Use QTCreator to create a QT project.
2. Add the following code to the QT project file.pro:

INCLUDEPATH +=D:\Opencv_Window\opencv\rebuild_for_qt\install\include
              D:\Opencv_Window\opencv\rebuild_for_qt\install\include\opencv
              D:\Opencv_Window\opencv\rebuild_for_qt\install\include\opencv2

LIBS  +=-L D:\Opencv_Window\opencv\rebuild_for_qt\install\x64\mingw\lib\libopencv_*.dll.a  //意思是将编译好的所有opencv库添加到QT中

3. Run the opencv code and it will run successfully.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Mat image = imread("the photo road!!!");//注意imread函数、waitkey函数都是属于cv命名空间里的内容,所以程序之前要添加using namespace cv
    namedWindow("Display window",WINDOW_AUTOSIZE);
    imshow("Display window",image);
    waitKey(0);
    Dialog w;
    w.show();
    return a.exec();
}

Guess you like

Origin blog.csdn.net/Mr_zhang1911116/article/details/126891870