
Chapter 23
Image Processing
23.1 Structure for Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
23.2 Processing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
23.2.1 Image Pixels and Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
23.2.2 Processing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
23.2.3 Applying a Color Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
23.2.4 Inverting the Image Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
23.2.5 Edge Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
23.2.6 Color Equalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
The rapid growth of digital photography is one of the most important technological changes
in the past fifteen years. Cameras are now standard on mobile phones, tablets, and laptops.
There is also a proliferation of webcams and surveillance cameras. All of these digital images
call for clever applications to improve our lives. For example, social media websites use facial
recognition in order to make it easier to see photos of your friends. Future applications may
be able to determine what people are doing in images, or sequences of images.
This chapter introduces some basics of image processing. The main goal of this chapter
is to explain how to read and write images, as well as how to modify the colors in the
image pixels. For simplicity, this chapter considers only one image format: bitmap (BMP).
BMP files are not normally compressed and the pixels are independently stored. A more
commonly used format is called Joint Photographic Experts Group, also known as JPEG.
JPEG files are compressed using the discrete cosine transform (DCT). This compression
algorithm is beyond the scope of this book.
23.1 Structure for Image
An image includes many pixels. Each pixel is a dot in the image and it has one single
color. The colors of the pixels are called the “data” of the image. In addition to the colors,
an image has additional information about the image. For example, if it is a photograph,
the file may have the date when the photo was taken, the brand of the camera, etc. The
additional information is separate from the pixel colors, but describes something that may
be interesting about the pixels. This additional information is called “metadata”. When
taking a photograph with a digital camera, the camera records a wide range of metadata.
Fig. 23.1 shows the metadata of a photograph taken by a Nikon Coolpix S3500 camera. A
different camera model or a different brand may produce different types of metadata.
A bitmap image file has two parts. The first part is the metadata (also called header).
The second part is the data. The header has 54 bytes in length and the size of the data
depends on the number of pixels.
The header is defined as:
381