原文:https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
先下好工具
Qt5.7.1
CMake 3.10.2
opencv-3.3.0-vc14
Qt下载地址集合
Qt安装
Cmake安装
opencv解压出来
使用CMake配置OpenCv
添加minGW到环境变量
右键“我的电脑”——属性——高级系统设置——环境变量,我的路径是C:\Qt\Qt5.7.1\Tools\mingw530_32\bin,如图所示
配置
打开CMake,设置源码的路径和输出路径,然后点击Configure,选择如所示
Next,配置c和c++,我的路径如下
C:/Qt/Qt5.7.1/Tools/mingw530_32/bin/gcc.exe
C:/Qt/Qt5.7.1/Tools/mingw530_32/bin/g++.exe
点击Finish,等待加载完毕,暴很多红项
在Search框内搜索WITH_QT和WITH_OPENGL,都打上勾,再点击Configure
此时还有这几项是红的,点击ADD Entry,添加“QT_MAKE_EXECUTABLE”,并选择qmake的路径如图所示
在Search搜索CMAKE_BUILD_TYPE,修改value成 Release 或RelWithDebInfo
再次点击Configure,然后点击Generate
编译
打开cmd命令提示符,进入输出文件夹并编译
d:
cd tmp\opencv-build
mingw32-make -j 8
这里的8根据自己电脑配置而定,8核cpu 我这里填8
如果编译过程中出现类似这样的错误:
... windres.exe: unknown option -- W ...
解决办法:在CMake搜索ENABLE_PRECOMPILED_HEADERS,把这个项的打勾去掉,然后点Configure,Generate,再执行编译:ming32-make -j 8
如果遇到类似这样的错误
opencv/sources/modules/videoio/src/cap_dshow.cpp, you have the following error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
解决办法:根据提示的路径去opencv源码文件找cap_dshow.cpp文件,我的在D:\tmp\opencv\sources\modules\videoio\src文件夹里,用记事本打开cap_dshow.cpp,搜索
#include "DShow.h"
然后在上面添加
#define NO_DSHOW_STRSAFE
再次执行编译:ming32-make -j 8
执行安装
mingw32-make install
添加到环境变量
方法同上,把D:\tmp\opencv-build\install\x86\mingw\bin添加到path里
测试例子
打开Qt,新建一个项目,Qt Widgets Application
调试
如果出现错误:
Could not create directory "C:\data\qt\Error in " Util.asciify("build-untitled12312-Desktop_Qt_5_7_1_MinGW_32bit-Debug")": TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension(0xe10afa0) is not a function" Error while building/deploying project untitled12312 (kit: Desktop Qt 5.7.1 MinGW 32bit) When executing step "qmake"��
解决办法:点击左侧的项目,填好构建目录,如图所示:
修改.pro文件,添加
INCLUDEPATH +=G:\data\opencv\opencv\build\include
LIBS +=G:\data\opencv\build\bin\libopencv_core330.dll
LIBS +=G:\data\opencv\build\bin\libopencv_highgui330.dll
LIBS +=G:\data\opencv\build\bin\libopencv_imgcodecs330.dll
LIBS +=G:\data\opencv\build\bin\libopencv_imgproc330.dll
LIBS +=G:\data\opencv\build\bin\libopencv_features2d330.dll
LIBS +=G:\data\opencv\build\bin\libopencv_calib3d330.dll
这里需要注意一下330,有些版本是320
修改完后,点击构建 ,执行qmake
修改mainwindow.cpp
效果: