Cognotics Home  
Search Cognotics:
.
Home > OpenCV Resources > Seeing With OpenCV > Part 1 Pages:   Prev   1   2   3   4   Next
.
 
Part 1: Introduction to OpenCV (Continued)
This article originally appeared in SERVO Magazine, January 2007. Reprinted by permission of T & L Publications, Inc.
 

Programming with OpenCV: Some Basics

More about Headers and Libraries

Most OpenCV programs need to include cv.h and highgui.h. Later, for face recognition, we'll also include cvaux.h. The remaining header files are included by these top-level headers.

If you've left the header files in multiple directories (default installation), make sure your compiler's include path contains these directories. If you've gathered the headers into one include directory, make sure that directory's on your compiler's include path.

Your linker will need both the library path and the names of the static libraries to use. The static libraries you need to link to are cxcore.lib, cv.lib, and highgui.lib. Later, for face recognition, you'll also link to cvaux.lib. These are in OpenCV's "lib" directory.

Reading and Writing Images

Image I/O is easy with OpenCV. Figure 3, below, shows a complete program listing for reading an image from file and writing it as a second file, in a different compression format.

.  
Figure 3. Example program that reads an image from a file and writes it to a second file in a different compression format. (Click to select, then copy.)

To read an image file, simply call cvLoadImage(), passing it the filename (line 14) . OpenCV supports most common image formats, including JPEG, PNG, and BMP. You don't need to provide format information. cvLoadImage() determines file format by reading the file header.

To write an image to file, call cvSaveImage(). This function decides which file format to use from the file extension. In this example, the extension is "png," so it will write the image data in PNG format. When you're finished using the input image received from cvLoadImage(), free it by calling cvReleaseImage(), as on line 29.

Both cvLoadImage() and cvSaveImage() are in the HighGUI module.

Live Video Input

Capturing image frames from a webcam, or other digital video device, is nearly as easy as loading from file. Figure 4 shows a complete program listing to initialize frame capture, capture and store several video frames, and close the capture interface.

.  
Figure 4. Example program that captures live video frames and stores them as files. (Click to select, then copy.)

The capture interface is initialized, on line 19, by calling cvCaptureFromCAM(). This function returns a pointer to a CvCapture structure. You won't access this structure directly. Insead, you'll store the pointer to pass to cvQueryFrame().

When you're finished using video input, call cvReleaseCapture() to release video resources. As with cvReleaseImage(), you pass the address of the CvCapture pointer to cvReleaseCapture().

Don't release or otherwise modify the IplImage you receive from cvQueryFrame()! If you need to modify image data, create a copy to work with:

// Copy the video frame
IplImage * pImgToChange = cvCloneImage( pVideoFrame );

// Insert your image-processing code here...

// Free the copy after using it
cvReleaseImage( &pImgToChange );

.
Source Code for this Article CONTINUED   Prev   1   2   3   4   Next
. .
.
.
 
Source Code for this Article
 
 
Related Resources:

Download OpenCV
Official OpenCV usergroup
OpenCV Wiki

 
Articles in this series:

Part 1: Introduction to OpenCV

Part 2: Finding Faces in Images

Part 3: Follow that Face!

Part 4: Face Recognition With Eigenface

Part 5: Implementing Eigenface
 
.
bottom margin
Home | OpenCV Resources | Seeing With OpenCV