Image Overlay Using Transparency

When we need to look at two images together—for example, to compare the input and output of a particular image processing operation, or to compare different images of the same scene—overlaying one image on top of the other is often more effective than viewing the images side-by-side. Each pixel in a Handle Graphics® image object can be assigned a different level of transparency using the ‘AlphaData’ property of the image. The ‘AlphaData’ is a MxN matrix of the same size as the image with each element in the range [0 1] indicating the “opacity” of a pixel.

This article focuses on two transparency patterns that can be applied to create a wide variety of image visualizations: a checkerboard that shows portions of two images in an alternating pattern of squares, and a data-dependent pattern that uses varying shades of a single color.

Using a Checkerboard Transparency Pattern

We will use this technique to compare the input and the output of a grayscale conversion.

First, we display the input and output images together (Figure 1).

rgb = imread('peppers.png');
imshow(rgb);
I = rgb2gray(rgb);
hold on
% Save the handle for later use
h = imshow(I);
hold off

Figure 1. Grayscale output image overlaid on color input image.

Not too surprisingly, only the grayscale image is visible. We will give the grayscale image a checkerboard transparency pattern so that some pixels are opaque and others are transparent (Figure 2).

Using Image Data to Control Transparency

Our second transparency example gets a bit more creative. This technique “colors” some pixels in one image according to the values of pixels in a second image. Our example uses this method to visualize downhill water flow on top of a digital elevation model (DEM) (Figure 3).

E = imread('peppercorn_hill.png');
imshow(E, 'InitialMag', 'fit')

Figure 3. DEM of Peppercorn Hill and North Pond, Massachusetts. Bright pixels correspond to high spots in the terrain. (Original data courtesy of the U. S. Geological Survey)

First, we create an influence map, a visualization of downhill water flow, starting from the peak of Peppercorn Hill (Figure 4).

I = imread('influence_map.png');
imshow(I, 'InitialMag', 'fit')

Figure 4. Influence map showing downhill water flow.

It is difficult to interpret the influence map image outside the context of the original DEM—for example, it is hard to see exactly where the downhill flow reaches North Pond. To visualize the two images together, we do the following: