Qt и Intel realsense Начало работы - PullRequest
2 голосов
/ 19 июня 2020

Я не знал, где спросить, как новичок. Итак, если вы можете помочь ...

Я хочу создать проект, используя:

  • Qt 5.15.0
  • Visual studio 2019
  • C ++
  • openCV 4.2.0 (при необходимости)
  • Intel realsense camera

Собственно для начала, Мне нужен более простой проект, например, из следующих: Примеры для Intel RealSense SDK 2.0,

  1. « im-show »
  2. или / и « hello-realsense"

A пример кода будет лучшим или даже лучше учебным пособием.
До сих пор ничего не нашел в гугле.
В случае, если вышеуказанное сложно, совет о том, как начать, будет прекрасен.

Например, я нашел: « Qt и openCV на Windows с MSV C»
Это хорошее место для начала, нужен ли мне openCV показать / отобразить изображение глубины, как в проекте "im-show"?

Заранее благодарю

Ответы [ 2 ]

3 голосов
/ 23 июня 2020

Это очень простой пример, который использует только 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

Чтобы полностью понять, что это class, я перенаправляю вас на эти две страницы: Сигналы и слоты и QThread . Это класс 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

Мы создаем простой вид для окна, с изображениями, которые будут отображаться вертикально.

#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();
}
0 голосов
/ 21 июня 2020

Исходя из вашего вопроса, я понимаю, что вы пытаетесь предварительно просмотреть данные камеры в приложении Qt.

  1. запись imshow () внутри приложения Qt невозможна.
  2. Вы должны преобразовать OpenCV Mat в QImage, как показано ниже, и отобразить его в вашем средстве просмотра (widget / QML)

Mat img; QImage img1 = QImage ((uchar *) img.data, img.cols, img.rows, img.step, QImage :: Format_Indexed8);

...