1.CMake下载安装
官网地址:CMake - Upgrade Your Software Build System
(1)下载后双击安装

(2)进入安装界面,点击【Next】

(3)同意协议,点击【Next】

(4)将Cmake添加进用户变量,黄色框是创建桌面图标,可根据自己需要选择,点击【Next】

(5)选择路径,点击【Next】

(6)开始安装,点击【Install】

(7)安装完毕,点击【Finish】

(8)安装完毕,进行测试。
打开cmd黑窗,输入命令查看版本
cmake /V
2.OpenCV下载
OpenCV官网:Releases - OpenCV
opencv_contrib下载地址:GitCode - 开发者的代码家园
3.Qt编译OpenCV
(1)Qt配置CMake


(2)使用Qt打开opencv/sources/CMakeLists.txt
(3)开始配置Qt工程
打开这两个,然后提交:


编译:这个过程可能需要两个小时等待

如编译过程中出现错误:
Building RC object modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj
gcc: error: long: No such file or directory
解决方法是根据build.make中的错误行,自己修改编译命令,手动生成vs_version.rc.obj,并重新执行mingw32-make命令编译。在终端执行命令:
windres.exe D:\opencv\opencv\build-sources-Desktop_Qt_5_9_0_MinGW_32bit-Default\modules\core\vs_version.rc -O coff D:\opencv\opencv\build-sources-Desktop_Qt_5_9_0_MinGW_32bit-Default\modules\core\CMakeFiles\opencv_core.dir\vs_version.rc.obj
(4)安装
勾选install,再构建

(5)使用
- 库文件:D:\opencv\opencv\build-sources-Desktop_Qt_5_9_0_MinGW_32bit-Default\install\x64\mingw\lib\libopencv_*.a
- 头文件:D:\opencv\opencv\build-sources-Desktop_Qt_5_9_0_MinGW_32bit-Default\install\include
- DLL文件:D:\opencv\opencv\build-sources-Desktop_Qt_5_9_0_MinGW_32bit-Default\bin
Qt工程在使用OpenCV时需要将dll文件拷贝到程序运行目录下,然后需要在pro文件中加入:
INCLUDEPATH += $$PWD/OpencvLib/include
LIBS += $$PWD/OpencvLib/lib/libopencv_*.a