Ask Your Question
0

Accessing to webcam video?

asked 2013-09-05 15:31:35 -0600

diegoaguilar gravatar image

updated 2013-09-05 15:53:34 -0600

I'm testing out some code I found to capture the web cam video stream, it's quite different from what I used to do for achieving the same but it's supposed to be the appropiate way for doing it.

This is the way I used to do:

CvCapture* capture;
IplImage* frame = 0;

while (true)
{
    //Read the video stream
    capture = cvCaptureFromCAM(1);
    if (! capture) break;
    frame = cvQueryFrame(capture);

    //Create a window to display 
    cvNamedWindow("Te estas viendo", CV_WINDOW_NORMAL|CV_WINDOW_KEEPRATIO);

    cvShowImage("Te estas viendo", frame);

    int c = cvWaitKey(10);
    if ( (char)c == 27 ) break;
}

//Clean and release resources
cvReleaseImage(&frame);
cvDestroyAllWindows();

This is the testing code:

VideoCapture camera;
camera.open(cameraNumber);
namedWindow("output");

if (!camera.isOpened()) {
    cerr << "ERROR: Could not access the camera or video!" <<endl;
    return -1;
}

while (true) {
    // Grab the next camera frame.
    cv::Mat cameraFrame;
    camera >> cameraFrame;
    if (cameraFrame.empty()) {
        std::cerr << "ERROR: Couldn't grab a camera frame." <<
            std::endl;
        return -1;
    }
    imshow("output", cameraFrame);
    waitKey(10);
}

I'm not getting the first error line, so it's supposed to be opening the camera, but always failing at grabbing camera frames.

edit retag flag offensive close merge delete

Comments

please get the braces right, as already proposed on SO [ also, that wouldn't even compile, so can't be the exact code you're actually using ]

berak gravatar imageberak ( 2013-09-05 15:47:45 -0600 )edit

I did, @berak, I simply did a bad copy paste of question from SO.

diegoaguilar gravatar imagediegoaguilar ( 2013-09-05 15:55:09 -0600 )edit
1

yes, but, correcting that will help you. so please just edit.

berak gravatar imageberak ( 2013-09-05 16:10:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-05 16:37:47 -0600

Moster gravatar image

Im pretty sure this wont work, but you could try to explicitly grab the camera frame.

if (camera.grab())
   camera.retrieve(camera_frame);
edit flag offensive delete link more

Comments

It's taking me to an exception

diegoaguilar gravatar imagediegoaguilar ( 2013-09-05 22:40:20 -0600 )edit

And what is the exception?

Moster gravatar imageMoster ( 2013-09-06 00:49:31 -0600 )edit

It actually works, BUT I had to downgrade to 2.4.5 version, I know know it's a reported bug.

diegoaguilar gravatar imagediegoaguilar ( 2013-09-06 16:12:56 -0600 )edit

Question Tools

Stats

Asked: 2013-09-05 15:31:35 -0600

Seen: 367 times

Last updated: Sep 05 '13