Page 38 -
P. 38
12 Chapter 1 ■ Practical Aspects of a Vision System
IplImage *frame = 0;
int i, n=0;
char filename[256];
char c;
Initialize the camera and check to make sure that it is working.
camera = cvCaptureFromCAM( CV_CAP_ANY );
if( !camera ) // Get a camera?
{
fprintf(stderr, “Can’t initialize camera\n“);
return -1;
}
Open a window for image display.
cvNamedWindow(“video“, CV_WINDOW_AUTOSIZE);
cvMoveWindow (“video“, 150, 200);
This program will capture 600 frames. At video rates of 30 FPS, this would
be 20 seconds, although cameras do vary on this.
for(i=0; i<600; i++)
{
frame = cvQueryFrame( camera ); // Get one frame.
if( !frame )
{
fprintf(stderr, “Capture failed.\n“);
}
The following creates a short pause between frames. Without it, the images
come in too fast, and in many cases nothing is displayed. cvWaitKey waits for
a key press or for the time specified — in this case, 100 milliseconds.
c = cvWaitKey(100);
Display the image we just captured in the window.
// Display the current frame.
cvShowImage(“video“, frame);
If cvWaitKey actually caught a key press, this means that the image is to be
saved. If so, the character returned will be >0. Save it as a file in the AIPCV
directory.
if (c>0)
{
sprintf(filename, “C:/AIPCV/VideoFrame%d.jpg“, n++);
if( !cvSaveImage(filename, frame) )