Hunt
Hunt

Reputation: 379

Unable to detect web cam in OpenCV

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

Upvotes: 1

Views: 9526

Answers (4)

roland j
roland j

Reputation: 11

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

Upvotes: 1

Utkarsh Sinha
Utkarsh Sinha

Reputation: 3305

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/

Upvotes: 0

karlphillip
karlphillip

Reputation: 93410

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

Upvotes: 3

Steven Mohr
Steven Mohr

Reputation: 1167

I usually use

capture = cvCreateCameraCapture( -1 );

to let OpenCV automaticly detect a proper camera.

Upvotes: 0

Related Questions