访问网络摄像头失败

时间:2011-03-27 08:18:50

标签: opencv

我仍然是opencv的新手,我根据示例制作简单的程序来访问网络摄像头但总是失败。我将变量id更改为0,1,2 ... 100但我得到了相同的结果。这是我的计划:

#include "cv.h"
#include "highgui.h"

#include "stdio.h"
#include "iostream"

// A Simple Camera Capture Framework
int main()
{
IplImage* img = NULL;
CvCapture* cap = NULL;
int id=0;

cap = cvCaptureFromCAM(id);
cvNamedWindow("Images",CV_WINDOW_AUTOSIZE);

if ( !cap )
printf("ERROR\n\n");
else
for(;;)
{
img = cvQueryFrame(cap);
cvShowImage("Imagenes", img);
cvWaitKey(10);
}

cvReleaseImage(&img);
cvReleaseCapture(&cap);

return 0;
}

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

帮自己一个忙,并检查功能的返回。也许他们中的一些人失败了,你永远不会知道为什么。

另一个提示:尝试使用id = -1

#include <iostream>
#include <sstream>
#include <string>

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

int main()
{
    CvCapture* capture = NULL;
    if ((capture = cvCaptureFromCAM(-1)) == NULL)
    {
        fprintf(stderr, "ERROR: capture is NULL \n");
        return -1;
    }

    cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);

    cvQueryFrame(capture); // Sometimes needed to get correct data

    IplImage* frame  = NULL;
    while (1)
    {
        if ((frame = cvQueryFrame(capture)) == NULL)
        {
            fprintf( stderr, "ERROR: cvQueryFrame failed\n");
            break;
        }

        if (frame == NULL)
        {
            usleep(100000);
            continue;
        }

        cvShowImage("mywindow", frame); // Do not release the frame!

        int key = cvWaitKey(10);
        if (key  == 27)  // ESC was pressed
            break;
    }

    cvReleaseCapture(&capture);
    cvDestroyWindow("mywindow");

    return 0;
}