简体   繁体   English

视频捕获期间显示灰屏-OpenCV

[英]Gray screen being displayed during video capture - OpenCV

I am trying to run a program for video capture from the webcam in OpenCV. 我正在尝试运行一个程序来从OpenCV中的网络摄像头捕获视频。 Everytime I run the program, a gray screen is being displayed. 每次我运行该程序时,都会显示灰色屏幕。 I initially tried programming in C API using the CvCapture Function and it worked perfectly fine. 我最初尝试使用CvCapture函数在C API中进行编程,并且运行良好。 But now in the C++ API when I try running the following code which uses VideoCapture, a gray screen is getting displayed. 但是现在在C ++ API中,当我尝试运行以下使用VideoCapture的代码时,将显示灰屏。

How do I resolve this problem? 我该如何解决这个问题? Please help. 请帮忙。 My OpenCV version is 2.4.6 and I am running the code in MS Visual Studio 2010 Professional. 我的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);  
}

Your code is running fine on my laptop. 您的代码在我的笔记本电脑上运行良好。 Make sure that your camera device is not blocked by another application, or you can try to comment out the namedWindow call (but it should not be a problem), actually you can use following loop to grab video frames from camera: 确保您的摄像头设备未被其他应用程序阻止,或者您可以尝试注释掉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);

According to documentation: "If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer." 根据文档:“如果没有抓取任何帧(相机已断开连接,或者视频文件中没有其他帧),则这些方法将返回false,而这些函数将返回NULL指针。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM