Image Overlay Example
From MATLAB Techniques for Image Processing by Steve Eddins.
It's often useful to modify an image so that a set of pixels is displayed using a certain color.
For example, suppose you started with this image:
imshow george.png

And you processed the image to produce this binary result:
imshow thinned.png

To help evaluate the result, you would like to turn each pixel in george.png that corresponds to a foregound pixel in thinned.png yellow, like this:
imshow yellow_overlay.png

Obviously, the output image can't be grayscale, because we want some of the pixels to be yellow.
Here's one good procedure that produces a truecolor output image:
- Copy the gray-scale image array to three arrays, representing the red, green, and blue components of the output image.
- Modify the pixels in the component arrays as desired.
- Concatenate the component arrays in the third dimension to make the output image.
Let's use this procedure for the images above. First, create the red, green, and blue component arrays.
I = imread('george.png');
red = I;
green = I;
blue = I;
Next, modify the pixels of the component arrays to produce a yellow color corresponding to the binary image. We do this by setting the corresponding pixels in the red and green components to 255.
mask = imread('thinned.png');
red(mask) = 255;
green(mask) = 255;
Finally, concatenate the component arrays in the third dimension to produce the output truecolor image.
overlay_image = cat(3,red,green,blue); imshow(overlay_image);

See my imoverlay function on the MATLAB Central File Exchange.