OpenCV : Accessing the Webcam

Here is a Sample program to access the webcam.


Download the Project folder and the source code.

//______________________________________________________________________________________
// OpenCV Webcam access
// Author: Bharath Prabhuswamy
//______________________________________________________________________________________
//______________________________________________________________________________________

#include "cv.h"
#include "highgui.h"
#include <stdio.h>

int main()
{
    CvCapture *capture = 0;
    IplImage  *img = 0;
    char key = 0;

    /* Initialize the webcam */
    capture = cvCaptureFromCAM( 0 );
 
 /* Always check if the program can find a device */
    if ( !capture ) 
        return -1;

   cvNamedWindow( "Webcam",CV_WINDOW_AUTOSIZE);

   while((char) key != 27 )
   {

  /* Obtain a frame from the device */
        img = cvQueryFrame( capture );
  
  /* Always check if the device returns a frame */
        if( !img ) 
   return -1;

  cvShowImage( "Webcam",img);

  /* Quit execution if 'ESC' key is pressed */
  key = cvWaitKey(1);
 }

 /* Clean up memory */
    cvDestroyWindow( "Webcam" );
    cvReleaseCapture( &capture );
    return 0;
}

No comments:

Post a Comment