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