无法在OpenCV中检测到网络摄像头

嗨我正在尝试使用以下代码检测opencv中的网络摄像头我得到了空白的黑屏虽然我的网络摄像头通过usb连接到我的电脑

我的网络摄像头正在使用** ICatch(VI)PC Camera **驱动程序和我在VS 2008中使用OpenCV 2.1

#include "cv.h" #include "highgui.h" int main( int argc, char** argv ) { cvNamedWindow( "cam", CV_WINDOW_AUTOSIZE ); CvCapture* capture; if (argc==1) { capture = cvCreateCameraCapture( 0 ); } else { capture = cvCreateFileCapture( argv[1] ); } assert( capture != NULL ); IplImage* frame; while(1) { frame = cvQueryFrame( capture ); if( !frame ) break; cvShowImage( "cam", frame ); char c = cvWaitKey(10); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "cam" ); } 

好的,首先……您的网络摄像头是否适用于其他网络摄像头应用程序?

你的代码有点搞砸了! 您创建一个名为Example2_9的窗口,但您尝试使用cvShowImage()绘制到另一个不存在的窗口(名为cam )! 解决了! 用Example2_9替换cam的出现次数。

如果没有解决问题,我可能会用这个替换main()的开头:

 int main( int argc, char** argv ) { cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZE ); CvCapture* capture; capture = cvCreateCameraCapture( -1 ); //yes, if 0 doesn't work try with -1 assert( capture != NULL ); 

您的代码在几个地方缺少错误检查,请小心。 其中一个function可能是返回错误,在您进行正确检查之前,您永远不会知道。

您还可以在Google上找到一堆其他OpenCV示例,它们调用cvCaptureFromCAM()而不是cvCreateCameraCapture()。 如果上述建议不起作用,请尝试一下!

还有一件事,在我的Macbook Pro上,我必须使用cvCaptureFromCAM(0)才能使应用程序正常工作。 在Linux上,我总是使用cvCaptureFromCAM(-1)。

我在尝试阅读LearningOpenCV一书的例子2-9时遇到了同样的问题。

我正在VM中使用Win7-Prof上的VS13 Ultimate进行编码; 来自Host-PC的WebCam是BisonCam,NB Pro; 我尝试了cvCreateCameraCapture的不同变体,它总是返回NULL; 我甚至成功地使用VLC-Player测试了WebCam,因为我不确定它是否因VM而起作用。

我的解决方案是使用VideoCapture类,它将捕获的图像存储在Mat类中,因此转换为结构IplImage是必要的。 (在这里找到)

我的解决方案是:

 #include "opencv\cv.h" #include "opencv\highgui.h" #include  #include  #include  #include  #include  using namespace cv; ... void Run_with_WebCAM(){ std::string WindowName = "WebCam_Example"; VideoCapture webcam; webcam.open(0); Mat m_frame; if (webcam.isOpened()){ // create a window cvNamedWindow(WindowName.c_str(), CV_WINDOW_AUTOSIZE); IplImage* frame; while (1) { // update frame and display it: webcam >> m_frame; // convert captured frame to a IplImage frame = new IplImage(m_frame); if (!frame) break; cvShowImage(WindowName.c_str(), frame); // Do some processing... delete frame; // some abort condition... } // release memory and destroy all windows cvDestroyWindow(WindowName.c_str()); ... } } 

我经常使用

 capture = cvCreateCameraCapture( -1 ); 

让OpenCV自动检测合适的相机。

也许OpenCV不支持您的网络摄像头。 您似乎正在使用Windows系统,因此您可以尝试使用videoInput库通过DirectX访问您的网络摄像头。

更多信息: http : //aishack.in/tutorials/capturing-images-with-directx/