在 QTCreator GUI 中播放视频

发布于 2024-12-20 05:25:09 字数 1005 浏览 1 评论 0原文

我目前正在使用 QTCreator 版本 4.7.4 编写一个 GUI 应用程序,该应用程序在应用程序内播放 AVI 文件。

我这样做的方法是将每个帧设置为 QLabel 内的 QPixmap。

         video.load("lk",video.EXT_AVI);
         if(video.hasLoaded()){
              while(!video.hasFinished()){
                  frame = video.getCurrentFrame();
                  cv::cvtColor(frame, frame, CV_BGR2RGB);
                  QImage myImage = QImage( (const unsigned char*) (frame.data),frame.cols, frame.rows, frame.step1(),QImage::Format_RGB888 );
                  ui->displayLabel->setPixmap((QPixmap::fromImage(myImage)));
                  video.nextFrame();
    }
}

由于我使用 opencv API 中的 VideoCapture 类来获取每个帧,因此帧最初作为 CV::MAT 对象检索,因此我将它们转换为 QImage。

如果我在一个简单的控制台程序中使用 opencv API 中的 CV::imshow() 函数,我就可以很好地播放这些文件,但是当我循环运行上述代码时,我的程序基本上会崩溃,直到最后一帧,然后它将返回到显示最后一帧的稳定状态。

我对 C++ 很陌生,但对 Java 有很好的经验,所以我对这个问题的初步猜测是所有事情都是在事件线程上完成的,因此占用了所有资源,导致应用程序停止响应尽管。

任何帮助将不胜感激。

注意:video是我自己的类,它封装了opencv API中的VideoCapture类。

I'm currently using QTCreator version 4.7.4 to write a GUI application which plays AVI files inside the application.

The way I'm doing it is by setting each frame as a QPixmap inside a QLabel.

         video.load("lk",video.EXT_AVI);
         if(video.hasLoaded()){
              while(!video.hasFinished()){
                  frame = video.getCurrentFrame();
                  cv::cvtColor(frame, frame, CV_BGR2RGB);
                  QImage myImage = QImage( (const unsigned char*) (frame.data),frame.cols, frame.rows, frame.step1(),QImage::Format_RGB888 );
                  ui->displayLabel->setPixmap((QPixmap::fromImage(myImage)));
                  video.nextFrame();
    }
}

As I'm using the VideoCapture class from the opencv API to get each frame, frames are initially retrieved as CV::MAT objects, so I convert them into QImages.

I'm able to play the files fine if I'm using using the CV::imshow() functions from the opencv API in a simple console program, but when I run the above code in a loop, my program essentially crashes until the last frame, where it will then return to a stable state showing the very last frame.

I'm quite new to C++, but have good experience with Java, so my initial guess for this problem would be that everything was being done on an event thread, and therefore taking up all the resources, causing the app to stop responding for a while.

Any help will be appreciated.

Note: video is my own class, which encapsulates the VideoCapture class from the opencv API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-12-27 05:25:09

简而言之:

Your code does not update UI during video playback, so program hungs.

详细的

The main problem is that Qt does not update UI just after you make any changes of widget's states in the code. Qt uses events to update widgets. And all events processing in common case run in single thread. Your code is event handler too (probably it is a handler of button click event). Your code changes states of widgets inside a loop, but changes only will be displayed after widgets processed events. But widgets can process events only after current event is processed, that is your code. So, your code really blocks processing until it is finished. And when it is finished, you see last frame.

你应该做什么

你不能在这里使用循环(好吧,你确实可以,但不需要)
您应该创建一个函数,显示一个单独的帧,然后完成让 Qt 处理事件。
Qt 应该以某个固定的时间间隔调用此函数,该时间间隔由帧速率确定
让 Qt 以指定时间间隔调用某个函数的最佳方法是使用 计时器

因此,要开始播放,您应该初始化视频流并启动计时器。显示最后一帧后,您应该停止计时器。

In short:

Your code does not update UI during video playback, so program hungs.

Detailed

The main problem is that Qt does not update UI just after you make any changes of widget's states in the code. Qt uses events to update widgets. And all events processing in common case run in single thread. Your code is event handler too (probably it is a handler of button click event). Your code changes states of widgets inside a loop, but changes only will be displayed after widgets processed events. But widgets can process events only after current event is processed, that is your code. So, your code really blocks processing until it is finished. And when it is finished, you see last frame.

What should you do

You cannot use loop here (ok, you really can, but needn't)
You should make a function, that shows one single frame and than finishes to let Qt process events.
And Qt should call this function with some fixed interval of time, that is determined from framerate
The best way to make Qt call some function with specified time interval is to use a timer.

So, to start a playback you should initialize video stream ant start a timer. After last frame is shown, you should stop timer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文