Qt+Opencv:Qt中部署opencv

一、获取opencv

本文先只介绍opencv-release版本的获取方式,如果有版本指定编译工具链需求的朋友,可以通过下载opencv的源码,使用cmake进行编译。
获取地址:https://opencv.org/releases/

在这里插入图片描述
注意:opencv-3.x.x的release版本是支持vc14-15的,opencv-4.x.x需要vc16以上版本支持

本系列仅作为研究学习,我们选择下载:opencv-3.4.16 windows版本,支持 vc14_vc15。
下载后文件解压目录如下:
在这里插入图片描述
我们直接打开bulid 目录:
在这里插入图片描述
includex64bin 目录是我们一会在Qt项目中需要的。很显然哈,官方的release版本果然只有64位的dll,如果采用32位编译工具链的朋友,自己配置编译下吧,也很简单。后面有需要,我们可以再出一篇专门讲解opencv保姆级源码编译的教程。话说博主还是很喜欢编译各种源码,貌似过往的博文里包含的编译江湖已经不下几十篇了。毕竟授人以鱼不如授人以渔。

二、在Qt中集成opencv

2.1 新建一个Qt项目

在pro中添加opencv头文件路径和库文件路径

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp

HEADERS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

# clean code for Qt
CONFIG     += no_debug_release
DESTDIR     = $$PWD/bin
OBJECTS_DIR = $$PWD/temp/obj
MOC_DIR     = $$PWD/temp/moc
RCC_DIR     = $$PWD/temp/rcc
UI_DIR      = $$PWD/temp/ui

# generate .pdb
QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO

CONFIG(release, debug|release) {
    # release
    DESTDIR = $${DESTDIR}/$${QMAKE_TARGET_ARCH}/release
    TARGET = $${TARGET}
} else {
    # debug
    DESTDIR = $${DESTDIR}/$${QMAKE_TARGET_ARCH}/debug
    TARGET = $${TARGET}d
}



INCLUDEPATH += 3rdpart/opencv/include \
               3rdpart/opencv/include/opencv \
               3rdpart/opencv/include/opencv2


# 添加v14版 opencv 库文件,区分debug和release
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/3rdpart/opencv/x64/vc14/lib/ -lopencv_world3416
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/3rdpart/opencv/x64/vc14/lib/ -lopencv_world3416d

INCLUDEPATH += $$PWD/3rdpart/opencv/x64/vc14
DEPENDPATH += $$PWD/3rdpart/opencv/x64/vc14

2.2 验证添加opencv依赖是否成功

#include <QApplication>
#include <opencv.hpp>
#include <QDebug>

bool test_cv_openimage()
{
    try{
        QString imagePath = QApplication::applicationDirPath() + "/image/1.png";
        cv::Mat mat = cv::imread("image/1.png");
        if(mat.empty()){
            return false;
        }
        cv::imshow("Image",mat);

    }catch(...)
    {
        qWarning() << "cv::imread error." << Qt::endl;
    }
    return true;
}


bool test_cv_openvideo()
{
    // 打开视频文件
    cv::VideoCapture cap("image/scan-action.mp4");

    // 检查是否成功打开视频
    if (!cap.isOpened()) {
        std::cout << "无法打开视频文件" << std::endl;
        return false;
    }
    
    // 读取并显示视频帧
    cv::Mat frame;
    while (true) {
        cap >> frame;  // 读取视频帧
        if (frame.empty()) {
            break;  // 视频结束
        }
        cv::imshow("Video", frame);  // 显示视频帧
        if (cv::waitKey(30) == 27) {  // 按下 ESC 键退出
            break;
        }
    }
    return true;
}

int main(int argc,char* argv[])
{

    QApplication a(argc,argv);

    test_cv_openimage();

    test_cv_openvideo();

    return a.exec();
}

2.3 结果演示

在这里插入图片描述

三、总结

opencv官方发布的版本还是十分的友好的,在编译工具链和版本匹配的情况下,快速集成opencv的开发环境还真是分分钟的事情。所谓“见贤思齐焉”,我们自己在平时创建插件项目时,也可以参考opencv的目录格式,谁用谁不得竖拇指哥儿!

本章到这里就结束啦,接下来,我们将逐渐深入opencv的图像处理模块,通过项目实践来逐步精进。当然,按照博主一贯的态度,我们不会只限于接口调用和简单使用上,设计模式、算法、源码解析都是超级like的。

Unfinished, to be continued~