【发布时间】: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