【问题标题】:Qt OpenCV Webcam Stream Opening and ClosingQt OpenCV 网络摄像头流打开和关闭
【发布时间】:2013-10-18 23:05:06
【问题描述】:

我使用Qt 创建了一个非常简单的用户界面,它由一个简单的按钮和一个标签组成。当按钮的clicked() 信号发出时,将调用使用OpenCV 从网络摄像头捕获帧的函数。我目前用来实现这一点的代码是:

cv::Mat MainWindow::captureFrame(int width, int height)
{

    //sets the width and height of the frame to be captured
    webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
    webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);

    //determine whether or not the webcam video stream was successfully initialized
    if(!webcam.isOpened())
    {
        qDebug() << "Camera initialization failed.";
    }

    //attempts to grab a frame from the webcam
    if (!webcam.grab()) {
        qDebug() << "Failed to capture frame.";
    }

    //attempts to read the grabbed frame and stores it in frame
    if (!webcam.read(frame)) {
        qDebug() << "Failed to read data from captured frame.";
    }

    return frame;
}

捕获帧后,必须将其转换为QImage 才能显示在标签中。为了实现这一点,我使用了以下方法:

QImage MainWindow::getQImageFromFrame(cv::Mat frame) {
    //converts the color model of the image from RGB to BGR because OpenCV uses BGR
    cv::cvtColor(frame, frame, CV_RGB2BGR);
    return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
}

MainWaindow 类的构造函数如下所示:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    resize(1280, 720);
    move(QPoint(200, 200));
    webcam.open(0);
    fps = 1000/25;
    qTimer = new QTimer(this);
    qTimer->setInterval(fps);
    connect(qTimer, SIGNAL(timeout()), this, SLOT(displayFrame()));
}

QTimer 应该通过调用 dislayFrame() 来显示一个框架

void MainWindow::displayFrame() {
    //capture a frame from the webcam
    frame = captureFrame(640, 360);
    image = getQImageFromFrame(frame);

    //set the image of the label to be the captured frame and resize the label appropriately
    ui->label->setPixmap(QPixmap::fromImage(image));
    ui->label->resize(ui->label->pixmap()->size());
}

每次发出 timeout() 信号时。 然而,虽然这似乎在一定程度上有效,但实际发生的是来自我的网络摄像头(Logitech Quickcam Pro 9000)的视频捕获流反复打开和关闭。表明网络摄像头已打开的蓝色环反复闪烁和关闭的事实证明了这一点。这导致网络摄像头视频流标签的刷新率非常低,是不可取的。有没有办法让网络摄像头流保持打开状态,以防止这种“闪烁”的发生?

【问题讨论】:

    标签: c++ qt opencv video-streaming video-capture


    【解决方案1】:

    我好像解决了网络摄像头流打开和关闭的问题,去掉了这些行:

    webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
    webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);
    

    来自captureFrame() 函数并在MainWindow 构造函数中设置要捕获的帧的宽度和高度。

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2012-06-16
      • 2012-07-23
      • 2017-02-09
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多