windows下安装opencv(c++版本),并配置到Qt中

本文详细介绍了如何在Windows下安装OpenCV 4.5.2,并通过CMake配置到Qt 5.12.9中。首先,下载并安装CMake,确保将其路径添加到系统变量。接着,下载OpenCV并解压,使用CMake生成编译文件,配置Qt的编译器路径。在CMake中连续配置直至无误,然后使用mingw32-make进行编译。最后,将编译好的OpenCV bin目录添加到系统路径,并在Qt Creator中创建项目,导入OpenCV库,成功运行显示图片。
摘要由CSDN通过智能技术生成

1.软件环境
OpenCV:https://opencv.org/releases/
Qt:https://download.qt.io/archive/qt/
CMake:https://cmake.org/download/
Qt Creator: https://download.qt.io/archive/qtcreator/

注:大家选择合适的版本安装即可,Qt和Qt creator的安装配置,可以参考我的博客:Win10安装Qt5和Qt Creator
这篇主要讲opencv的安装

2.下载安装CMake
a.选择合适的版本,我选用的是 3.20.1
在这里插入图片描述
b.可视化方式进行安装
在这里插入图片描述

注:记得安装时,勾选把cmake安装路径加入到系统变量路径中
笔者把cmake安装在了E盘,所以cmake的可执行文件路径为:E:\Cmake\bin

3.安装opencv版本

a.下载合适的opencv版本,我选用的是opencv 4.5.2
在这里插入图片描述

注:图中圈出的开发文档很关键,遇到不太熟悉的库函数,自行查阅

b.将文件进行解压

笔者放在了E:\opencv4.5.2
在这里插入图片描述
解压后的文件目录为:
在这里插入图片描述
c.找到qt安装目录下tools下的mingw,将其加入系统变量中。
笔者的是:E:\Qt5.12.9\Tools\mingw730_64\bin
,添加到系统变量path中
在这里插入图片描述

d.编译opencv

打开cmake的可执行文件,可以通过前面安装cmake时生成的桌面快捷方式,也可以进入到进入E:\Cmake\bin,点击cmake-gui.exe

之后进入如图所示的操作
在这里插入图片描述
在这里插入图片描述
点击next,
c选择,路径:E:\Qt5.12.9\Tools\mingw730_64\bin\gcc.exe
c++选择E:\Qt5.12.9\Tools\mingw730_64\bin\g++.exe
在这里插入图片描述
正在生成
在这里插入图片描述
编译后,出现了一堆红色的东西,根据提示“Press Configure to update and display new value in red, then press Genarate to genarate selected build files.” 再次点击“Configure”按钮,直到红色消失。

在这里插入图片描述
再次点击“Configure”按钮,直到红色消失。

在这里插入图片描述
点击Generate
Generate完成之后,关闭Cmake即可,此时进入之前我们新建的文件夹E:\opencv4.5.2\opencv\bianyi
如下图:
在这里插入图片描述
然后执行mingw
(前面我们已经把它加入到系统变量中了,在E:\Qt5.12.9\Tools\mingw730_64\bin下面有mingw32-make.exe)

正在编译
在这里插入图片描述
因编译速度较慢,可以使用多核,此时mingw32-make命令可改为mingw32-make -j 8
8是为了让CPU进行多任务处理,可以通过“此电脑/计算机—>管理—>设别管理器—>处理器”来查看几核。

这里我把整个编译过程作为附录1,放在文末,以便查阅,以资缺漏。

编译完成,安装opencv,将安装过程附在附录2中
安装命令: mingw32-make.exe install
安装完成后,把E:\opencv4.5.2\opencv\bianyi\bin添加到系统变量中

4.在qt中使用opencv

打开qt creator,创建一个项目
在这里插入图片描述
在.pro项目文件中,导入opencv库

INCLUDEPATH += E:\opencv4.5.2\opencv\bianyi\install\include
LIBS += E:\opencv4.5.2\opencv\bianyi\lib\libopencv_*.a

如图所示
在这里插入图片描述
在mainwindow.cpp中红框位置加上如下代码

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    using namespace cv;
    
    

    Mat image=imread("E:\\test.jpg",1);
    namedWindow( "Display window", WINDOW_AUTOSIZE );
    imshow( "Display window",image );

在这里插入图片描述
ctrl+s 保存,红色提示小时。点击运行

在这里插入图片描述
运行,显示出窗体和图片
在这里插入图片描述

附录1

在这里插入代码片

附录2

E:\opencv4.5.2\opencv\bianyi>
E:\opencv4.5.2\opencv\bianyi>mingw32-make.exe install
Consolidate compiler generated dependencies of target libopenjp2
[  1%] Built target libopenjp2
[  1%] Built target opencv_videoio_plugins
Consolidate compiler generated dependencies of target zlib
[  2%] Built target zlib
Consolidate compiler generated dependencies of target opencv_core
[  9%] Built target opencv_core
Consolidate compiler generated dependencies of target opencv_imgproc
[ 15%] Built target opencv_imgproc
Consolidate compiler generated dependencies of target libjpeg-turbo
[ 18%] Built target libjpeg-turbo
Consolidate compiler generated dependencies of target libwebp
[ 25%] Built target libwebp
Consolidate compiler generated dependencies of target libtiff
[ 28%] Built target libtiff
Consolidate compiler generated dependencies of target libpng
[ 29%] Built target libpng
Consolidate compiler generated dependencies of target IlmImf
[ 35%] Built target IlmImf
Consolidate compiler generated dependencies of target opencv_imgcodecs
[ 36%] Built target opencv_imgcodecs
Consolidate compiler generated dependencies of target opencv_videoio
[ 37%] Built target opencv_videoio
Consolidate compiler generated dependencies of target opencv_highgui
[ 38%] Built target opencv_highgui
Consolidate compiler generated dependencies of target opencv_ts
[ 38%] Built target opencv_ts
Consolidate compiler generated dependencies of target opencv_test_core
[ 41%] Built target opencv_test_core
Consolidate compiler generated dependencies of target opencv_perf_core
[ 43%] Built target opencv_perf_core
Consolidate compiler generated dependencies of target opencv_flann
[ 43%] Built target opencv_flann
Consolidate compiler generated dependencies of target opencv_test_flann
[ 43%] Built target opencv_test_flann
Consolidate compiler generated dependencies of target opencv_test_imgproc
[ 47%] Built target opencv_test_imgproc
Consolidate compiler generated dependencies of target opencv_perf_imgproc
[ 49%] Built target opencv_perf_imgproc
Consolidate compiler generated dependencies of target opencv_ml
[ 50%] Built target opencv_ml
Consolidate compiler generated dependencies of target opencv_test_ml
[ 51%] Built target opencv_test_ml
Consolidate compiler generated dependencies of target opencv_photo
[ 52%] Built target opencv_photo
Consolidate compiler generated dependencies of target opencv_test_photo
[ 53%] Built target opencv_test_photo
Consolidate compiler generated dependencies of target opencv_perf_photo
[ 53%] Built target opencv_perf_photo
Consolidate compiler generated dependencies of target libprotobuf
[ 58%] Built target libprotobuf
Consolidate compiler generated dependencies of target opencv_dnn
[ 65%] Built target opencv_dnn
Consolidate compiler generated dependencies of target opencv_test_dnn
[ 66%] Built target opencv_test_dnn
Consolidate compiler generated dependencies of target opencv_perf_dnn
[ 66%] Built target opencv_perf_dnn
Consolidate compiler generated dependencies of target opencv_features2d
[ 68%] Built target opencv_features2d
Consolidate compiler generated dependencies of target opencv_test_features2d
[ 69%] Built target opencv_test_features2d
Consolidate compiler generated dependencies of target opencv_perf_features2d
[ 70%] Built target opencv_perf_features2d
Consolidate compiler generated dependencies of target opencv_test_imgcodecs
[ 70%] Built target opencv_test_imgcodecs
Consolidate compiler generated dependencies of target opencv_perf_imgcodecs
[ 70%] Built target opencv_perf_imgcodecs
Consolidate compiler generated dependencies of target opencv_test_videoio
[ 71%] Built target opencv_test_videoio
Consolidate compiler generated dependencies of target opencv_perf_videoio
[ 72%] Built target opencv_perf_videoio
Consolidate compiler generated dependencies of target opencv_calib3d
[ 75%] Built target opencv_calib3d
Consolidate compiler generated dependencies of target opencv_test_calib3d
[ 77%] Built target opencv_test_calib3d
Consolidate compiler generated dependencies of target opencv_perf_calib3d
[ 78%] Built target opencv_perf_calib3d
Consolidate compiler generated dependencies of target opencv_test_highgui
[ 79%] Built target opencv_test_highgui
Consolidate compiler generated dependencies of target quirc
[ 80%] Built target quirc
Consolidate compiler generated dependencies of target opencv_objdetect
[ 80%] Built target opencv_objdetect
Consolidate compiler generated dependencies of target opencv_test_objdetect
[ 80%] Built target opencv_test_objdetect
Consolidate compiler generated dependencies of target opencv_perf_objdetect
[ 81%] Built target opencv_perf_objdetect
Consolidate compiler generated dependencies of target opencv_stitching
[ 82%] Built target opencv_stitching
Consolidate compiler generated dependencies of target opencv_test_stitching
[ 82%] Built target opencv_test_stitching
Consolidate compiler generated dependencies of target opencv_perf_stitching
[ 82%] Built target opencv_perf_stitching
Consolidate compiler generated dependencies of target opencv_video
[ 83%] Built target opencv_video
Consolidate compiler generated dependencies of target opencv_test_video
[ 84%] Built target opencv_test_video
Consolidate compiler generated dependencies of target opencv_perf_video
[ 85%] Built target opencv_perf_video
Consolidate compiler generated dependencies of target ade
[ 86%] Built target ade
Consolidate compiler generated dependencies of target opencv_gapi
[ 91%] Built target opencv_gapi
Consolidate compiler generated dependencies of target opencv_test_gapi
[ 96%] Built target opencv_test_gapi
Consolidate compiler generated dependencies of target opencv_perf_gapi
[ 97%] Built target opencv_perf_gapi
Consolidate compiler generated dependencies of target opencv_annotation
[ 97%] Built target opencv_annotation
Consolidate compiler generated dependencies of target opencv_visualisation
[ 97%] Built target opencv_visualisation
Consolidate compiler generated dependencies of target opencv_interactive-calibration
[ 97%] Built target opencv_interactive-calibration
Consolidate compiler generated dependencies of target opencv_version
[ 97%] Built target opencv_version
Consolidate compiler generated dependencies of target opencv_version_win32
[ 98%] Built target opencv_version_win32
Consolidate compiler generated dependencies of target opencv_model_diagnostics
[100%] Built target opencv_model_diagnostics
Install the project...
-- Install configuration: "Release"
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/opencl-headers-LICENSE.txt
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/cvconfig.h
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/opencv_modules.hpp
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/lib/OpenCVModules.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/lib/OpenCVModules-release.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/lib/OpenCVConfig-version.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/lib/OpenCVConfig.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/./OpenCVConfig-version.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/./OpenCVConfig.cmake
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/./LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/./setup_vars_opencv4.cmd
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/zlib-README
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libjpeg-turbo-README.md
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libjpeg-turbo-LICENSE.md
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libjpeg-turbo-README.ijg
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libtiff-COPYRIGHT
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libopenjp2-README.md
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libopenjp2-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libpng-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/libpng-README
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/openexr-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/openexr-AUTHORS.ilmbase
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/openexr-AUTHORS.openexr
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/protobuf-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/protobuf-README.md
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/quirc-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/opencv.hpp
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/ade-LICENSE
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/ffmpeg-license.txt
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/etc/licenses/ffmpeg-readme.txt
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/lib/libopencv_core452.dll.a
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/x64/mingw/bin/libopencv_core452.dll
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/core.hpp
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/core/affine.hpp
-- Installing: E:/opencv4.5.2/opencv/bianyi/install/include/opencv2/core/async.hpp
-- Install
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

同志啊为人民服务!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值