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);
ers and other high-level constructs. All pixel-level calculations
are performed inside OpenCV functions. However, if you
write your own image-processing algorithms, you may need
to access raw pixel values. Here are two ways to do that:
1. Simple Pixel Access
The easiest way to read individual pixels is with the
cvGet2D() function:
CvScalar cvGet2D(const CvArr*,
int row, int col);
Color Conversions
Figure 5 shows code for converting a color image to
grayscale. OpenCV has built-in support for converting to and
from many useful color models, including RGB, HSV, YCrCb,
and CIELAB. (For a discussion of color models, see “The
World of Color,” SERVO Magazine, November 2005.)
Note that the conversion function, cvCvtColor(), requires
two images in its input list. The first one, pRGBImg, is the
source image. The second, pGrayImg, is the destination image.
It will contain the conversion result when cvCvtColor() returns.
Because this paradigm of passing source and destination
images to a processing function is common in OpenCV, you’ll
frequently need to create a destination image. On line 25, a
call to cvCreateImage() creates an image the same size as the
original, with uninitialized pixel data.
This function takes three parameters: a pointer to a data container (CVArr*), and array indices for row and column location.
The data container can be an IplImage structure. The topmost
row of pixels is row=0, and the bottommost is row=height-1.
The cvGet2D()function returns a C structure, CvScalar,
defined as
typedef struct CvScalar
{
double val[4];
}
CvScalar;
How OpenCV Stores Images
OpenCV stores images as a C structure, IplImage. IPL
stands for Image Processing Library, a legacy from the
original OpenCV versions that required this product.
The IplImage datatype is defined in CXCORE. In addition to raw pixel data, it contains a number of descriptive
fields, collectively called the Image Header. These include
The pixel values for each channel are in val[i]. For
grayscale images, val[0] contains pixel brightness. The other
three values are set to 0. For a three-channel, BGR image,
blue=val[0], green=val[1], and red=val[ 2].
The complementary function, cvSet2D(), allows you to
modify pixel values. It’s defined as
• Width — Image width in pixels
• Height — Image height in pixels
• Depth — One of several predefined constants that
indicate the number of bits per pixel per channel. For
example, if depth=IPL_DEPTH_8U, data for each pixel
channel are stored as eight-bit, unsigned values.
• nChannels — The number of data channels (from one to
four). Each channel contains one type of pixel data. For example, RGB images have three channels – red, green, and blue
intensities. (These are sometimes called BGR images, because
pixel data are stored as blue, green, then red values.)
Grayscale images contain only one channel – pixel brightness.
Accessing Pixel Values
It’s possible to create many types of functionality using
OpenCV without directly accessing raw pixel data. For
example, the face detection, tracking, and recognition programs described later in this
series never manipulate raw
pixel data directly. Instead,
they work with image point-
FIGURE 5. Example
program for converting a
color image to grayscale.
SERVO 01.2007 65