前言
一、软件版本说明
二、配置过程
1.安装opencv3.4.5
2.安装CMake
3.安装Qt
4.配置编译器路径
5.CMake配置
6.CMake修改配置
7.CMake为报错项修改值
8.CMake生成
9.编译opencv
10.添加生成的bin路径到path环境变量
11.在Qt项目中配置opencv
三、minwg32-setup时遇到的问题及解决方案
qt使用opencv之前,需要使用cmake进行编译,本文具体介绍编译过程。亲测有效!拒绝踩雷!
本机:win10 x64
qt:qt5.9.9
opencv:3.4.5
cmake:3.4.5
在github搜索opencv,下载3.4.5版本,下载exe版。双击exe即解压,解压到D:\MyProgram,即安装完成。
下载路径:https://cmake.org/download/
一直点下一步即安装完成。
qt官网可免费下载安装。
编译器使用qt自带c、c++编译器。之前有装过的mingw编译器的,在环境变量中的path路径要去掉。然后添加两个路径到path:
C:\Qt\Qt5.8.0\5.8\mingw53_32\bin;
C:\Qt\Qt5.8.0\Tools\mingw530_32\bin
打开cmake软件,执行以下操作:
WITH_OPENGL选中,WITH_QT选中,WITH_IPP取消(如果有的话)
然后再点击configure。
刚才配置完成之后,还会剩下一些红色的项需要自己修改值(左边红色为待修改项,右边为修改的参数,要根据自己qt的安装路径进行修改):
Qt5Concurrent_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5Concurrent
Qt5Core_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5Core
Qt5Gui_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5Gui
Qt5OpenGL_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5OpenGL
Qt5Test_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5Test
Qt5Widgets_DIR C:/Qt/Qt5.8.0/5.8/mingw53_32/lib/cmake/Qt5Widgets
设置好之后再点configure。应该可以看到没有红色的了。
点击generate,generate很快就会完成,然后关闭对话框即可。
打开命令行,进入buildOpencv目录,执行mingw32-make命令(也可以输入mingw32-make -j 4(-j 后跟的是用几核编译 提升编译速度)),要执行大概半小时才会完成, 如果需要重新编译,先清理命令:mingw32-make clean,再执行命令:mingw32-make:
此时可能会遇到问题,详细说明和解决方案在后续说明。
然后再执行mingw32-make install
这第二步很快就会完成,然后关闭窗口即可。
将新生成的D:\MyProgram\buildOpencv\bin添加到path变量。
注意:要重启电脑使环境变量生效
创建一个Qt桌面项目;
然后打开.pro进行配置:
INCLUDEPATH += D:\MyProgram\Opencv\opencv\build\include
D:\MyProgram\Opencv\opencv\build\include\opencv
D:\MyProgram\Opencv\opencv\build\include\opencv2
LIBS += D:\MyProgram\buildOpencv\lib\libopencv_*.a
在main.cpp修改代码:
#include "mainwindow.h"
#include
#include
using namespace cv;
int main(int argc, char *argv[])
{
Mat image;
image = imread("D:\\test.jpg");
namedWindow("显示");
imshow("显示",image);
waitKey(0);
return 0;
}
可以看到图片显示出来,测试成功!
1.CMake编译OpenCV3.4.5时opencv_ffmpeg.dll等下载失败的解决办法
报错信息如下:
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:
解决办法:
手动下载,下载网址GitHub Proxy 代理加速 (ghproxy.com)(https://ghproxy.com/),输入CMakeDownloadLog.txt中所述网址(http….)即可下载,下载后直接放到目标路径 /3rdparty/ffmpeg中(没有ffmpeg则创建该文件夹)
2.编译OpenCV3.4.5时出现C++11问题
报错信息如下:
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
3.OPENCV编译Videoio模块问题解决
错误信息如下:
[ 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 的下面