tags:

views:

73

answers:

3

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

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

karlphillip
well i am new to OpenCV , so i dont know where to take care of errors , but i tried cvCaptureFromCAM(0) , cvCaptureFromCAM(-1) ,cvCreateCameraCapture(0) , cvCreateCameraCapture(-1) nothing works for me
Hunt
I Just add these lines to my code and it is working now if(capture != NULL) printf("Working ") ; else printf("Not working ") ;
Hunt
Great! Don't forget to vote UP on my answer if it helped you, or even accept it as the official answer to your question so it can help others in the future. Thank you!
karlphillip
A: 

I usually use

capture = cvCreateCameraCapture( -1 );

to let OpenCV automaticly detect a proper camera.

Steven Mohr
i have tried using -1 but the same problem occur
Hunt
A: 

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://www.aishack.in/2010/03/capturing-images-with-directx/

Utkarsh Sinha