我正在使用Opencv进行一些实时视频处理。使用OpenCV从网络摄像头捕捉视频时,QT GUI冻结
作为前端,我正在使用QT框架。
在我的GUI上,我有一个输入图像窗口(映射到标签)和一个输出图像窗口(映射到另一个标签)和3个按钮。一个开始输入视频捕捉,第二个处理视频(代码尚未写入),第三个退出。
我目前能够流式传输视频并将其显示在前端。但是这会锁住我的GUI并且无法退出。
我试过使用QTimers(使用来自这个和QT论坛的建议),但我的GUI仍然保持锁定状态。
如果有人能指出我的方向正确,我们将不胜感激。
下面是代码:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp> // for cvtColor
#include <iostream>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_buttonCaptureVideo_clicked();
void on_buttonExit_clicked();
public slots:
virtual void doNextFrame() {repaint();}
private:
Ui::MainWindow *ui;
CvCapture *capture; // OpenCV Video Capture Variable
IplImage *frame; // Variable to capture a frame of the input video
cv::Mat source_image; // Variable pointing to the same input frame
cv::Mat dest_image; // Variable to output a frame of the processed video
QTimer *imageTimer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
void MainWindow::on_buttonCaptureVideo_clicked()
{
// Set to 25 frames per second
const int imagePeriod = 1000/25; // ms
imageTimer = new QTimer(this);
imageTimer->setInterval(imagePeriod);
connect(imageTimer, SIGNAL(timeout()), this, SLOT(doNextFrame()));
// Use the default camera
capture = cvCreateCameraCapture(-1);
while(capture)
{
// Capture a frame
frame = cvQueryFrame(capture);
// Point to the same frame
source_image = frame;
// Resize Image
cv::resize(source_image, source_image, cv::Size(128,128) , 0, 0);
// Change to RGB format
cv::cvtColor(source_image,source_image,CV_BGR2RGB);
// Convert to QImage
QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols, source_image.rows, QImage::Format_RGB888); // convert to QImage
// Display on Input Label
ui->labelInputVideo->setPixmap(QPixmap::fromImage(qimg));
// Resize the label to fit the image
ui->labelInputVideo->resize(ui->labelInputVideo->pixmap()->size());
}
}
void MainWindow::on_buttonExit_clicked()
{
connect(ui->buttonExit, SIGNAL(clicked()), qApp, SLOT(closeAllWindows()));
}
“捕获”评估为真的时间有多长?那个while循环运行多久了? – jdi 2012-07-23 04:58:37
@jdi:只要用户希望流式传输输入视频,捕获应该评估为真。我已经得到了使用QTimer进行响应的GUI,但是我注意到,如果我将流动激活时间过长,我会收到消息“Camera dropped frame!”,所以我的猜测是最终我需要移动到QThreads。感谢您的回复。 – Sid 2012-07-24 02:58:43
@Sid,你可以发布代码吗?谢谢 – 2013-03-27 17:50:33