cvQueryFrame always return NULL

asked 2015-11-28 06:29:51 -0600

sorry for my poor English,but I meet a annoying problem.I use a laptop with win 8.1 system and opencv 3.0. I wrote a small program hoping to show the video captured from the built-in camera, but failed. I figured out that the problem lies in the function cvQueryFrame(), as it always return NULL but the camera is opened. the code is as follow:

int main()
{

CvCapture* capture=cvCaptureFromCAM(-1);
if(capture==NULL)
{
    printf("camera not available");
    return 0;
}
cvNamedWindow("video",1);
char c = cvWaitKey(1000);
IplImage* frame=cvQueryFrame(capture);
for (;;)
{
    frame=cvQueryFrame(capture);
    if(!frame)
        break;
    cvShowImage("video",frame);
    if(cvWaitKey(100)>=0)
        break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("video");
return 0;

}

edit retag flag offensive close merge delete

Comments

please, you must not use the c-api .

try this

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera

        imshow("VideoCapture Basic Demo", frame);

        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
berak gravatar imageberak ( 2015-11-28 07:47:43 -0600 )edit

oh thank you,it works,but why?would you please give me some hints or reference?

sum_of_lilith gravatar imagesum_of_lilith ( 2015-11-28 11:11:57 -0600 )edit