QT开发中文网

QT RealSense摄像头的视频流

QT RealSense摄像头的视频流可以通过使用Intel RealSense SDK来实现。以下是基本步骤:

  1. 下载和安装RealSense SDK。
  2. 在QT项目中包含RealSense SDK的头文件和库文件。
  3. 创建一个QMainWindow或QDialog,并添加一个QLabel用于显示视频流。
  4. 使用rs::context类初始化RealSense设备。
  5. 打开深度、颜色和红外相机流,设置它们的分辨率、帧速率等属性。
  6. 在主循环中,读取帧并将其转换为OpenCV Mat格式以便在Qt中显示。

以下是示例代码:

#include <QMainWindow>
#include <QLabel>
#include <opencv2/opencv.hpp>
#include <librealsense2/rs.hpp>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
    {
        // Create QLabel for displaying video stream
        m_label = new QLabel(this);
        setCentralWidget(m_label);

        // Initialize RealSense context and device
        m_context = new rs2::context();
        rs2::device_list devices = m_context->query_devices();
        if (devices.size() == 0) {
            qDebug() << "No RealSense devices found";
            return;
        }
        m_device = new rs2::device(devices.front());
        
        // Enable depth, color, and infrared streams
        rs2::config cfg;
        cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
        cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
        cfg.enable_stream(RS2_STREAM_INFRARED, 1, 640, 480, RS2_FORMAT_Y8, 30);
        m_pipe = new rs2::pipeline(*m_context);
        m_profile = m_pipe->start(cfg);

        // Start main loop
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
        timer->start(10); // Update every 10ms
    }

private slots:
    void update()
    {
        // Wait for next frame
        rs2::frameset frames = m_pipe->wait_for_frames();
        
        // Convert depth and color frames to OpenCV Mat format
        rs2::frame depth_frame = frames.get_depth_frame();
        cv::Mat depth_mat(cv::Size(depth_frame.as<rs2::video_frame>().get_width(),
                                   depth_frame.as<rs2::video_frame>().get_height()),
                          CV_16UC1,
                          (void*)depth_frame.get_data(),
                          cv::Mat::AUTO_STEP);

        rs2::frame color_frame = frames.get_color_frame();
        cv::Mat color_mat(cv::Size(color_frame.as<rs2::video_frame>().get_width(),
                                   color_frame.as<rs2::video_frame>().get_height()),
                          CV_8UC3,
                          (void*)color_frame.get_data(),
                          cv::Mat::AUTO_STEP);

        // Display color stream in QLabel
        QImage qimg((uchar*)color_mat.data,
                    color_mat.cols,
                    color_mat.rows,
                    color_mat.step,
                    QImage::Format_RGB888);
                    
        m_label->setPixmap(QPixmap().fromImage(qimg));
    }

private:
    QLabel *m_label;
    rs2::context *m_context;
    rs2::device *m_device;
    rs2::pipeline *m_pipe;
    rs2::pipeline_profile m_profile;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=2014

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?