Ask Your Question

Revision history [back]

I had the same problem and I see that the following code works:

#include <iostream>
#include <highgui.h>
#include <imgproc/imgproc.hpp>

using namespace std;

#define CAMERA_OUTPUT_WINDOW_NAME "camera-output"

int main(int argc, char **argv)
{
    CvCapture *camCapture;
    int ret = 0;
    int skipFrames = 100;
    CvMemStorage* storage;

    if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
        cout << "Failed to capture from camera" << endl;

        ret = 1;

        goto exitCameraOpenFailed;
    }

    cout << "Camera opened successfully" << endl;

    storage = cvCreateMemStorage(0);
    cvNamedWindow(CAMERA_OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    IplImage *cameraFrame;
    int grabFrameRet;

    while (skipFrames-- >= 0) {
        if(cvQueryFrame(camCapture)) {
            // We're getting valid frames now. Break out
            break;
        }
        cvWaitKey(100);
    }

    while ((cameraFrame = cvQueryFrame(camCapture))) {
        if (!cameraFrame) {
            cout << "Retrieve frame returned NULL" << endl;

            break;
        }

        cvShowImage(CAMERA_OUTPUT_WINDOW_NAME, cameraFrame);

        if (cvWaitKey(60) != -1) {
            cout << "Input" << endl;
            break;
        }
    }

    cout << "Done" << endl;

    cvReleaseCapture(&camCapture);
    cvDestroyWindow(CAMERA_OUTPUT_WINDOW_NAME);
exitCameraOpenFailed:
    return ret;
}

The key, for me, was to keep trying for a while (the variable values I used amounts to a 10 second wait). Apparently, the camera takes a while to start streaming frames and the cvQueryFrame call doesn't block until the first valid frame becomes available.

I had the same problem and I see that the following code works:

#include <iostream>
#include <highgui.h>
#include <imgproc/imgproc.hpp>

using namespace std;

#define CAMERA_OUTPUT_WINDOW_NAME "camera-output"

int main(int argc, char **argv)
{
    CvCapture *camCapture;
    int ret = 0;
    int skipFrames = 100;
    CvMemStorage* storage;

    if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
        cout << "Failed to capture from camera" << endl;

        ret = 1;

        goto exitCameraOpenFailed;
    }

    cout << "Camera opened successfully" << endl;

    storage = cvCreateMemStorage(0);
    cvNamedWindow(CAMERA_OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    IplImage *cameraFrame;
    int grabFrameRet;

    while (skipFrames-- >= 0) {
        if(cvQueryFrame(camCapture)) {
            // We're getting valid frames now. Break out
            break;
        }
        cvWaitKey(100);
    }

    while (true) {
        if ((cameraFrame = cvQueryFrame(camCapture))) {
        if (!cameraFrame) {
            cout << "Retrieve frame returned NULL" << endl;

            break;
        }

        cvShowImage(CAMERA_OUTPUT_WINDOW_NAME, cameraFrame);
        }

        if (cvWaitKey(60) != -1) {
            cout << "Input" << endl;
            break;
        }
    }

    cout << "Done" << endl;

    cvReleaseCapture(&camCapture);
    cvDestroyWindow(CAMERA_OUTPUT_WINDOW_NAME);
exitCameraOpenFailed:
    return ret;
}

The key, for me, was cvQueryFrame appears to keep trying for a while (the variable values I used amounts be non-blocking and if there's no frame to a 10 second wait). Apparently, the camera takes a while to start streaming frames and the cvQueryFrame call doesn't block until the first valid frame becomes available.return, it just returns null.