使用环境
- 系统:Win10
- IDE:Qt5.14.2
- 下载Windows安装包:opencv-xx-xx.exe
- CMake:使用VS2019自带的
- MinGW:使用Qt自带的
使用自带工具
设置两个路径到环境变量path
中:
D:\Program_Files\Qt\Tools\mingw730_32\bin
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin
编译Opencv
因为安装版opencv有VC x64
的dll,想在Qt中使用可以安装qt msvc编译器。Qt 5.14.2 中编译器最高只有msvc2017,并且不带调试器,除非安装了vs2017(但我安装了vs2019)否则要额外安装配置,索性直接用qt MinGW 直接编译。
Windows Powershell
cd D:\\opencv\source
mkdir build
cd build
cmake ../ -G "MinGW Makefiles" -DOPENCV_ENABLE_ALLOCATOR_STATS=OFFC
mingw32-make -j 8
mingw32-make install
ls install
指令运行完后会直接生成库,把生成的库路径放到环境变量里或把库考到自己的工程就可以使用了
使用步骤
untitlrd.pro
//库引用配置
win32: LIBS += \
-L$$PWD/../../../../opencv/install/x64/mingw/lib/ \
-llibopencv_core430.dll \
-llibopencv_highgui430.dll \
-llibopencv_imgcodecs430.dll \
-llibopencv_imgproc430.dll \
-llibopencv_features2d430.dll \
-llibopencv_calib3d430.dll
INCLUDEPATH += $$PWD/../../../../opencv/install/include
DEPENDPATH += $$PWD/../../../../opencv/install/include
mainwindow.cpp
//声明
#include <opencv2/opencv.hpp>
using namespace cv;
{
//调用代码
Mat src = imread("D:\\USER\\long\\Desktop\\1122.jpg");
imshow("显示一张图片", src);
}