Unable to detect web cam in OpenCV

天大地大妈咪最大 提交于 2019-12-13 00:38:42

问题


Hi I am trying to detect web cam in opencv using following code I am getting blank black screen though my web cam is attached to my PC via usb

My web cam is using **ICatch(VI) PC Camera ** driver & I am using OpenCV 2.1 with VS 2008

#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" );
}

回答1:


Ok, first... does your webcam work with other webcam applications?

Your code is a little messed up! You create a window named Example2_9 , but you try to draw with cvShowImage() to another window (named cam) that doesn't exist! Fix that! Replace the occurrences of cam by Example2_9.

IF that doesn't solve the problem, I would probably replace the beginning of main() by this:

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 );

You code lacks error checking in several places, be careful. One of the functions could be returning an error and you'll never know until you do the proper check.

You can also find a bunch of other OpenCV examples on Google that calls cvCaptureFromCAM() instead of cvCreateCameraCapture(). If the above suggestions doesn't work, try it!

One more thing, on my Macbook Pro I have to use cvCaptureFromCAM(0) for the application to work. On Linux, I always use cvCaptureFromCAM(-1).




回答2:


I faced the same issue trying the example 2-9 of the book LearningOpenCV.

I am coding with VS13 Ultimate on Win7-Prof in a VM; The WebCam from the Host-PC is a BisonCam, NB Pro; I tried different variants of the cvCreateCameraCapture, which always returned NULL; I even tested the WebCam with the VLC-Player successfully, cause I wasn't sure if it works due to the VM.

My solution is to make use of the class VideoCapture, which stores the captured image in the class Mat, so a convertion to the structure IplImage is necessary. (found here)

My solution is:

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

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());
       ...
    }
 }



回答3:


I usually use

capture = cvCreateCameraCapture( -1 );

to let OpenCV automaticly detect a proper camera.




回答4:


Maybe OpenCV doesn't support your webcam. It seems you're working on a Windows system, so you can try using the videoInput library to get access to your webcam through DirectX.

More info: http://aishack.in/tutorials/capturing-images-with-directx/



来源:https://stackoverflow.com/questions/3325556/unable-to-detect-web-cam-in-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!