【Qt_OpenCV基础篇 - 001】Qt 5.10.1下配置OpenCV开发环境及Demo

一、开发环境

1、Windows 7 64位 SP1 旗舰版;

2、Qt 5.10.1;

3、OpenCV 3.4.1

二、环境配置

1、编译生成OpenCV库,具体过程可参考本人教程(【Qt_OpenCV基础篇 - 000】Qt5.10.1 + MingW5.3.0 + Win7_64 + CMake3.11.1环境下编译OpenCV 3.4.1),生成好的库文件夹如图1所示;

【Qt_OpenCV基础篇 - 001】Qt 5.10.1下配置OpenCV开发环境及Demo_第1张图片

图1 OpenCV库文件夹

2、打开Qt Creator,新建基于控制台的项目,此处项目命名为“OpenCV_ShowImage”;

3、将OpenCV库复制到工程目录,放在文件夹“OpenCV_Qt”下;

4、打开“OpenCV_ShowImage.pro”文件,添加如下路径包含代码:

1)项目文件夹下相对路径1:

INCLUDEPATH += $$PWD/OpenCV_Qt/include\
               $$PWD/OpenCV_Qt/include/opencv\
               $$PWD/OpenCV_Qt/include/opencv2

LIBS += -L $$PWD/OpenCV_Qt/lib/libopencv_*.a

2)项目文件夹下相对路径2:

INCLUDEPATH += $$PWD/../OpenCV_Qt/include\
               $$PWD/../OpenCV_Qt/include/opencv\
               $$PWD/../OpenCV_Qt/include/opencv2

LIBS += -L $$PWD/../OpenCV_Qt/lib/libopencv_*.a
3)绝对路径:
INCLUDEPATH += D:/OpenCV_Qt/include\
               D:/OpenCV_Qt/include/opencv\
               D:/OpenCV_Qt/include/opencv2

LIBS += -L D:/OpenCV_Qt/lib/libopencv_*.a

4)添加完整包含:

INCLUDEPATH += $$PWD/../OpenCV_Qt/include\
               $$PWD/../OpenCV_Qt/include/opencv\
               $$PWD/../OpenCV_Qt/include/opencv2

unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_calib3d341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_core341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_dnn341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_features2d341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_flann341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_highgui341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_imgcodecs341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_imgproc341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_ml341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_objdetect341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_photo341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_shape341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_stitching341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_superres341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_video341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_videoio341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -llibopencv_videostab341
unix|win32: LIBS += -L$$PWD/../OpenCV_Qt/bin/ -lopencv_ffmpeg341

以上4种路径设置均可,取决于OpenCV库所放的位置;其中第4种将所有库文件均包含进来,简单粗暴,不过不容易出错;不同版本的OpenCV库,仅需要修改后面的版本号即可。以上4种仅为参考,项目编写过程中需根据具体情况来设置,遇到问题适当调整即可解决。

经过上述步骤,即完成OpenCV库的环境配置,下面是简单的示例程序,用来演示配置是否有效。

三、示例Demo

1、完成第二部分配置后,打开“main.cpp”文件,添加如下代码:

#include 
#include "opencv2/opencv.hpp"//添加Opencv相关头文件

using namespace cv;

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

    Mat img = cv::imread("test.jpg");//加载并图片,延时2后自动关闭窗口
    imshow("Image", img);
    waitKey(2000);

    destroyAllWindows();

    return a.exec();
}

2、将自定义的图片添加到合适位置;

3、构建并运行程序,运行效果如图2所示。

【Qt_OpenCV基础篇 - 001】Qt 5.10.1下配置OpenCV开发环境及Demo_第2张图片

图2 运行效果图





你可能感兴趣的:(Qt_OpenCV基础篇,【Qt-OpenCV基础篇】)