Visual Studio qt opencv 打开视频

方式一:直接打开

cpp 复制代码
#include "QtWidgetsOpen.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

QtWidgetsOpen::QtWidgetsOpen(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    // 创建VideoCapture对象打开视频
    cv::VideoCapture cap("cat.mp4");
    
    // 检查视频是否成功打开
    if (!cap.isOpened()) {
        qDebug() << "Error: Could not open video file";
        return;
    }

    // 读取并显示视频帧
    cv::Mat frame;
    while (cap.read(frame)) {
        // 显示当前帧
        cv::imshow("Video", frame);
        
        // 等待30ms,按'ESC'键退出
        if (cv::waitKey(30) == 27) {
            break;
        }
    }

    // 释放资源
    cap.release();
    cv::destroyAllWindows();
}

QtWidgetsOpen::~QtWidgetsOpen()
{}

方式二:在qt界面中打开,假如项目名是QtWidgetsOpen,让ai实现

现在添加了一个按钮pushButton 和一个label ,想实现点击按钮,跳出选择视频的窗口,选择视频后,视频在label中播放,注意用opencv实现播放功能

QtWidgetsOpen.h

cpp 复制代码
#pragma once

#include <QtWidgets/QMainWindow>
#include <QTimer>
#include "ui_QtWidgetsOpen.h"
#include <opencv2/opencv.hpp>

class QtWidgetsOpen : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsOpen(QWidget *parent = nullptr);
    ~QtWidgetsOpen();

private slots:
    void on_pushButton_clicked();    // 按钮点击事件槽函数
    void updateFrame();              // 更新视频帧的槽函数

private:
    Ui::QtWidgetsOpenClass ui;
    cv::VideoCapture videoCapture;   // OpenCV视频捕获对象
    QTimer* timer;                   // 定时器用于更新视频帧
    bool isPlaying;                  // 视频播放状态
};

QtWidgetsOpen.cpp

cpp 复制代码
#include "QtWidgetsOpen.h"
#include <QFileDialog>
#include <QMessageBox>

QtWidgetsOpen::QtWidgetsOpen(QWidget *parent)
    : QMainWindow(parent), isPlaying(false)
{
    ui.setupUi(this);
    
    // 初始化定时器
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &QtWidgetsOpen::updateFrame);
}

QtWidgetsOpen::~QtWidgetsOpen()
{
    if (timer->isActive()) {
        timer->stop();
    }
    if (videoCapture.isOpened()) {
        videoCapture.release();
    }
}

void QtWidgetsOpen::on_pushButton_clicked()
{
    // 如果视频正在播放,先停止
    if (isPlaying) {
        timer->stop();
        videoCapture.release();
        isPlaying = false;
        return;
    }

    // 打开文件选择对话框
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("选择视频文件"),
        "",
        tr("视频文件 (*.avi *.mp4 *.mkv);;所有文件 (*.*)"));

    if (fileName.isEmpty())
        return;

    // 打开视频文件
    videoCapture.open(fileName.toStdString());
    if (!videoCapture.isOpened()) {
        QMessageBox::warning(this, tr("错误"), tr("无法打开视频文件!"));
        return;
    }

    // 开始播放视频
    isPlaying = true;
    timer->start(33); // 约30fps
}

void QtWidgetsOpen::updateFrame()
{
    cv::Mat frame;
    if (videoCapture.read(frame)) {
        // 转换为RGB格式
        cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
        
        // 调整图像大小以适应label
        cv::resize(frame, frame, cv::Size(ui.label->width(), ui.label->height()));

        // 将OpenCV的Mat转换为QImage
        QImage image((uchar*)frame.data, frame.cols, frame.rows, 
                    frame.step, QImage::Format_RGB888);

        // 在label中显示图像
        ui.label->setPixmap(QPixmap::fromImage(image));
    }
    else {
        // 视频播放结束
        timer->stop();
        videoCapture.release();
        isPlaying = false;
    }
}