最终结果展示
笔者使用的版本如下
友情提示:这里Opencv 和opencv_contrib版本需要一致,笔者踩过坑。
Win10 家庭版 64位
Qt Creator 5.12.2
Cmake 3.18.0
Opencv 4.3.0
opencv_contrib 4.3.0
以win10家庭版为例进行下载和安装
官方下载地址http://download.qt.io/archive/qt/5.12/5.12.2/下面有百度网盘安装包,请往下看
选择window版本
安装包有点大3.7G,下载很慢,最终报错,跳转到迅雷下载成功
这里给出笔者百度网盘安装包window版本Qt 5.12.2
官网下载地址https://cmake.org/download/,目前最新的版本是Cmake3.18.0
笔者选择了如下配置:配置全局变量和创建桌面快捷方式
更改安装路径
安装完成
官方下载地址:https://opencv.org/releases/
目前最新版本是4.3.0
这里选择windows
下载完压缩包名:opencv-4.3.0-vc14_vc15.exe
解压完文件名:opencv
系统变量下找到PATH,点击编辑,添加如下
D:\Qt5.12.2\Tools\mingw730_64\bin
这是下载Qt时候的路径,需要自己找到
根据知乎:PATH和path是一样的,原因是:Windows系统对大小写不敏感。
根据安装路径,找到Cmake,点击cmake.gui.exe
where is the source code
添加opencv的sources
D:/opencv/sources
where to build the binaries
D:/opencv/opencv-build
保持默认MinGW Makefiles,点击Specify native compilers,再next
编译器选择:这里自己的安装路径
C语言
D:/Qt5.12.2/Tools/mingw730_64/bin/gcc.exe
C++
D:/Qt5.12.2/Tools/mingw730_64/bin/g++.exe
需要下拉,选中下面这两个WITH_OPENGL和WITH_QT
CMake Error at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
to a directory containing one of the above files. If "Qt5" provides a
separate development package or SDK, be sure it has been installed.
Call Stack (most recent call first):
CMakeLists.txt:685 (include)
Configuring incomplete, errors occurred!
解决办法
在Opencv文件夹sources底下,也就是上面配置source code 的地址:D:/opencv/sources
在CMakeLists.txt文件下添加,位置如图所示
set (CMAKE_PREFIX_PATH "D:\\Qt5.12.2\\5.12.2\\mingw73_64\\")
这里是cmake配置opencv时候的编译器选项位置
完成之后来到下面的界面
然后再次点击configure,然后显示configuring done
此时,可以选择关闭Cmake,打开
where to build the binaries
D:/opencv/opencv-build
鼠标放在该文件夹内,按住Shift键右击鼠标,选择在此处打开Powershell窗口
在Powershell中输入
mingw32-make -j 8
据参考博客的博主所言:这里的-j 8是为了让CPU进行多任务处理,可以加快编译速度
进行到40%发生错误(这是没有添加opencv-contrib扩展库时候的报错)
gcc: error: long: No such file or directory
D:\Qt5.12.2\Tools\mingw730_64\bin\windres.exe: preprocessing failed.
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1495: modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
mingw32-make[2]: *** Waiting for unfinished jobs....
mingw32-make[1]: *** [CMakeFiles\Makefile2:1889: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:182: all] Error 2
PS D:\opencv\opencv-build>
解决办法
在stackoverflow上面看到同样的问题https://stackoverflow.com/questions/61707195/error-building-opencv-with-mingw-qt-windows
打开我们新建的opencv-build文件下该路径
D:\opencv\opencv-build\modules\core\CMakeFiles\opencv_core.dir
打开flags.make,然后把RC-DEFINES(下面划线部分)替换掉
替换代码(注:cmake每configure和generate之后都需要更改这个文件)
RC_DEFINES = -DCVAPI_EXPORTS -D_USE_MATH_DEFINES -D_WIN32_WINNT=0x0601 -D__OPENCV_BUILD=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
100%之后输入
mingw32-make install
完成之后退出
现在需要将以下路径添加环境变量
D:\opencv\opencv-build\install\x64\mingw\bin
新建project
选择默认的项目Application-Qt Widgets Application
项目命名和位置:注意这里不能有中文路径,下面是错误的示范,后面笔者又新建项目,重新改成英文的路径,否则会报错
默认下一步
继续下一步
在test.pro文件该位置添加如下代码
注意不要写成这种形式:
//这是错误的代码
INCLUDEPATH += D:\opencv\opencv-build\install\include
LIBS += D:\opencv\opencv-build\lib\
正确的代码
INCLUDEPATH += D:\opencv\opencv-build\install\include
LIBS += D:\opencv\opencv-build\lib\libopencv_*.a
在mainwindow.cpp文件下添加相应的包,添加代码如下
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/xfeatures2d.hpp>
using namespace cv;
Mat image=imread("D:\\m1521\\Pictures\\Saved Pictures\\cute-2528119.jpg",1);
namedWindow( "Display window", WINDOW_AUTOSIZE );
imshow("Display window", image );
01:55:07: 为项目test执行步骤 ...
01:55:07: 配置没有改变, 跳过 qmake 步骤。
01:55:07: 正在启动 "D:\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe" -j8
D:/Qt5.12.2/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'D:/m1521/Documents/Qt_opencv/test/build-test-Desktop_Qt_5_12_2_MinGW_64_bit-Debug'
mingw32-make[1]: Nothing to be done for 'first'.
mingw32-make[1]: Leaving directory 'D:/m1521/Documents/Qt_opencv/test/build-test-Desktop_Qt_5_12_2_MinGW_64_bit-Debug'
01:55:08: 进程"D:\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe"正常退出。
01:55:08: Elapsed time: 00:01.
至此,Qt5配置OpenCV安装配置完成!
该部分在第一部分的基础上,配置opencv_contrib,本文结构这样安排的目的在于:第一,笔者先是安装了qt和opencv,当时没想到要使用opencv_contrib扩展库,也就是说这是笔者真实的安装过程;第二,基于对读者的换位思考。经过上面第一部分的配置工作,读者可能初步产生配置成功的喜悦,以更好的状态进入到扩展库的部分。
1.下载opencv扩展库,下载地址opencv_contrib全部版本下载地址
选择版本4.3.0
解压完:
2.编译的事前准备
打开路径:在D:\opencv\sources\cmake下面找到OpenCVCompilerOptions.cmake文件,打开它并找到add_extra_compiler_option(-Werror=non-virtual-dtor),
将其注释掉,如:
#add_extra_compiler_option(-Werror=non-virtual-dtor)
(这一步,笔者不知道是干嘛所用)
这一步参考该篇博文:qt5(mingw版) opencv3.0(含contrib扩展)配置详解
由于是后面补充的contrib扩展库,打开Cmake,在第一部分配置成功后选中下面这个NONFREE
再搜索path,找到OPENCV-EXTRA-MODULES_PATH,配置value,找到解压后的扩展库,路径到modules文件,这里配置value
下面便可以点击generate,显示generating done
每次generate之后都要经过第4步修改flags.make 文件,请参考第4步
(如果电脑没有安装cuda,请去掉勾选BUILD_CUDA_STUBS,否则会找不到cuda从而报错)
然后generate(下面这是错误的图,因为未去掉BUILD_CUDA_STUBS)
然后开始重复上面没有配置opencv_contrib的步骤4:编译OpenCV
在Powershell中输入
mingw32-make -j 8
[ 26%] Building CXX object modules/ml/CMakeFiles/opencv_ml.dir/src/em.cpp.obj
[ 26%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/approx.cpp.obj
[ 26%] Building CXX object modules/ml/CMakeFiles/opencv_ml.dir/src/gbt.cpp.obj
[ 26%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/bilateral_filter.dispatch.cpp.obj
[ 26%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/lut.cpp.obj
In file included from D:\opencv_contrib-4.3.0\opencv_contrib-4.3.0\modules\cudaarithm\src\lut.cpp:7:0:
D:\opencv_contrib-4.3.0\opencv_contrib-4.3.0\modules\cudaarithm\src\lut.hpp:10:10: fatal error: cuda_runtime.h: No such file or directory
#include <cuda_runtime.h>
^~~~~~~~~~~~~~~~
compilation terminated.
mingw32-make[2]: *** [modules\cudaarithm\CMakeFiles\opencv_cudaarithm.dir\build.make:125: modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/lut.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4335: modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all] Error 2
mingw32-make[1]: *** Waiting for unfinished jobs...
error: 'GL_RENDERBUFFER_EXT' was not declared in this scope
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth);
解决办法:如果出现dynafu.cpp的错误,BUILD_opencv_rgbd与WITH_OPENGL只能勾选其一
去掉勾选BUILD_opencv_rgbd
还发现这样的错误
[ 56%] [ 56%] Scanning dependencies of target opencv_aruco
Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.objBuilding CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/brief.cpp.obj
[ 56%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/daisy.cpp.obj
[ 56%] [ 56%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/ellipticKeyPoint.cpp.objBuilding CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/fast.cpp.obj
[ 58%] Built target opencv_bioinspired
[ 58%] Built target opencv_ximgproc
Scanning dependencies of target opencv_ccalib
Scanning dependencies of target opencv_dnn_objdetect
D:\opencv_contrib-3.4\opencv_contrib-3.4\modules\xfeatures2d\src\boostdesc.cpp:654:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
^~~~~~~~~~~~~~~~~
compilation terminated.
mingw32-make[2]: *** [modules\xfeatures2d\CMakeFiles\opencv_xfeatures2d.dir\build.make:102: modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.obj] Error 1
mingw32-make[2]: *** Waiting for unfinished jobs....
按照这个教程下载zip包,解压之后
安装OpenCV时提示缺少boostdesc_bgm.i文件的问题解决方案(附带百度云资源)
在这个路径内复制
D:\opencv_contrib-4.3.0\opencv_contrib-4.3.0\modules\xfeatures2d\src
fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory
#include "features2d/test/test_detectors_regression.impl.hpp"
此处bug参考:树莓派4的opencv4.1.0–python3.7的开发环境安装
我们回到opencv4.3.0文件夹下复制下面两个文件到报错的路径下
复制到如下路径(总共需要复制5个hpp文件)
D:\opencv_contrib-4.3.0\opencv_contrib-4.3.0\modules\xfeatures2d\test
修改test_features2d.cpp文件(具体修改哪个文件,需要查看报错的位置,哪个文件引用)
修改之前
#include "features2d/test/test_detectors_regression.impl.hpp"
#include "features2d/test/test_descriptors_regression.impl.hpp"
修改之后
#include "test_detectors_regression.impl.hpp"
#include "test_descriptors_regression.impl.hpp"
发现成功一处,还有一处错误
打开test_rotation_and_scale_invariance.cpp文件
修改前
修改后
#include "test_detectors_invariance.impl.hpp" // main OpenCV repo
#include "test_descriptors_invariance.impl.hpp" // main OpenCV repo
100%之后输入
mingw32-make install
完成之后退出
请参考第5步:测试Qt程序
这里只是在配置了opencv_contrib之后,引入头文件#include
在mainwindow.cpp文件下添加相应的包,添加代码如下
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/xfeatures2d.hpp>
using namespace cv;
Mat image=imread("D:\\m1521\\Pictures\\Saved Pictures\\cute-2528119.jpg",1);
namedWindow( "Display window", WINDOW_AUTOSIZE );
imshow("Display window", image );
到这里:完整的Qt Creator5.12+OpenCV4.3.0+opencv_contrib4.3.0配置就完成了!
链接:https://pan.baidu.com/s/1_gRt_bHAjKq6tRlKgF8FLQ
提取码:4qmo
附上编译后的文件夹opencv-build
参考博客:
两个博客都有用
[1]【Qt5.12】Qt5.12安装教程
[2]win10系统Qt5.12配置OpenCV4.0.1库教程暨Qt,OpenCV,Cmake详细下载安装教程(史上最全!)