c++ - Qt 和英特尔实感入门

标签 c++ visual-studio qt opencv realsense

作为一个初学者,我不知道该去哪里问。所以如果你能帮忙...

我想使用以下方法创建一个项目:

  • Qt5.15.0
  • Visual Studio 2019
  • C++
  • openCV 4.2.0(如果需要)
  • 英特尔实感摄像头

实际上,首先,我需要一个更简单的项目,例如:英特尔实感 SDK 2.0 示例,

  1. im-show
  2. 或/和“hello-realsense

代码示例将是最好甚至更好的教程。
到目前为止我还没有在谷歌上找到任何东西。
如果上述内容很困难,那么有关如何开始的建议就可以了。

例如,我发现:“Qt and openCV on Windows with MSVC
这对我来说是一个好的起点吗?我是否需要像“im-show”项目那样使用 openCV 来显示/显示深度图像?

预先感谢您。

最佳答案

这是一个非常简单的示例,仅使用 Qt 和 Intel Realsense SDK。

我们首先编写一个处理相机的类:

#ifndef CAMERA_H
#define CAMERA_H

// Import QT libs, one for threads and one for images
#include <QThread> 
#include <QImage>

// Import librealsense header
#include <librealsense2/rs.hpp>

// Let's define our camera as a thread, it will be constantly running and sending frames to
// our main window
class Camera : public QThread 
{
    Q_OBJECT
public:
    // We need to instantiate a camera with both depth and rgb resolution (as well as fps)
    Camera(int rgb_width, int rgb_height, int depth_width, int depth_height, int fps);
    ~Camera() {}

    // Member function that handles thread iteration
    void run();
    
    // If called it will stop the thread
    void stop() { camera_running = false; }

private:
    // Realsense configuration structure, it will define streams that need to be opened
    rs2::config cfg;

    // Our pipeline, main object used by realsense to handle streams
    rs2::pipeline pipe;

    // Frames returned by our pipeline, they will be packed in this structure
    rs2::frameset frames;

    // A bool that defines if our thread is running
    bool camera_running = true;

signals:
    // A signal sent by our class to notify that there are frames that need to be processed
    void framesReady(QImage frameRGB, QImage frameDepth);
};
// A function that will convert realsense frames to QImage
QImage realsenseFrameToQImage(const rs2::frame& f);

#endif // CAMERA_H

为了完全理解这个类的内容,我将您重定向到这两个页面: Signals & SlotsQThread 。这个类是一个QThread,这意味着它可以与我们的主窗口并行运行。当几帧准备好时,将发出framesReady 信号,窗口将显示图像。

我们首先来说一下如何使用 librealsense 打开摄像头流:

Camera::Camera(int rgb_width, int rgb_height, int depth_width, int depth_height, int fps)
{
    // Enable depth stream with given resolution. Pixel will have a bit depth of 16 bit
    cfg.enable_stream(RS2_STREAM_DEPTH, depth_width, depth_height, RS2_FORMAT_Z16, fps);
    
    // Enable RGB stream as frames with 3 channel of 8 bit
    cfg.enable_stream(RS2_STREAM_COLOR, rgb_width, rgb_height, RS2_FORMAT_RGB8, fps);

    // Start our pipeline
    pipe.start(cfg);
}

正如您所看到的,我们的构造函数非常简单,它只会使用给定的流打开管道。

现在管道已启动,我们只需要获取相应的帧即可。我们将在“run”方法中执行此操作,该方法将在 QThread 启动时启动:

void Camera::run()
{
    while(camera_running)
    {
        // Wait for frames and get them as soon as they are ready
        frames = pipe.wait_for_frames();

        // Let's get our depth frame
        rs2::depth_frame depth = frames.get_depth_frame();
        // And our rgb frame
        rs2::frame rgb = frames.get_color_frame();

        // Let's convert them to QImage
        auto q_rgb = realsenseFrameToQImage(rgb);
        auto q_depth = realsenseFrameToQImage(depth);

        // And finally we'll emit our signal
        emit framesReady(q_rgb, q_depth);
    }
}

执行转换的函数如下:

QImage realsenseFrameToQImage(const rs2::frame &f)
{
    using namespace rs2;

    auto vf = f.as<video_frame>();
    const int w = vf.get_width();
    const int h = vf.get_height();

    if (f.get_profile().format() == RS2_FORMAT_RGB8)
    {
        auto r = QImage((uchar*) f.get_data(), w, h, w*3, QImage::Format_RGB888);
        return r;
    }
    else if (f.get_profile().format() == RS2_FORMAT_Z16)
    {
        // only if you have Qt > 5.13
        auto r = QImage((uchar*) f.get_data(), w, h, w*2, QImage::Format_Grayscale16);
        return r;
    }

    throw std::runtime_error("Frame format is not supported yet!");
}

我们的相机完成了。

现在我们将定义我们的主窗口。我们需要一个用于接收框架的插槽和两个用于放置图像的标签:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

public slots:
    // Slot that will receive frames from the camera
    void receiveFrame(QImage rgb, QImage depth);

private:
    QLabel *rgb_label;
    QLabel *depth_label;
};

#endif // MAINWINDOW_H

我们为窗口创建一个简单的 View ,其中的图像将垂直显示。

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    // Creates our central widget that will contain the labels
    QWidget *widget = new QWidget();
 
    // Create our labels with an empty string
    rgb_label = new QLabel("");
    depth_label = new QLabel("");

    // Define a vertical layout
    QVBoxLayout *widgetLayout = new QVBoxLayout;

    // Add the labels to the layout
    widgetLayout->addWidget(rgb_label);
    widgetLayout->addWidget(depth_label);

    // And then assign the layout to the central widget
    widget->setLayout(widgetLayout);

    // Lastly assign our central widget to our window
    setCentralWidget(widget);
}

现在我们需要定义槽函数。分配给该功能的唯一工作是更改与标签相关的图像:

void MainWindow::receiveFrame(QImage rgb, QImage depth)
{
    rgb_label->setPixmap(QPixmap::fromImage(rgb));
    depth_label->setPixmap(QPixmap::fromImage(depth));
}

我们就完成了!

最后,我们编写 main,它将启动我们的线程并显示我们的窗口。

#include <QApplication>
#include "mainwindow.h"
#include "camera.h"

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

    MainWindow window;
    Camera camera(640, 480, 320, 240, 30);

    // Connect the signal from the camera to the slot of the window
    QApplication::connect(&camera, &Camera::framesReady, &window, &MainWindow::receiveFrame);

    window.show();

    camera.start();

    return a.exec();
}

关于c++ - Qt 和英特尔实感入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62474665/

相关文章:

c++ - 内存模型,负载获取语义实际上是如何工作的?

c++ - 为 C++ 库构建 C 包装器时出现 cmath 语法错误

c++ - CLion IDE 是否包含 ReSharper C++ 在 Visual Studio 下提供的所有功能?

c++ - 使用带有 C++17 的 VS C++ 编译器 15.0 构建 Qt 项目以使用 WinRT API

c++ - 反向偏移分词器

c++ - move 语义和引用语义

visual-studio-2010 - MSBuild 目标,BeforeCompile,在加载项目文件时触发

visual-studio - Notepad++的ASP.NET Razor语法突出显示

python - 如何使 QLabel 的边框看起来像 QTreeWidget 等其他小部件的边框?

Qt creator 启用 SSL