Features


Image Processing Part 12: Boolean and Overlay Operations

Dwayne Phillips


The author works as a computer and electronics engineer with the U.S. Department of Defense. He has a PhD in Electrical and Computer Engineering from Louisiana State University. His interest include computer vision, artificial intelligence, software engineering, and programming languages.

Introduction to the Series of Articles

This article is twelvth in a series on images and image processing. Previous articles discussed reading, writing, displaying, and printing images (TIFF format), histograms, edge detection, spatial frequency filtering, sundry image operations, image segmentation, and working with shapes. (For an overview of this series, see the sidebar "Navigating the Phillips Series.")

This article will discuss Boolean and overlay operations. Boolean operations performed on single images can produce a variety of special effects. In addition, Boolean and overlay operations are useful for combining images in interesting ways. The Boolean operations apply the functions of Boolean algebra to the pixels of one or more images to produce an output image. The overlay operations "lay" select pixels from one image on top of another image, obscuring the pixels underneath. These operations are similar to the image addition and subtraction operations presented in [1].

Boolean Operations

The Boolean operations execute the basic functions from Boolean algebra, and extend these functions to handle gray-scale images. Figure 1 shows the truth table for these operations as applied to strictly binary (black-and-white) images. In this case the columns marked A and B show pixel values from two input images (call the images A and B), the output image pixel values corresponding to each Boolean operation are shown in the remaining columns.

It is a simple matter to extend Boolean operations for gray scale images. The truth table for gray-scale operations is shown in Figure 2. The 1s under the A and B columns have been replaced with variables a and b, respectively. a and b represent non-zero pixel values in images A and B. In the first three output columns, all 1s have been replaced by a. This portion of the truth table represents a form of Boolean operations which selectively mask (replace with 0) or pass pixel values from image A to the output. These Boolean operations (AND, OR, XOR) interpret all non-zero input values as 1s, perform the standard Boolean functions on the inputs, and pass a to the output if the result is 1. If the result is 0, the operations pass a 0 to the output. In the last three output columns, all 1s have been replaced with value. value represents a parameter passed to the Boolean operations NAND, NOR, and NOT. Like the preceding Boolean operations, these operations interpret all non-zero input values as 1s, and perform the standard Boolean functions on the inputs. However, instead of passing a to the output if the result is 1, these operations pass value. If the result is 0, the operations pass a 0 to the output. (Note that these Boolean operations are somewhat different than the kind commonly applied in fuzzy logic applications, in which A AND B = min(A, B), and A OR B = max(A, B).)

Listing 1 shows the subroutines that implement the Boolean operations. Each of the routines (and_image, or_image, xor_image, nand_image, nor_image, not_image) follows the same pattern. These routines create an output image file if needed, read the two input images, combine them using the truth table, and write the result.

Before applying the Boolean or overlay operations, all input images must occupy the same light-dark domain. Some image scanners save images from dark to light (dark=0 light=255), while others do the opposite (dark=255 light=0). CIPS uses the former convention. If you combine images of the opposite polarity, the results will be messy. For this reason, I've included an invert program with this article, which is available on this month's code disk, and from the CUJ online sources.

An Application of Boolean Operations

One application that lends itself to Boollean operations is labeling images. This process requires several steps. First, you must create an image of the label itself. You can use the program ilabel (a portion of this program is shown in Listing 2) to create a label image. This program writes simple 9x7 block letters to an image file. You call the program by giving an output image name, a line number in the image, and the text to go in the image. For example, typing

ilabel a.tif 10 adam
at the command prompt places the letters "ADAM" in the image file a.tif starting in the tenth row. (Most of the listing consists of arrays defining the block letters, numbers, and a few punctuation marks.)

The left side of the image in photograph 1 shows the sample output of the ilabel program. The words "ADAM PHILLIPS" are clear enough, but they would disappear if they were placed on top of an image. Therefore, the next step called for in the labeling process is to create a background for them. You can create a suitable background by combining the XOR operation with a process known as dilation. The center section of photograph 1 shows the result of dilating the words (for an explanation of dilation see [2]). The right side of photograph 1 shows the final label — black letters on a white background. The final label results from exclusive-ORing (XOR) the letters and their dilation. The output of the XOR is white only where one or the other image is white, but not both.

Finally, Photograph 2 shows the result of a labeling operation. This image results from OR-ing the final label of photograph 1 with the boy image. OR-ing allows you to see through the label to the image underneath.

This is only one possible use of the Boolean operations. There are many more, especially when you start combining them. (After all, combining Boolean operations is how people build computers.)

Overlay Operations

The overlay operations are so called because they appear to place portions of one image on top of another. In reality, the overlay operations replace some of one image's pixels with another's, and write the result into a third image. There are at least five ways to overlay pixels from an image A onto an image B: (1) overlay non-zero pixels from A onto B, (2) overlay zero-value pixels from A onto B, (3) overlay pixels from A onto B if their gray-scale values are greater, (4) overlay pixels from A onto B if their gray-scale values are less, and (5) average the pixels from A and B and use this as the output.

Figure 3 illustrates the last of these operations. It shows two image arrays: A and B. The bottom array shows the result of averaging images A and B. While this image does not look like much but a bunch of numbers, the photographs discussed below will make the averaging operation more meaningful.

The average overlay is useful for applying a texture to an image. Photograph 3 shows a leafy texture, and photograph 4 shows a house. Photograph 5 shows the result of averaging the two. It is easy to recognize the house, but parts of it (notably the roof and door) have a superimposed texture.

Listing 3 shows the subroutine that implements this overlay operation. Additional routines (non_zero_overlay, zero_overlay, greater_overlay, less_overlay) all follow the same sequence. They create the output image file if needed, read the two input images, combine them, and write the result.

Integrating the Operations

I have integrated the Boolean and overlay operations into my C Image Processing System (CIPS). I have also created two stand-alone application programs that apply the new operators to entire images. Listing 4 shows the boolean program that calls the Boolean subroutines. Like all stand-alone programs, boolean interprets the command line, sets up counters, and calls the desired operator as it loops through the input image.

This program handles several special cases. First, the NOT operator processes only one input image (versus the usual two) and produces one output image. Thus, all command-line parameters for NOT move up by one place in the command line. If you use the operator NAND or NOR, you must specify the output value. Unlike AND, OR, and XOR, NAND and NOR do not obtain their output values from the first input image.

The most noteworthy special case for listing 4 occurs when the first input image is only one ROWSxCOLS array in size. (All CIPS functions operate on image blocks of size ROWSxCOLS, which I have defined as 100x100 pixels. CIPS can also handle larger images composed of multiple blocks, each of size ROWSxCOLS.) Normally, the two input images are the same size and the program loops through them performing the Boolean operation using the corresponding image arrays from each. In many instances, however, you may want to apply the Boolean and overlay operators to just one ROWSxCOLS block. The labeling example shown earlier is one such case. The labeling example OR-ed the single ROWSxCOLS array of the label image with one ROWSxCOLS section of the boy image. In situations like this, you specify the line and column of the second input image with which you combine the first input image.

The if length == 1 and if width == 1 sections of code handle this special case. These code segments read in the extra parameters (il2 and ie2) and set the other line and element parameters. Note, the program does not create the output image to match the second input image. You need to do that before running the program (DOS command: copy second.tif output.tit). You could also put that feature into the program as homework.

The mainover program (not shown) calls the overlay subroutines. mainover interprets the command line, sets up counters, and calls the desired operator as it loops through the input image. It includes the if length == 1 and if width == 1 sections of code to handle the same special case as the boolean program. As is true for all the software referenced in this article, this program is available on code disk.

Conclusions

This installment of the series has discussed Boolean and overlay operations. Though not complicated, these operations allow you to combine images in interesting and creative ways. There are endless possibilities to the way these operations can be combined. As with all the other operators in this series, you should experiment. Use the operators as building blocks and mix them to fit your needs.

References:

[1] D. Phillips, "Image Processing, Part 8: Image Operations," The C Users Journal, November, 1992.

[2] D. Phillips, "Image Processing, Part 11: Working with Shapes," The C Users Journal, August 1993.

Sidebar: "Ordering Information"