本文介绍了OpenCV和Qt VideoCapture不会在Windows上打开正确的摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

qDebug()<< 打开相机< cameraIndex;
capture.open(cameraIndex);

if(!capture.isOpened()){
qDebug()< 无法打开相机<< cameraIndex;
emit threadReturned();
return;
}

while(!stopFlag){
capture>>帧;
qDebug()<< Frame Width =<< frame.cols<< Frame Height =<<框架
if(frame.cols == 0 || frame.rows == 0){
qDebug()< 无效跳帧;
continue;
}
img = cvMatToQImage(frame); //自定义函数
emit imageCaptured(img);
}
capture.release();
emit threadReturned(); //自定义信号
qDebug()<< 线程返回;
}

这应该可以工作,但问题是当线程启动时,我得到一个新的对话框蓝色要求我选择相机时选择其中一个相机连接,它有时工作,有时它不。这是我得到的对话框:



解决方案


>我注意到当一些函数不从主线程执行时,OpenCV有问题。



将捕获过程的初始化移动到应用程序的主线程,留下你的次要线程上的其余。初始化部分似乎是:

  cv :: VideoCapture capture; 

qDebug()<< 打开相机< cameraIndex;
capture.open(cameraIndex);

if(!capture.isOpened())
{
qDebug()< 无法打开相机<< cameraIndex;
emit threadReturned();
return;
}


I am using opencv and Qt to create an application. Inside the application I am creating a small tool to record video. For this reason and not to block the main event thread I created a separate dialog that contains a recording thread. In this thread for starters I wanted to just see the camera output(I have not introduced the recording code yet). So I subclassed QThread and the run() function is the following:

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";
}

this is supposed to work, but the problem is that when the thread starts, I get a new dialog "out of the blue" asking me to select the camera when I select one of the cameras connected, it sometimes work and sometimes it doesn't. Here is the dialog that I get:

Any help on what can I do?

解决方案

I've noticed that OpenCV has problems when some functions are not executed from the main thread.

Move the initialization of the capture procedure to the main thread of your application and leave the rest on your secondary thread. The initialization part seems to be:

cv::VideoCapture capture;

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

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

这篇关于OpenCV和Qt VideoCapture不会在Windows上打开正确的摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:39