前言
QT里安装OpenCV环境过程比较复杂,如果网友们想要在QT里做OpenCV的开发,就需要耐心的跟着我列出来的操作步骤一步一步去安装环境,其中包含我在安装环境时遇到的坑,都已经在步骤中帮大家罗列出来,如有不懂的网友,可以评论或者私信我即可!
一、软件准备
1、Qt软件
2、CMake
3、OpenCV
二、环境搭建
1、安装QT:5.12.4版本(我用的版本)
(1)安装在D盘(任意选择一个盘)
(2)将QT环境变量添加到系统环境变量PATH中
注意的是需要将以下两个QT环境变量添加到系统PATH中:
D:\QTCreator_Windows\5.12.4\mingw73_64\bin
D:\QTCreator_Windows\Tools\mingw730_64\bin
2、安装CMake
官网:
https://cmake.org/files/
版本不作要求:
v3.12/(我用的版本)----cmake-3.12…4-win64-64.zip
(1)安装在D盘(任意选择一个盘,最好是与QT在一个盘里)
(2)将CMake环境变量添加到系统环境变量PATH中
D:\Opencv_Window\cmake-3.12.4-win64-x64\bin
3、安装opencv
官网:
https://opencv.org/releases
版本:
OpenCV-3.4.9(我用的版本)—选择Windows–自动开始下载.exe(opencv-3.4.9-vc14_vc15.exe)
(1)将opencv-3.4.9-vc14_vc15.exe放在D盘
(2)双击运行解压opencv-3.4.9-vc14_vc15.exe
会出现两个文件:
build文件:该文件为编译后Windows环境下编译好的可执行文件
sources文件:该文件为未编译过的源代码,要想在QT中运行opencv需要重新编译该文件里的代码(所以这时候就需要用前面下载好的CMake来重新编译opencv源码)
(3)cmake配置opencv
a、打开D:\Opencv_Window\cmake-3.12.4-win64-x64\bin\cmake-gui.exe
b、将opencv中的sources文件添加进来
where is the souce code:D:/Opencv_Window/opencv/sources
where is build the binaries:D:/Opencv_Window/opencv/rebuild_for_qt(注:rebuild_for_qt文件是我们自己建立的文件,用于存放编译后的代码)
c、点击configure开始配置
在弹出的界面中:
》Specify the generator for the project中选择:MinGw makefiles
》再选择:Specify native compilers
》点击next
在弹出的界面中
》C:选择QT路径下的GCC
即:D:/QTCreator_Windows/Tools/mingw730_64/bin/gcc.exe
》C++:选择QT路径下的G++
即:D:/QTCreator_Windows/Tools/mingw730_64/bin/g++.exe
》点击Finish开始配置
在点击Finish配置时出现:
由于找不到libwinpthread-1.dll,无法继续执行代码,重新安装可能会解决此问题
说明:QT的环境变量未添加到系统环境PATH中(我当时添加的时候是用了中文字符的冒号所以报错
》配置好出现Configuring done
》再次修改配置
ENABLE_CXX11(勾选)
WITH_OPENGL(勾选)
WITH_QT(勾选)
WITH_IPP(不勾选)
其他选择默认即可
》再次点击Configure
》又再一次点击Configure
然后选中Name下的CMAKE_CONFIGURATION_TYPES,将后面Value下的Debug给删除掉,只留下Release即可
》继续点击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文件
***************** 上面这个问题非常重要*****************
该问题解决如下:
(1)查看CMake编译后生成的D:\Opencv_Window\opencv\rebuild_for_qt\CMakeDownloadLog.txt文本,文本内容如下(解决方法就是我在文本内容注释的地方):
#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)下载以下3个文件
opencv_ffmpeg.dll文件
下载链接:https://raw.githubusercontent.com
opencv_ffmpeg_64.dll文件
下载链接:https://raw.githubusercontent.com
ffmpeg_version.cmake文件
下载链接:https://raw.githubusercontent.com
(3)下载好上面3个文件之后,将3个文件以MD5命名方式(前面已经讲过)存入到“D:/Opencv_Window/opencv/sources/.cache/ffmpeg/”路径下即可
(4)在CMake中点击Configure即可,成功之后点击generate,解决该问题
4、编译安装opencv
(1)打开windows的CMD终端
使用命令切换到安装的opencv路径下刚刚编译好的的rebuild_for_qt文件夹
即:
>>D:
>>cd Opencv_Window\opencv\rebuild_for_qt
注:window下的盘符切换与Linux有区别,先输入“D:”切换到D盘,然后再使用cd命令具体切换到哪个目录
(2)输入命令编译opencv(编译需要10分钟左右)
即:
mingw32-make //输入之后等待编译完成
(3)编译完成之后,输入命令下载opencv库
即:
mingw32-make install //成功之后重新将编译好的opencv环境添加到系统PATH中
(4)添加新的opencv环境到PATH中
即:
将:
D:\Opencv_Window\opencv\rebuild_for_qt\bin
D:\Opencv_Window\opencv\rebuild_for_qt\install\x64\mingw\bin
添加到系统PATH中
三、配置QT工程验证opencv
1、使用QTCreator创建QT工程
2、在QT工程文件.pro中添加以下代码:文章来源:https://www.toymoban.com/news/detail-562505.html
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、运行opencv代码,即成功运行文章来源地址https://www.toymoban.com/news/detail-562505.html
#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();
}
到了这里,关于QT安装OpenCv步骤流程及相关问题(基于Windows下的QT Creator编程)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!