Ask Your Question
-1

OpenCV can't capture the frame from the webcam.

asked 2013-08-22 07:20:52 -0600

Dejan gravatar image

updated 2013-08-22 08:57:58 -0600

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;
}
edit retag flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 2013-08-22 08:05:45 -0600 )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 ( 2013-08-22 08:59:15 -0600 )edit

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

FLY gravatar imageFLY ( 2013-08-23 13:16:50 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-08-22 08:01:35 -0600

updated 2013-08-22 08:30:11 -0600

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);
    }
}
edit flag offensive delete link more

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 ( 2013-08-22 08:56:42 -0600 )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 ( 2013-08-22 10:38:48 -0600 )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 ( 2013-08-22 11:18:40 -0600 )edit
1

answered 2013-08-23 13:15:13 -0600

FLY gravatar image

updated 2013-08-23 13:19:13 -0600

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

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-08-22 07:20:52 -0600

Seen: 8,571 times

Last updated: Aug 23 '13