Search code examples
c++opencvvideowebcam

Opencv: cvCaptureFromCAM returning NULL


I'm in Linux Mint (don't know the version) and am using a Logitech Orbit AF webcam. I try the following code, but all I get is "ERROR: Capture is null!" Please help!!!!!

#include<cv.h>
#include<highgui.hpp>
#include<iostream>

using namespace std;

int main() {
    //Data Structure to store cam.
    CvCapture* cap=cvCaptureFromCAM(0);
    //Image variable to store frame
    IplImage* frame;
    //Window to show livefeed
    cvNamedWindow("LiveFeed",CV_WINDOW_AUTOSIZE);
    if(!cap)
    {
        cout << "ERROR: Capture is null!\n";
    }
    while(1)
    {
        //Load the next frame
        frame=cvQueryFrame(cap);
        //If frame is not loaded break from the loop
        if(!frame)
            break;
        //Show the present frame
        cvShowImage("LiveFeed",frame);
        //Escape Sequence
        char c=cvWaitKey(33);
        //If the key pressed by user is Esc(ASCII is 27) then break out of the loop
        if(c==27)
           break;
    }
    //CleanUp
    cvReleaseCapture(&cap);
    cvDestroyAllWindows(); 

}


Solution

  • When this call returns NULL:

    CvCapture* cap = cvCaptureFromCAM(0);
    if (!cap)
    {
        // print error and exit
        cout << "ERROR: Capture is null!\n";
        return -1;
    }
    

    it means that no device was found at index 0. Try passing CV_CAP_ANY instead to let OpenCV select a valid index number for you.

    If that doesn't work, it may be that your camera is not supported by OpenCV. Try to find it in this list.