QT5.12.3+OPENCV4.2.0配置,MINGW编译与库文件调用

目录

1.QT5.12.3+OPENCV4.2.0

2.准备工作

3.安装步骤

3.1.QT

3.2.CMAKE  

3.3.OPENCV

4.使用MINGW编译OPENCV

5.QT调用库文件

6.QT测试OPENCV


1.QT5.12.3+OPENCV4.2.0

2.准备工作

3.安装步骤

3.1.QT

  1.  Welcome to the Qt installer: next

  2.  Qt Account - your unified login to everything Qt: skip

  3.  Setup-Qt: next

  4.  installation folder: $$ProgramFiles/Qt5.12.3

  5.  select components: Qt-Qt5.12.3-MingGW 64 bit

  6.  select components: Qt-Tools-MinGW 64 bit

  7.  License Agreement: agree and next

  8.  start menu shortcuts: next

  9.  ready to install: install

3.2.CMAKE  

  1.  Welcome to the CMake Setup Wizzard: next

  2.  End-User License Agreement: [V] Accept and next

  3.  Install options: [V] Add CMake to the system PATH for all users, next

  4.  Destination folder: $$ProgramFiles/CMake, next

  5.  Ready to install CMake, Install

3.3.OPENCV

opencv需要选择release版本下载,直接clone可能在make的时候出错,这里下载最新的4.2.0版本,然后解压到没有中文路径的目录下,本教程假设解压到目录 $$opencv-4.2.0\ 下

4.使用MINGW编译OPENCV

mingw 来自 qt,添加其到系统环境变量

$$ProgramFiles\Qt\5.12.3\mingw73_64\bin

在 $$opencv-4.2.0 附近或子目录建立编译 opencv 的 output 文件夹:

mkdir cv_build

打开CMake,选中 opencv 的源码和 output 目录,然后 configure

在弹出的对话框中如下配置,调用 qt 的 mingw

configure 期间会下载 dll 文件,由于网络原因会出现下载失败,但不影响

第一次 config 完成后:

  • 勾上 WITH_QT
  • 勾上 WITH_OPENGL
  • 取消 ENABLE_PRECOMPILED_HEADERS
  • 取消 WITH_IPP

然后再次 config,这次 config 可能会报错

选择正确的 qt-cmake 文件路径 QT5_DIR --> $$ProgramFiles/Qt5/5.12.3/mingw73_64/lib/cmake/Qt5

其他几个文件路径也在这个目录下,一直 config 和配置参数,直到红色消失,参数配置如下

configure 完成后,执行 generate,然后在 cv_build 目录下运行Windows PowerShell终端,在终端下执行

mingw32-make -j 8

使用8个cpu核心编译 opencv 源码

其他版本可能出现的问题:

If, in the file opencv/sources/modules/videoio/src/cap_dshow.cpp, you have the following error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...

try this: put the following line: #define NO_DSHOW_STRSAFE, before the line : #include "DShow.h"

If you have the error: ‘nullptr’ was not declared in this scope..

try this: in cmake check the box ENABLE_CXX11

If, in the file modules\videoio\src\cap_msmf.cpp you have the error: using invalid field '{anonymous}::ComPtr<T>::p'..

try this: in cmake unchecking WITH_MSMF

If,Building RC object modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj v:\MinGW-Builds\mingw64\bin\windres.exe: unknown option -- W

try this: change the source code to release version

编译完成:

make install:

3e990947d80a6b4447d5411f6b49bf3d.png

install done:

install 完成后,在 cv_build 目录下自动生成了一个 install 文件夹,该文件夹内包含了 h文件、lib、dll文件

建立文件夹,用于其他工程调用:

 
  1. # 新建一个文件夹,用于其他工程调用:

  2. mkdir opencv420

  3. cd opencv420

建立文件目录如下

 
  1. opencv420/:

  2. +---bin

  3. +---include

  4. |   \---opencv2

  5. |       +---calib3d

  6. |       +---core

  7. |       +---dnn

  8. |       +---features2d

  9. |       +---flann

  10. |       +---gapi

  11. |       +---highgui

  12. |       +---imgcodecs

  13. |       +---imgproc

  14. |       +---ml

  15. |       +---objdetect

  16. |       +---photo

  17. |       +---stitching

  18. |       +---video

  19. |       \---videoio

  20. \---lib

其中:

  • include 目录拷贝自 cv_build/install/include
  • bin 目录拷贝自 cv_build/install/x64/mingw/bin
  • lib 目录拷贝自 cv_build/install/x64/mingw/lib

到现在为止,opencv420 目录包含了 mingw 编译的库文件,接口的头文件,可用于其他 mingw-based 项目调用

5.QT调用库文件

拷贝 opencv420 到项目内 pro 文件的同目录,在 opencv420 目录内建立文件 opencv420.pri,内容如下:

 
  1. DEFINES += OPENCV4_DLL

  2. INCLUDEPATH += $$PWD/include

  3. LIBS += -L$$PWD/bin -llibopencv_calib3d420

  4. LIBS += -L$$PWD/bin -llibopencv_core420

  5. LIBS += -L$$PWD/bin -llibopencv_dnn420

  6. LIBS += -L$$PWD/bin -llibopencv_features2d420

  7. LIBS += -L$$PWD/bin -llibopencv_flann420

  8. LIBS += -L$$PWD/bin -llibopencv_gapi420

  9. LIBS += -L$$PWD/bin -llibopencv_highgui420

  10. LIBS += -L$$PWD/bin -llibopencv_imgcodecs420

  11. LIBS += -L$$PWD/bin -llibopencv_imgproc420

  12. LIBS += -L$$PWD/bin -llibopencv_ml420

  13. LIBS += -L$$PWD/bin -llibopencv_objdetect420

  14. LIBS += -L$$PWD/bin -llibopencv_photo420

  15. LIBS += -L$$PWD/bin -llibopencv_stitching420

  16. LIBS += -L$$PWD/bin -llibopencv_video420

  17. LIBS += -L$$PWD/bin -llibopencv_videoio420

  • 定义宏,使用 opencv4 动态库
  • 添加头文件路径为 pri 文件路径下 include 路径
  • 添加库文件,-L 添加库文件的路径为 pri 文件路径下的 bin 路径,-l 添加该路径下的动态库文件

6.QT测试OPENCV

修改 pro 文件,增加该行

include($$PWD/opencv420/opencv420.pri)

修改 main.cpp

 
  1. #include "mainwindow.h"

  2. //#include <QApplication>

  3. #include <opencv2/core/core.hpp>

  4. #include <opencv2/highgui/highgui.hpp>

  5. int main(int argc, char *argv[])

  6. {

  7. //    QApplication a(argc, argv);

  8. //    MainWindow w;

  9. //    w.show();

  10.     cv::Mat img_src = cv::imread("W:\\Images\\TestImages\\1.jpg", 1);

  11.     cv::namedWindow("Image Shower");

  12.     cv::imshow("Image Shower", img_src);

  13.     cv::waitKey(0);

  14. //    return a.exec();

  15.     return 0;

  16. }

构建运行:

除了拷贝库文件然后添加LIBS路径的方法外,还可以将dll文件的路径添加到系统环境变量中。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

smartvxworks

创造不易,感谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值