一、前言说明
Qt5.9的安装和cmake3.17.2的安装,官网下载安装即可,opencv3.4.5下载源码
Qt官方下载地址
http://download.qt.io/archive/qt/
CMake官网下载地址:
https://cmake.org/download/
OpenCV3.2.0官方下载地址:
http://opencv.org/releases.html#
环境说明:
环境电脑:win7 x64
opencv:3.4.5
cmake:3.17.2
二、环境变量设置
编译器使用qt自带mingw编译器的gcc、g++编译器。之前有装过的mingw编译器的path路径要去掉。因此qt要添加两个路径到path:
C:\Qt\Qt5.9.7\Tools\mingw530_32\bin;C:\Qt\Qt5.9.7\5.9.7\mingw53_32\bin;
三、cmake设置
1.打开cmake-gui程序,选择源码路径和编译的目录
2.点击configure按钮,开始配置
选择mingw和第二单选项,如下图
点击“next”下一步,选择mingw的编译器,如下图本机路径:
C:/Qt/Qt5.9.7/Tools/mingw530_32/bin/gcc.exe
C:/Qt/Qt5.9.7/Tools/mingw530_32/bin/g++.exe
点击“Finish”完成按钮,开始进行配置
等待配置完成,看是否有配置错误,红色提示为错误项,需要配置响应的项
我在这里遇到了下载opencv_ffmpeg.dll动态库失败的情况,根据第四点的第一小项,手动下载此动态库
CMake修改配置
WITH_OPENGL选中,WITH_QT选中,WITH_IPP取消(如果有的话)
然后再点击configure。
CMake为报错项修改值
配置这些剩下一些红色的项需要自己电脑对应的路径修改值:
Qt5Concurrent_DIR C:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5Concurrent
Qt5Core_DIR C:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5Core
Qt5Gui_DIR C:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5Gui
Qt5OpenGL_DIR CC:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5OpenGL
Qt5Test_DIR C:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5Test
Qt5Widgets_DIR C:/Qt/Qt5.9.7/Tools/mingw530_32/lib/cmake/Qt5Widgets
设置好之后再点configure。应该可以看到没有红色的了。这样配置完成了
点击“Generate”按钮进行生成makefile文件,如下图:
打开控制台,cd 到编译目录,如图:
开始执行编译:输入“mingw32-make”命令,按回车进行编译
大概等30分钟左右,显示100%完成,这样opencv的代码就编译完成了,其中遇到问题,请看第四部分,我遇到的几个问题。
如果需要重新编译,先清理命令:mingw32-make clean,再执行命令:mingw32-make
编译完成,安装命令:mingw32-make install ,默认会安装到编译目录“build/install”下:E:\QtProject\opencv-3.4.5\test-build\install
等待安装完成,可以在目录下取到动态库和静态库(E:\QtProject\opencv-3.4.5\test-build\install\x86\mingw\bin), 目录E:\QtProject\opencv-3.4.5\test-build\install\include为头文件的目录,可以根据自己喜好,使用动态库和静态库。
等待安装完成,可以在目录下取到动态库和静态库(E:\QtProject\opencv-3.4.5\test-build\install\x86\mingw\bin), 目录E:\QtProject\opencv-3.4.5\test-build\install\include为头文件的目录,可以根据自己喜好,使用动态库和静态库。
四、编译和配置编译环境中遇到的问题
正常情况下是编译到100%完成的。
我遇到好几处错误:
报错信息如下:
FFMPEG: Download: opencv_ffmpeg.dll
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
FFMPEG: Download failed: 35;"SSL connect error"
For details please refer to the download log file:
D:/opencv/opencv346/contrib_build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
cmake/OpenCVFindLibsVideo.cmake:228 (download_win_ffmpeg)
CMakeLists.txt:793 (include)
提示FFMPEG下载失败,查看CMakeDownloadLog.txt:
解决办法:
手动下载,直接放到目标路径 /3rdparty/ffmpeg中
报错信息如下:
3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:15: error: 'nullptr' was not declared in this scope
return s == nullptr || *s == 0;
^
3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed
解决方案:
在 cmake-gui 编译配置中:
勾选 ENABLE_CXX11
然后重新Configure-Generate-mingw32-make
错误信息如下:
[ 50%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj
In file included from D:/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/DShow.h:33:0,
from E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:111:
E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp: In member function 'void videoInput::getMediaSubtypeAsString(GUID, char*)':
E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:2145:41: error: 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope
if( type == MEDIASUBTYPE_RGB24) sprintf(tmpStr, "RGB24");
^
E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:2145:57: warning: left operand of comma operator has no effect [-Wunused-value]
if( type == MEDIASUBTYPE_RGB24) sprintf(tmpStr, "RGB24");
^
E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:2146:5: error: 'else' without a previous 'if'
else if(type == MEDIASUBTYPE_RGB32) sprintf(tmpStr, "RGB32");
^
In file included from D:/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/DShow.h:33:0,
from E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:111:
E:\software\opencv\opencv-master33\modules\videoio\src\cap_dshow.cpp:2146:41: error: 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope
else if(type == MEDIASUBTYPE_RGB32) sprintf(tmpStr, "RGB32");
解决方案:
在opencv的源码文件cap_dshow.cpp中添加
#define STRSAFE_NO_DEPRECATE
然后重新Configure-Generate-mingw32-make
注意:添加到第一个#include 的下面
编译好的库路径:
https://download.csdn.net/download/tyhenry/12619079
五、Qt中使用opencv配置和动态库使用
INCLUDEPATH += $$PWD/opencv
LIBS += -L$$PWD/opencv/lib -lopencv_core345 -lopencv_imgcodecs345 -lopencv_imgproc345 -lopencv_dnn345 -lopencv_highgui345
opencv目录结构如图:
六、qt测试
建立工程,进行测试,
工程文件.pro中配置,添加头文件目录和动态库
INCLUDEPATH += $$PWD/opencv
LIBS += -L$$PWD/opencv/lib -lopencv_core345 -lopencv_imgcodecs345 -lopencv_imgproc345 -lopencv_dnn345 -lopencv_highgui345
实例代码;
#include
#include "opencv2/opencv.hpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cv::Mat image;
image=cv::imread("D:\\study\\u769376857.jpg");
cv::namedWindow("显示");
cv::imshow("显示",image);
cv::waitKey(0);
return a.exec();
}