error:capture is NULL in opencv in window7

960 views Asked by At

I've seen other posts here similar to this question and even goggled and attempted to try every possible method stated, but neither of them worked for me. following code is just to capture the image infinitely from webcam and Code is building successfully getting error "error:capture is NULL".

Does opencv2.2.0 is supported for windows 7, i have seen in many posts where it is mentioned to use Direct show for video capturing in window 7

#include<opencv/cxcore.h>
#include<opencv/highgui.h>
#include<opencv/cxcore.h>

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char* argv[])
{
    CvSize size640x480 = cvSize(640,480);
    CvCapture* p_capWebcam;
    IplImage* p_imgOriginal;

    p_capWebcam=cvCaptureFromCAM(0);//i tried p_capWebcam=cvCaptureFromCAM(CV_CAP_ANY) 
                                    //i tried index from -1 to 10 but nothing worked
    if(p_capWebcam==NULL)
{
    printf("error:capture is NULL");
    getchar();
    return -1;
}


    cvNamedWindow("Original",CV_WINDOW_AUTOSIZE);
    while(1)
    {
    p_imgOriginal=cvQueryFrame(p_capWebcam);
    if(p_imgOriginal=NULL)
    {
        printf("error :frame is NULL \n");
        break;
    }
    cvWaitKey(10);
    cvShowImage("Original",p_imgOriginal);

    }
}

IDE is Microsoft Visual C++ 2010 Express,

Webcamera(Frontech) usb2.0 supports following formats {'YUY2_160x120' 'YUY2_176x144' 'YUY2_320x240' 'YUY2_352x288' 'YUY2_640x480'}

1

There are 1 answers

1
berak On

you're lacking a call to cvWaitKey(10); after the cvShowImage() (thus your window does not get updated).


and please, move over to the c++ api, the outdated c-api won't be supported for long.

so, the whole thing should look more like this:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;

int main() 
{
    VideoCapture cap(0);
    while( cap.isOpened() )
    {
        Mat frame;
        if ( ! cap.read(frame) )
            break;
        imshow("lalala",frame);
        int k = waitKey(10);
        if ( k==27 )
            break;
    }
    return 0;
}