2011-03-27 140 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