【问题标题】:OpenCV and Qt VideoCapture does not open the correct camera on windowsOpenCV 和 Qt VideoCapture 无法在 Windows 上打开正确的摄像头
【发布时间】:2012-05-07 15:28:33
【问题描述】:

我正在使用 opencv 和 Qt 创建一个应用程序。在应用程序内部,我正在创建一个小工具来录制视频。出于这个原因,为了不阻塞主事件线程,我创建了一个包含录制线程的单独对话框。在这个针对初学者的线程中,我只想查看相机输出(我还没有介绍录制代码)。所以我将QThread 子类化,run() 函数如下:

void VideoRecordThread::run(){
    cv::VideoCapture capture;
    cv::Mat frame;
    QImage img;

    qDebug() << "Opening camera" << cameraIndex ;
    capture.open(cameraIndex);

    if(!capture.isOpened()){
        qDebug() << "Could not open camera" << cameraIndex;
        emit threadReturned();
        return;
    }

    while(!stopFlag){
        capture >> frame;
        qDebug() << "Frame Width = " << frame.cols << "Frame Height = " << frame.rows;
        if(frame.cols ==0 || frame.rows==0){
            qDebug() << "Invalid frame skipping";
            continue;
        }
        img = cvMatToQImage(frame); //Custom function
        emit imageCaptured(img);
    }
    capture.release();
    emit threadReturned(); //Custom signal
    qDebug() << "Thread returning";
}

这应该可以工作,但问题是当线程启动时,当我选择一个连接的相机时,我得到一个“突然出现”的新对话框,要求我选择相机,它有时工作,有时工作它没有。这是我得到的对话框:

有什么帮助吗?

【问题讨论】:

  • cameraIndex 是什么值?
  • 如果只有一个摄像头或者使用什么摄像头无关紧要-1可以通过。在 Windows 上,索引 0 打开该对话框供用户选择视频源。

标签: c++ windows qt opencv image-processing


【解决方案1】:

我注意到当某些函数没有从主线程执行时,OpenCV 会出现问题。

将捕获过程的初始化移至应用程序的主线程,并将其余部分留给辅助线程。初始化部分似乎是:

cv::VideoCapture capture;

qDebug() << "Opening camera" << cameraIndex ;
capture.open(cameraIndex);

if(!capture.isOpened())
{
    qDebug() << "Could not open camera" << cameraIndex;
    emit threadReturned();
    return;
}

【讨论】:

  • 是的,但问题是我有一个主窗口 -> recodingdialog ->videorecording 线程。如何将 VideoCapture 从线程 2 级别移动到主窗口?
  • 只需跨类传递cv::VideoCapture 对象。您可以根据需要放入类的构造函数,以便在初始化新类时已经可以访问捕获接口,或者在主线程中成功初始化后添加一个新方法在这些类中设置此变量.或者,作为最后一个(丑陋的)选项,将cv::VideoCapture capture 声明为全局变量——我想出于测试目的,实现和测试会更快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 2016-08-13
  • 2016-02-01
  • 2021-09-10
  • 2018-04-12
  • 2015-03-01
相关资源
最近更新 更多