FIGURE 3. Example
program that reads an
image from a file and
writes it to a second
file in a different
compression format.
*.hpp. There will be
lots of matches. You
don’t need all of
them. Headers for all
modules except
HighGUI are in
separate “include”
directories inside each
module. You can skip
headers in the
“src” directories for
these modules. For
HighGUI, you’ll need
highgui.h, located in
otherlibs/highgui.
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.
OpenCV modules. Although you don’t
need to do this, I like to gather them
together into a single include directory.
On both Linux and Windows, you
can locate the headers by searching the
install directory and subdirectories for
filenames that match the pattern *.h,
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 is on
your compiler’s
include path.
Your linker
will need both
Reading and Writing Images
Image I/O is easy with OpenCV.
Figure 3 shows a complete program
listing for reading an image from
file and writing it as a second file, in a
different compression format.
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.
Both cvLoadImage() and
cvSaveImage() are in the HighGUI
module.
When you’re finished using the input
image received from cvLoadImage(), free
it by calling cvReleaseImage(), as on line
29. This function takes an address of a
pointer as its input because it does a
“safe release.” It frees the image structure only if it’s non-null. After freeing it, it
sets the image pointer to 0.
FIGURE 4. Example
program that
captures live video
frames and stores
them as files.
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.
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. Instead, 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
64 SERVO 01.2007