Thursday, October 04, 2007

Capturing a Image from a Web cam

This is a small C++ code sample that allows a image to be captured using a web cam. OpenCV library is used and it provides easy way to capture the image.

int captureImageNew(){
CvSize imgSize;
IplImage* image= 0;
imgSize.width = 320;
imgSize.height = 240;
//Initializing capture from the web cam
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture )
{
return -1;
}

// Create a window which present the captured image
cvNamedWindow( "capture", CV_WINDOW_AUTOSIZE );

// Show the image captured from the cam in the window and repeat until ESC is pressed
for(;;)
{
// Get one Image
image= cvQueryFrame( capture );
if( !image)
{
break;
}
cvSaveImage("C:\\ll.JPG",image);
cvShowImage( "capture", image);

//If ESC key pressed exit
if( (cvWaitKey(10) & 255) == 27 ) break;
}

cvReleaseCapture( &capture );
cvDestroyWindow( "capture" );
return 0;
}