QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库

QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库

文章目录

  • 前言
  • 一、安装QT,libtorch,opencv,realsense SDK
  • 二、新建QT项目工程
  • 三、编辑.ui文件
  • 三、编辑.pro文件
  • 四、编辑mainwindow.cpp文件
  • 五、点击release执行程序
  • 六、QT Creator 调用vs封装的lib库
    • 1、编辑.pro文件
    • 2、在mainwindow.cpp里添加头文件
    • 3、点击release 运行工程,成功


前言

使用qt做界面,并调用opencv、libtorch等库,有多种方式。
我尝试了以下三种方式,并成功运行:
(1) 下面要讲的QT Creator +qmake+ Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库+MSVC2017_64bit的方式
(配置opencv、libtorch和realsense sdk头文件和库,是在.pro文件,用MSVC2017 64bit编译)
(2) 前面文章有讲QT Creator +cmake+ Libtorch1.8.1+opencv+realsense sdk+加载用vs封装的lib库_配置的方式
(配置opencv、libtorch和realsense sdk头文件和库,是在CMakeList.txt文件,用MSVC2017 64bit编译)
(3) 前面文章讲到的vs2017-QT VS Tools搭建opencv+libtorch+realsense sdk+调用vs封装的lib库的方式和安装配置方法
(配置opencv、libtorch和realsense sdk头文件和库,是在项目->属性,配置包含目录、库目录、链接器等等)

一、安装QT,libtorch,opencv,realsense SDK

具体的加载链接和安装方法,在我之前的这篇文章中已经讲过。参照我之前的这篇文章进行下载和安装链接地址
其中有讲安装QT时一定要勾选MSVC,我勾选了MSVC2017 64-bit
已经下载调试工具CDB也有讲,按照该文章下载、安装、配置系统环境变量,完成后,就可以开始新建QT项目了。

二、新建QT项目工程

选择“文件”->“新建文件或项目”->Application->Qt widgets ApplicationQT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第1张图片
输入工程名称和路径:
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第2张图片
选择使用qmake编译QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第3张图片
根据自己的需要是否更改头文件或源文件的名称,如果不更改就直接选择下一步就可以了QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第4张图片
默认下一步
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第5张图片
此处选择MSVC2017 64bit,这个是根据你在安装QT时,选择的那个版本。
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第6张图片
在MSVC或MinGW选项后面,当鼠标放置在此处,会出现"Manage…",勾选"Desktop Qt 5.14.2 MSVC2017 64bit",并点击"Manage…",对选择的"Desktop Qt 5.14.2 MSVC2017 64bit"的各个选项进行配置。


点击"Manage…"后,出现如下配置列表,对红色框中的内容做如下配置。
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第7张图片
点击完成,新建的基于qmake的QT项目完成。
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第8张图片

三、编辑.ui文件

分别拖动Push Button和Label控件到编辑窗口
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第9张图片
在Push Button控件上,右击,选择“转到槽”

选择clicked()函数:
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第10张图片
则在mainwindow.h文件中自动出现on_pushButton_clicked()函数的声明,在
mainwindow.cpp文件中自动出现on_pushButton_clicked()的定义。如下:
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第11张图片
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第12张图片

三、编辑.pro文件

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

INCLUDEPATH += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include\
               D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include/opencv\
               D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include/opencv2\
               D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/include\
               D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/include/torch/csrc/api/include\
               "C:/Program Files (x86)/Intel RealSense SDK 2.0/include/"
#Debug: {
#LIBS += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib/opencv_world341d.lib\
#        D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib/*.lib\
#        "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"
#}
#Release: {
#LIBS += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib/opencv_world341.lib\
#        D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib/*.lib\
#        "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"
#}

LIBS += -LD:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib -lopencv_world341 \
-LD:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib -lc10 -ltorch -lc10d -ltorch_cpu \
 -lgloo -lcaffe2_module_test_dynamic -lasmjit -lclog -lcpuinfo -ldnnl -lfbgemm

LIBS += "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

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

在.pro文件里面,添加了以下几处:
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第13张图片
注:LIBS路径如果按照注释掉的方式,写会有问题,在这里写LIBS的方式,我尝试了几种不同的写法,最后上面的写法,终于可以了。如果realsense 的lib的路径按照-LC:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64 -lrealsense2 \的方式写,总提示extra expression的错误,或者提示“无法打开输入文件 Files.obj”,后来参考该文章,原来是.pro里面多余的\,或者.pro里面LIBS路径格式写的有问题。

四、编辑mainwindow.cpp文件

1、在mainwindow.cpp添加头文件

#undef slots
#include 
#include 
#define slots Q_SLOTS

#include
#include
#include

#include
#include

using namespace std;
using namespace cv;

2、在mainwindow.cpp的void MainWindow::on_pushButton_clicked()函数中添加代码,读取图像

void MainWindow::on_pushButton_clicked()
{
    rs2::pipeline pipe;
    rs2::config pipe_config;
    pipe_config.enable_stream(RS2_STREAM_DEPTH,640,480,RS2_FORMAT_Z16,30);
    pipe_config.enable_stream(RS2_STREAM_COLOR,640,480,RS2_FORMAT_BGR8,30);

    //start()函数返回数据管道的profile
    rs2::pipeline_profile profile = pipe.start(pipe_config);

    //声明数据流
    auto depth_stream=profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
    auto color_stream=profile.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();


    while(true)
    {
        //堵塞程序直到新的一帧捕获
        rs2::frameset frameset = pipe.wait_for_frames();
        //取深度图和彩色图
        rs2::frame color_frame = frameset.get_color_frame();//processed.first(align_to);

        //获取宽高
        const int color_w=color_frame.as<rs2::video_frame>().get_width();
        const int color_h=color_frame.as<rs2::video_frame>().get_height();

        //创建OPENCV类型 并传入数据
        Mat color_image(Size(color_w,color_h),
                               CV_8UC3,(void*)color_frame.get_data(),Mat::AUTO_STEP);

        Mat temp;
        QImage Qtemp;
        cvtColor(color_image, temp, CV_BGR2RGB);//BGR convert to RGB
        Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);

        ui->label->setPixmap(QPixmap::fromImage(Qtemp));
        ui->label->resize(Qtemp.size());
        ui->label->show();

        imshow("color_image",color_image);
        waitKey(10);
    }
}

3、mainwindow.cpp的全部代码为:

#undef slots
#include 
#include 
#define slots Q_SLOTS

#include
#include
#include

#include
#include

using namespace std;
using namespace cv;

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    rs2::pipeline pipe;
    rs2::config pipe_config;
    pipe_config.enable_stream(RS2_STREAM_DEPTH,640,480,RS2_FORMAT_Z16,30);
    pipe_config.enable_stream(RS2_STREAM_COLOR,640,480,RS2_FORMAT_BGR8,30);

    //start()函数返回数据管道的profile
    rs2::pipeline_profile profile = pipe.start(pipe_config);

    //声明数据流
    auto depth_stream=profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
    auto color_stream=profile.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();


    while(true)
    {
        //堵塞程序直到新的一帧捕获
        rs2::frameset frameset = pipe.wait_for_frames();
        //取深度图和彩色图
        rs2::frame color_frame = frameset.get_color_frame();//processed.first(align_to);

        //获取宽高
        const int color_w=color_frame.as<rs2::video_frame>().get_width();
        const int color_h=color_frame.as<rs2::video_frame>().get_height();

        //创建OPENCV类型 并传入数据
        Mat color_image(Size(color_w,color_h),
                               CV_8UC3,(void*)color_frame.get_data(),Mat::AUTO_STEP);

        Mat temp;
        QImage Qtemp;
        cvtColor(color_image, temp, CV_BGR2RGB);//BGR convert to RGB
        Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);

        ui->label->setPixmap(QPixmap::fromImage(Qtemp));
        ui->label->resize(Qtemp.size());
        ui->label->show();

        imshow("color_image",color_image);
        waitKey(10);
    }
}

五、点击release执行程序

提示下面三个错误
(1) “c10::IValue::IValue”:无法将函数定义与现有的声明匹配
(2) 语法错误:标识符"IValue"
(3) 意外的标记位于";"之前
在这里插入图片描述
在这里插入图片描述
参考该文章,里面提到
在报错的D:\libtorch\include\ATen/core/ivalue.h头文件和IValue_init.h文件中
找出如下3行内容,注释掉。共有多处

/// \cond DOXYGEN_CANNOT_HANDLE_CONSTRUCTORS_WITH_MACROS_SO_EXCLUDE_THIS_LINE_FROM_DOXYGEN
C10_DEPRECATED_MESSAGE("IValues based on std::vector are potentially slow and deprecated. Please use c10::List instead.")
/// \endcond

我只是点击出错的问题,自动进入ivalue.h文件,只把ivalue.h中语句前面有标红号的位置的上面的4行,对应ivalue.h文件的604-608行注释掉了。
修改后,再次运行程序,成功
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第14张图片

六、QT Creator 调用vs封装的lib库

前面已经讲过QT Creator配置opencv、libtorch、realsense sdk,且已经成功运行。
前面文章有讲到用VS封装lib库的方法,以及另一篇文章有讲QT Creator +CMake调用vs封装的lib库的方法。
那QT Creator+qmake调用vs封装的lib库的配置方法如下。

1、编辑.pro文件

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

INCLUDEPATH += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include\
               D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include/opencv\
               D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/include/opencv2\
               D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/include\
               D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/include/torch/csrc/api/include\
               "C:/Program Files (x86)/Intel RealSense SDK 2.0/include/"\
                "D:/software/test_qmake_msvc2017_64bit/include/"
#Debug: {
#LIBS += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib/opencv_world341d.lib\
#        D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib/*.lib\
#        "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"
#}
#Release: {
#LIBS += D:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib/opencv_world341.lib\
#        D:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib/*.lib\
#        "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"
#}

LIBS += -LD:/OpenCV/opencv-3.4.1-vc14_vc15/opencv/build/x64/vc14/lib -lopencv_world341 \
-LD:/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch/lib -lc10 -ltorch -lc10d -ltorch_cpu \
 -lgloo -lcaffe2_module_test_dynamic -lasmjit -lclog -lcpuinfo -ldnnl -lfbgemm

LIBS += "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib"
LIBS += "D:/software/test_qmake_msvc2017_64bit/lib/DetectRs.lib"

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

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

在.pro文件里增加了这两行,封装库的头文件的路径和lib路径:
QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第15张图片

2、在mainwindow.cpp里添加头文件

//#undef slots
//#include 
//#include 
//#define slots Q_SLOTS

//#include
//#include
//#include

//#include
//#include

//using namespace std;
//using namespace cv;
//上面的libtorch、opencv、realsense的头文件注释掉了,因为封装库的头文件"detector.h"和"rs.h"已经包含这些头文件了。
#include "detector.h"
#include "rs.h"
//#undef slots
//#include 
//#include 
//#define slots Q_SLOTS

//#include
//#include
//#include

//#include
//#include

//using namespace std;
//using namespace cv;
//因为封装库里的头文件已经包含了libtorch和opencv的相关头文件,所以这里就可以注释掉,只包含封装库的头文件即可

#include "detector.h"
#include "rs.h"

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
       // 加载数据分类的名称
       std::vector<std::string> class_names = LoadNames("D:\\software\\test\\weights\\class.names");
       if (class_names.empty()) {
           return;
       }

       // 加载模型
       std::string weights = "D:\\software\\test\\weights\\model.torchscript.pt";

       // set device type - CPU/GPU
       torch::DeviceType device_type;
       device_type = torch::kCPU;
       
       //封装库的类的初始化函数
       auto detector = Detector(weights, device_type);
       
       //定义结构体
       realsenseData rsData;
       realsenseInit(rsData);//调用封装库里的函数。
}

3、点击release 运行工程,成功

QT Creator + qmake+MSVC2017_64bit+Libtorch1.8.1+opencv+realsense sdk配置+调用vs封装的lib库_第16张图片

你可能感兴趣的:(opencv,qt,c++)