Page 37 -
P. 37

Chapter 1 ■ Practical Aspects of a Vision System  11


                               robotics, games, biometrics, and places where computers are easily available
                               and very high quality is not essential.
                                 There are a great many types of webcam, and the details of how they work
                               are not relevant to this discussion. If a webcam is properly installed, then
                               OpenCV should be able to detect it, and the capture functions should be able
                               to acquire images from it. The scheme used by OpenCV is to first declare and
                               initialize a camera, using a handle created by the system. Assuming that this
                               is successful, images can be captured through the handle.
                                 Initializing a camera uses the cvCaptureFromCAM function:

                                           CvCapture  *camera = 0;
                                           camera = cvCaptureFromCAM( CV_CAP_ANY );
                                           if( !camera )          error ...

                                 The type CvCapture is internal, and represents the handle used to capture
                               images. The function cvCaptureFromCam initializes capturing a video from a
                               camera, which is specified using the single parameter. CV_CAP_ANY will allow
                               any connected camera to be used, but the system will choose which one. If
                               0 is returned, then no camera was seen, and image capture is not possible;
                               otherwise, the camera’s handle is returned and is needed to grab images.
                                 A frame (image) can be captured using the cvQueryFrame function:

                                               IplImage  *frame = 0;
                                               frame = cvQueryFrame( camera );
                                 The image returned is an IplImage pointer, which can be used immediately.
                                 When the program is complete, it is always a good idea to free any resources
                               allocated. In this case, that means releasing the camera, as follows:

                                               cvReleaseCapture( &camera );

                                 It is now possible to write a program that drives the webcam. Let’s have
                               the images displayed in a window so that the live video can be seen. When a
                               key is pressed, the program will save the current image in a JPEG file named
                               VideoFramexx.jpg,where xx is a number that increases each time.


                                 // Capture.c - image capture from a webcam
                                 #include “stdafx.h“
                                 #include “stdio.h“
                                 #include “string.h“
                                 #include “cv.h“
                                 #include “highgui.h“

                                 int main(int argc, char ** argv)
                                 {
                                   CvCapture  *camera = 0;
   32   33   34   35   36   37   38   39   40   41   42