2012-05-29 102 views
4

没有启动程序,我有一台USB摄像头连接到PC,我想用OpenCV来传输图像。这里是我的代码:在C++中使用OpenCV流式传输来自摄像头的视频

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

int main() 
{ 

    CvCapture* cameraCapture = cvCaptureFromCAM(CV_CAP_ANY); 
    cvNamedWindow("Camera"); 

    while(1) 
    { 
     IplImage* frame = cvQueryFrame(cameraCapture); 
     cvShowImage("Camera", frame); 
     if((cvWaitKey(10) & 255) == 27) 
      break; 
    } 

    cvReleaseCapture(&cameraCapture); 
    cvDestroyWindow("Camera"); 
} 

的问题是,当我开始计划我得到这个弹出错误:“应用程序无法正确(0xc0150002)启动单击确定关闭应用程序。”我确定我已经包含了所有正确的库,头文件和ddl,所以我真的不知道它有什么问题。

任何帮助解决这个问题将不胜感激。

+0

尝试在cvCaptureFromCAM和cvQueryFrame中添加一些错误检查。不要总是那么乐观,他们确实会失败。 –

+2

调试您的应用程序并找出哪条线路导致此错误。 – karlphillip

+1

尝试从视频中捕捉,如果这样工作,那么可能是相机的问题......兼容性... –

回答

4

我建议你使用OpenCV 2.3.1来尝试这种处理相机的方式。

VideoCapture _videoSource; 
bool camera = 1; 

if(camera) 
{ 
    if(!_videoSource.open(0))    // Try to start camera. 0 = default camera 
    {          
    cout << "Error opening camera" << endl; // here you control why the error happens 
    exit(1);    // Exit if fail   
    } 
} 
else 
{ 
    if(!_videoSource.open(Path+"video.avi")) 
    { 
     cout << "Error opening file" << endl; 
     exit(2);      // Exit if fail 
    } 
} 
_videoSource.set(CV_CAP_PROP_CONVERT_RGB, 1); 

Mat frame; 
namedWindow("Image"); 

while(1) 
{ 
    _videoSource >> frame; 
    imshow("output", frame); 
    return 0; 
} 

如果失败了,您会确定问题出在您的相机上。也许是司机。祝你好运。

相关问题