Transparent Overlays Example

From MATLAB Techniques for Image Processing by Steve Eddins.

Transparent graphics objects can be used effectively to visualize image processing concepts. Two particularly useful techniques are:

Our first example uses transparent patches based on the 'Extrema' measurement returned by regionprops. The extrema for a given object are eight points: the left-most pixel on the bottom, the right-most pixel on the bottom, the top-most pixel on the right, the bottom-most pixel on the right, and so on.

We'll start with the rice image and try to segment it.

I = imread('rice.png');
imshow(I)

Even out the illumination with the tophat operator, threshold, and then clean up the thresholded image a bit.

I2 = imtophat(I,ones(15, 15));
bw = im2bw(I2,graythresh(I2));
bw2 = bwareaopen(bw,5);
bw3 = imclearborder(bw2);
imshow(bw3)

Label the binary objects and compute the extrema.

bw = imread('rice_binary.png');
L = bwlabel(bw);
s = regionprops(L,'Extrema');
s(1).Extrema
ans =

   11.5000   86.5000
   12.5000   86.5000
   34.5000  100.5000
   34.5000  102.5000
   33.5000  103.5000
   27.5000  103.5000
    9.5000   89.5000
    9.5000   87.5000

Superimpose the extrema-bounded shapes on top of the original rice image.

imshow(I)

hold on
for k = 1:numel(s)
    x = s(k).Extrema(:,1);
    y = s(k).Extrema(:,2);
    patch(x,y,'g')
end
hold off

The above visualization is pretty clear. If you zoom in on some of the odd, larger shapes, though, you can't really tell what's going on. We solve that by displaying the patches transparently.

imshow(I)

hold on
for k = 1:numel(s)
    x = s(k).Extrema(:,1);
    y = s(k).Extrema(:,2);
    patch(x,y,'g','FaceAlpha',0.3)
end
hold off

This example displays Handle Graphics image objects transparently. In fact, each individual pixel can be assigned a different level of transparency. This can be used in various ways to view one image on top of another. Here we'll use a "checkerboard" transparency pattern to view a gray-scale image on top of the original color image.

First, display the color image and the gray-scale image together, in the same place.

rgb = imread('peppers.png');
clf
imshow(rgb);
I = rgb2gray(rgb);
hold on
h = imshow(I); % Save the handle; we'll need it later
hold off

Not too surprisingly, only the gray-scale is visible. That's because it's "covering up" the color image. Let's give it a "checkerboard" transparency pattern, so that some of the pixels are fully opaque, and others are fully transparent.

[M,N] = size(I);
block_size = 50;
P = ceil(M/block_size);
Q = ceil(N/block_size);
alpha_data = checkerboard(block_size,P,Q) > 0;
alpha_data = alpha_data(1:M,1:N);
set(h,'AlphaData',alpha_data);

Now we can see some of both images. This visualization technique is often used to evaluate image fusion algorithms.

Our last transparency example gets a bit more creative. We'll be displaying a solid color "image," while the data we're really interested in is used to vary the transparency of the solid color.

Here's a digital elevation model (DEM) of Peppercorn Hill and North Pond in Massachusetts.

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

The bright blob at the upper left is Peppercorn Hill, and the flat, dark plateau in the upper middle is North Pond.

Below is an "influence map." This is a visualization of water flow, starting from the peak of Peppercorn Hill.

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

It's difficult to interpret the influence map image on its own, apart from the original DEM. Let's visualize the two images together as follows:

  1. Display the original DEM image.
  2. Display a solid green "image" on top of the original image.
  3. Use the influence map pixels to control the transparency of each pixel of the green image.
imshow(E,'InitialMag','fit')

% Make a truecolor all-green image.
green = cat(3,zeros(size(E)),ones(size(E)),zeros(size(E)));
hold on
h = imshow(green);
hold off
% Use our influence map image as the AlphaData for the solid
% green image.
set(h,'AlphaData',I)