[英]Gray screen being displayed during video capture - OpenCV
我正在尝试运行一个程序来从OpenCV中的网络摄像头捕获视频。 每次我运行该程序时,都会显示灰色屏幕。 我最初尝试使用CvCapture函数在C API中进行编程,并且运行良好。 但是现在在C ++ API中,当我尝试运行以下使用VideoCapture的代码时,将显示灰屏。
我该如何解决这个问题? 请帮忙。 我的OpenCV版本是2.4.6,并且我正在MS Visual Studio 2010 Professional中运行代码。
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
for( ; ; )
{
capture.read(frame);
if(frame.empty())
break;
imshow("w", frame);
waitKey(1);
}
waitKey(0);
}
您的代码在我的笔记本电脑上运行良好。 确保您的摄像头设备未被其他应用程序阻止,或者您可以尝试注释掉namedWindow调用(但这应该不是问题),实际上,您可以使用以下循环从摄像头抓取视频帧:
VideoCapture capture(0);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow( "w", 1);
while(capture.read(frame))
{
imshow("w", frame);
waitKey(1);
}
waitKey(0);
根据文档:“如果没有抓取任何帧(相机已断开连接,或者视频文件中没有其他帧),则这些方法将返回false,而这些函数将返回NULL指针。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.