Ask Your Question
0

Can't grab frame from webcamera

asked 2013-09-20 07:07:22 -0600

Dejan gravatar image

I am using OpenCV 2.4.6, VS2010 with Windows 7 64 bit. I could not grab frame from the camera. The code below is working fine for avi file, but not working to capture from camera. Can anyone help me, how can I capture the frame? Thanks in advance.......

Actually problem in this portion:

bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video file" << endl;
        break;
    }

The full source code:

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if(!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while(1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break; 
        }
    }
    return 0;
}
edit retag flag offensive close merge delete

Comments

What is the error exactly?

Moster gravatar imageMoster ( 2013-09-20 07:15:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-20 07:30:16 -0600

Geppertm gravatar image

Use

cap.grab();
cap.retrieve(&frame, 0);

instead of

cap.read(frame);

Best

edit flag offensive delete link more

Comments

cap.read does exactly the same. It calls cap.grab and then cap.retrieve();

Moster gravatar imageMoster ( 2013-09-20 07:33:45 -0600 )edit

Yes it should do the same but I had the same Problem using VS2010 with Win 7 64-Bit using OpenCV 2.4.4. Using grab + retrieve instead of read solved it.

Geppertm gravatar imageGeppertm ( 2013-09-20 07:49:27 -0600 )edit

Question Tools

Stats

Asked: 2013-09-20 07:07:22 -0600

Seen: 6,861 times

Last updated: Sep 20 '13