Ask Your Question
0

Accessing to webcam video?

asked Sep 5 '13

diegoaguilar gravatar image

updated Sep 5 '13

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.

Preview: (hide)

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 (Sep 5 '13)edit

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

diegoaguilar gravatar imagediegoaguilar (Sep 5 '13)edit
1

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

berak gravatar imageberak (Sep 5 '13)edit

1 answer

Sort by » oldest newest most voted
1

answered Sep 5 '13

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);
Preview: (hide)

Comments

It's taking me to an exception

diegoaguilar gravatar imagediegoaguilar (Sep 6 '13)edit

And what is the exception?

Moster gravatar imageMoster (Sep 6 '13)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 (Sep 6 '13)edit

Question Tools

Stats

Asked: Sep 5 '13

Seen: 389 times

Last updated: Sep 05 '13