OpenCV cvCaptureFromCAM returns zero

2019-06-03 07:21发布

问题:

I got OpenCV 2.1 installed on my laptop and is trying to implement a face detection program. I'm using Logitech C210 for my project. I know the camera is okay because the software detects and displays it, and starting facedetect.exe in the samples directory shows the camera to be working. But somehow when using the original facedetect.cpp code in my VS2010 Ultimate I couldn't even get the cvCaptureFromCAM to work! Here's my code:

#include "stdafx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

int main(int, char**) {
IplImage* frame;
// CvCapture* cap = cvCaptureFromCAM(-1);
//  cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZE );
CvCapture* capture;
cvWaitKey(20);
capture = cvCreateCameraCapture( -1 ); //yes, if 0 doesn't work try with -1
//assert( capture != NULL );

for(;;) {
    frame = cvQueryFrame(capture);

    if(frame == NULL)
        return -1;

   imshow("cap", frame);
    if(waitKey(30) >= 0)
        break;
}

}

Okay, so that isn't the actual facedetect code(it's too long), but it highlights the problem here it think:

Using breakpoints I found out that the value capture after cvCaptureFromCAM is 0x000000. This isn't supposed to happen, is it? Can someone tell me what's going on?

回答1:

Try duplicating the calls. Please refer to my question which looks similar. How to avoid "Video Source -> Capture source" selection in OpenCV 2.3.0 - Visual C++ 2008



回答2:

Try upgrading to OpenCV 2.4.2. Not the exact same problem ( for me queryFrame() was returning always NULL using v2.2.0), but now it works like a charm. Maybe are just compatibility issues.