2011-03-27 144 views
0

我仍然是新的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; 
} 

感谢你的帮助

+0

您正在使用哪种版本的OpenCV?而哪个操作系统?视窗? Linux呢?苹果电脑? – karlphillip 2011-03-27 09:07:55

+0

如果您是新手,并且从#include“iostream”中看到您使用C++,则使用相当类型的'cv :: Mat'是有益的,因此您不会像cvReleaseImage或cvReleaseCapture那样写行,因为它是自动的。 。如果你使用printf,你为什么要#include“iostream”? – 2012-11-01 23:39:44

回答

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; 
} 
+0

我的程序编译完美,我已经改变id = -1但仍然得到相同的结果,我尝试从opencv访问凸轮的示例程序(.exe)它运行良好(我很抱歉,我的英语不好,我希望你明白) – subman 2011-03-27 09:04:35

+0

@subman我刚刚更新了代码,它使用OpenCV v2.1 – karlphillip 2011-03-30 21:49:41