Ask Your Question
-1

OpenCV can't capture the frame from the webcam.

asked Aug 22 '13

Dejan gravatar image

updated Aug 22 '13

Hello All, I am using OpenCV 2.4.6 with VS2010. I think my webcam can't capture the frame. When I executed the code it has builed successfully. But I am not getting output. I think, when I checked "if(!bSuccess)" its executed and can't capture frame from the webcam. Please help me, how can I resolve it. Thanks in advanced.........

Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
   cout << "Cannot read a frame from video file" << endl;
   break;
}
Preview: (hide)

Comments

good idea to check cap.isOpened() before attempting to read()

berak gravatar imageberak (Aug 22 '13)edit

also, it is a bit strange that your code snippet doesn't contain a creating of a VideoCapture element to actually capture frames from.

StevenPuttemans gravatar imageStevenPuttemans (Aug 22 '13)edit

Your trying to save every frame or you just want to run your webcam ?

FLY gravatar imageFLY (Aug 23 '13)edit

2 answers

Sort by » oldest newest most voted
1

answered Aug 22 '13

updated Aug 22 '13

try that:

VideoCapture cap(0);
if(cap)
{
    while(1)
    {
        Mat image;
        cap>>image;
        if (!image.data) break;
        if (waitKey(30) >= 0) break;

        imshow("test",image);
        waitKey(1);
    }
}
Preview: (hide)

Comments

As an addition to this, you could replace the !image.data by the function image.empty() which does internally the same thing ;)

StevenPuttemans gravatar imageStevenPuttemans (Aug 22 '13)edit

include "opencv2/highgui/highgui.hpp"

include <iostream>

using namespace cv; using namespace std; int main(int argc, char* argv[]) { VideoCapture cap(0); if (!cap.isOpened())
{ cout << "Cannot open the video file" << endl; return -1; } double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); cout << "Frame size : " << dWidth << " x " << dHeight << endl; namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); while (1) { Mat frame; bool bSuccess = cap.read(frame); if (!bSuccess) { cout << "Cannot read a frame from video file" << endl; break; } imshow("MyVideo", frame); if (waitKey(30) == 27) { cout << "key is pressed" << endl; break; } } return 0; }

Dejan gravatar imageDejan (Aug 22 '13)edit

Hello dear, I submitted the full code in the above comment. Video Capture in ok. but when the frame is capture that time problem happened. When check the if structure for capturing frame that time I got message "Cannot read a frame from video file" and program terminated. Please give a solution about it.

Dejan gravatar imageDejan (Aug 22 '13)edit
1

answered Aug 23 '13

FLY gravatar image

updated Aug 23 '13

Try this

    CvCapture *capture1 = cvCaptureFromCAM(0);
    if( !capture1 ) return 1; 
    cvNamedWindow("Video1");
    while(true) 
    {
        //grab and retrieve each frames of the video sequentially 
        IplImage* frame1 = cvQueryFrame( capture1 );

        cvShowImage( "Video1", frame1 );

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if((char)c==27 ) break;
    }
}

Hope this works for you , and the code which you paste in comment kindly paste it in your question , so that every one can better understand what your trying to do

In c++ format you can get the answer from here answer

Preview: (hide)

Question Tools

Stats

Asked: Aug 22 '13

Seen: 8,647 times

Last updated: Aug 23 '13