繁体   English   中英

在 QLabel 中显示视频

[英]Displaying video in QLabel

我正在尝试在 QT Creator 的 QLabet 中显示视频。 我正在使用 openCV 阅读视频。 这是我的代码:

主窗口.cpp

#include "includes.h"
#include "vidreadthread.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    VidReadThread *thread1 = new VidReadThread("Video read thread");
    thread1->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

视频读取线程.cpp

#include "vidreadthread.h"
#include "includes.h"

using namespace cv;
extern MainWindow *mainPtr;

VidReadThread::VidReadThread(QString s) : name(s)
{
}

void VidReadThread::run()
{
    QThread::msleep(100);
    VideoCapture cap;
    cap.open("helicopter_with_stickers.mp4");
    while(1)
    {
        Mat image1;
       // Capture frame-by-frame
        cap >> image1;

       // If the frame is empty, break immediately
       if (image1.empty())
         break;

       QImage image2 = QImage((uchar*) image1.data, image1.cols, image1.rows, image1.step, QImage::Format_RGB888);
       mainPtr->ui->label1->setPixmap(QPixmap::fromImage(image2));
    }
}

我可以显示视频,但无法设置帧速率。 整个 60 秒的视频在 4-5 帧内结束。 仅使用 OpenCV,我可以使用cvWaitkey()来控制帧速率,但这里msleep()似乎不适用于类似的应用程序。 请提出一种无需跳帧的方法。 我制作了一个vidreadthread以便在阅读视频时不会挂起 GUI。

如果有任何其他方式可以在我的 QT UI 中显示 OpenCV 窗口,那么也请推荐。

尝试 moveto 线程可能效果更好

.cpp

for (int i = 0; i<Camera::getCameraCount();)
        ui->comboBox->addItem(QString::number(i++)); //name camera
    camera = new Camera();
    camera->moveToThread(&thread); 

connect(this, SIGNAL(cameraOperate(int)), camera, SLOT(Operate(int))); 
connect(camera, SIGNAL(updateImage(QImage)), this, SLOT(updateImage(QImage))); 


void app0::updateImage(QImage image)
{
    ui->videoviewer->setPixmap(QPixmap::fromImage(image));
}

相机线程:

void Camera::Operate(int _index)
{
    if (open(_index)) { qDebug() << "Camera open success!";}
    else { qDebug() << "Camera open failed!"; return; }
    if (capture.get(28) == -1) { cout << "get 28 -1" << "\n";emit }
    while (1)
    {
        qApp->processEvents();
        Mat matin = read(); //read mat
        matnow = matin;
        QImage image = Mat2QImage(matin);
        emit updateImage(image);
    }
}

链接: https : //blog.csdn.net/Sun_tian/article/details/104236327

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM