Page 31 -
P. 31
Chapter 1 ■ Practical Aspects of a Vision System 5
Figure 1.1: Sample digital image for use in this chapter. It is an image of a tree in Chico,
CA, and was acquired using an HP Photosmart M637 camera. This is typical of a modern,
medium-quality camera.
How can an individual pixel be accessed? The field widthStep is the size of
a row, so the start of image row i would be found at
img->imageData + i*img->widthStep
Column j is j pixels along from this location; if pixels are bytes, then that’s
img->imageData + i*img->widthStep + j
If pixels are RGB values, as in the JPEG image read in above, then each pixel
is 3 bytes long and pixel j starts at location
img->imageData + i*img->widthStep + j*3
Thevalue of thefield nChannels is essentially the number of bytes per pixel,
so the pixel location can be generalized as:
img->imageData + i*img->widthStep))[j*img->nChannels]
Finally, the color components are in the order blue, green, and red. Thus,
thebluevalue forpixel [i,j]isfound at
(img->imageData + i*img->widthStep)[j*img->nChannels + 0]
and green and red at the following, respectively:
(img->imageData + i*img->widthStep)[j*img->nChannels + 1]
(img->imageData + i*img->widthStep)[j*img->nChannels + 2]
The data type for a pixel will be unsigned character (or uchar).
There is a generic way to access pixels in an image that automatically uses
what is known about the image and its format and returns or modifies a
specified pixel. This is quite handy, because pixels can be bytes, RGB, float, or