简体   繁体   中英

Gray screen being displayed during video capture - OpenCV

I am trying to run a program for video capture from the webcam in 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. But now in the C++ API when I try running the following code which uses VideoCapture, a gray screen is getting displayed.

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.

#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:

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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