使用OpenCV在Qt控件上播放视频

OpenCV在Qt控件上播放视频实现
本文介绍如何使用OpenCV解码播放Mp4文件,并将其图像显示到Qt的QLabel控件中。核心代码包括图像格式转换和QLabel的显示方法。通过将OpenCV读取的BGR格式转换为RGB,然后利用QImage和QPixmap显示在Qt界面。运行结果显示图像清晰。

代码已上传至码云:https://gitee.com/fensnote/demo_code/tree/master/qtCode/opencv_video

简介

opencv是一个开源计算机视觉库,功能非常多,这里简单介绍一下OpenCV解码播放Mp4文件,并将图像显示到Qt的QLabel上面。

核心代码

头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

using namespace std;
using namespace cv;


namespace Ui {
   
   
class MainWindow;
}

class MainWindow : public QMainWindow
{
   
   
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void InitVideo();

private slots
### 如何在 Qt使用 OpenCV 播放视频 为了实现这一目标,可以创建一个基于 Qt 的应用程序,在其中集成 OpenCV 来处理和显示视频帧。下面是一个简单的例子来说明如何做到这一点。 #### 创建项目结构并配置环境变量 确保已经安装好 OpenCV 并设置好了环境变量[^2]。对于 Qt 项目而言,还需要适当调整 `.pro` 文件以便链接必要的库文件: ```qmake QT += core gui widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # 添加 Opencv 库路径 INCLUDEPATH += /path/to/opencv/include \ /path/to/opencv/build/include LIBS += -L/path/to/opencv/lib \ -lopencv_core \ -lopencv_videoio \ -lopencv_highgui TARGET = VideoPlayerQtOpencvExample TEMPLATE = app ``` #### 编写主窗口类 `MainWindow.h` 定义用于展示图像的自定义控件以及加载视频的功能按钮等组件。 ```cpp #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QLabel> class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); private slots: void onPlayButtonClicked(); private: QPushButton* m_playButton; QLabel* m_imageLabel; }; #endif // MAINWINDOW_H ``` #### 实现主窗口逻辑 `MainWindow.cpp` 这里实现了打开摄像头或读取本地视频文件,并通过定时器不断更新界面中的图片标签以达到连续播放的效果。 ```cpp #include "mainwindow.h" #include <QVBoxLayout> #include <QTimer> #include <QDebug> #include <opencv2/opencv.hpp> using namespace cv; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_playButton(new QPushButton(tr("Play"), this)), m_imageLabel(new QLabel(this)) { QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(m_playButton); layout->addWidget(m_imageLabel); QWidget* centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget); connect(m_playButton, SIGNAL(clicked()), SLOT(onPlayButtonClicked())); } void MainWindow::onPlayButtonClicked() { QString fileName = QFileDialog::getOpenFileName( this, tr("Select a video file"), "", tr("Video Files (*.avi *.mpg);;All Files (*)")); if (!fileName.isEmpty()) { VideoCapture cap(fileName.toStdString()); Mat frame; QTimer timer; QObject::connect(&timer,SIGNAL(timeout()), [&]() -> void { cap >> frame; if (frame.empty()) return; QImage img((const uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped(); m_imageLabel->setPixmap(QPixmap::fromImage(img)); }); timer.start(30); // 设置刷新频率为每秒约33次(即每隔30ms) } } ``` 此段代码展示了基本框架下的操作流程,实际应用时可能需要考虑更多细节如错误处理、性能优化等问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值