Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I want to capture video from webcam, but window disappears after capturing one frame

Hi. I'm trying to capture video from my webcam via OpenCV C++. The program works fine, but the window terminates after displaying just one frame. I've tried Python examples as well on my computer, and they work as advertised. Help please.

click to hide/show revision 2
No.2 Revision

updated 2017-01-19 08:20:43 -0500

berak gravatar image

I want to capture video from webcam, but window disappears after capturing one frame

Hi. I'm trying to capture video from my webcam via OpenCV C++. The program works fine, but the window terminates after displaying just one frame. I've tried Python examples as well on my computer, and they work as advertised. Help please.

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.
    if (!stream1.isOpened())//check if video device has been initialised
        cout << "cannot open camera";
    //unconditional loop
    while (true)
    {
        Mat cameraFrame;
        stream1.read(cameraFrame);
        imshow("cam", cameraFrame);
        if (waitKey(0) >= 0)
            break;
    }
    return 0;
}