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