Fanning Software Consulting

Creating a Transparent Color Overlay Image

QUESTION: I'd like to know how to display color overlays on medical images. For example, an overlay of blood flow rate superimposed on gray-scale images of the brain. I'm not able to do this currently without destroying the information in the original image. What I would like is to make the color overlay transparent, or at least semi-transparent, to some degree. How can I do this in IDL?

ANSWER: In direct graphics, the simplest way to do this is to use the TRANSPARENT keyword with the Coyote Graphics image command cgImage.

Here, for example, are the commands I used to create the three displays shown in the figure below with a CAT scan image and an PET image.

   cgDisplay, 350, 350, /Free, Title='CT Image'
   cgImage, ct, CIndex=0
   cgDisplay, 350, 350, /Free, Title='PET Image'
   cgImage, pet, CIndex=0
   cgDisplay, 350, 350, /Free, Title='Fusion Image'
   cgImage, ct, CIndex=0
   cgImage, pet, CIndex=3, Transparent=50, Missing_Value=0
Color image overlay of a PET image on top of a CT image.
Color image overlay of a PET image on top of a CT image.
 

Color Transparency with Object Graphic Images

Another method I've used successfully is to use the alpha blending channel that is available with object graphic images to "combine" the colors of the two images I am trying to overlay. The trick here is to realize (the documentation is a little unclear on this point) that the foreground image must be a 24-bit color image if you want the image colors to show through. The background image can be either an 8-bit (2D) image with a palette, as in the example here, or a 24-bit (3D) image with the colors specified directly.

A blending function value of [3,4] will provide a nice see-through sort of look for the two images. The code for my foreground or alpha image will look like this:

   alphaImage = Obj_New('IDLgrImage', image24, $
      Dimensions=[400,400], Interleave=0,  Blend_Func=[3,4])

The example code is named Image_Blend. You can call it without parameters and it will use two of the images in the IDL distribution. Or, you can pass the program your own 2D images (they can be different sizes if you like):

   IDL> Image_Blend, backgroundImage, foregroundImage

The foreground image will be turned into a 24-bit image automatically by the program, using the color table you specify with the ColorTable keyword. (Color table 3, the Red Temperature color table will be used if you don't specify a preference.)

Pixels in the foreground image with a value of zero will all be set to totally transparent. Any other non-zero pixel will have its alpha channel value initially set to 128. This will make these pixels semi-transparent. A slider bar will allow you to change this value interactively. As you do, notice how the transparency of the foreground image changes.

Here is an example of how the program looks when it is running:

Example of gray-scale
image with transparent color overlay on top.
Example of gray-scale image with transparent color overlay on top.
 

Color Transparency with 24-Bit Direct Graphic Images

If you have a 24-bit color display, you can do your own alpha blending in direct graphics. Mike Miller on the IDL newsgroup provided this formula:

   RGB = BYTE( alpha * FLOAT(baseRGB) + (1.0 - alpha) * FLOAT(overlayRGB) )

Where baseRGB and overlayRGB are 24-bit images and alpha is a number between 0 and 1 indicating the percentage of alpha blending desired. For example, a value of 0.8 will give 80 percent of the weight to the background image pixels and 20 percent to the foreground image pixels. If you have downloaded the programs from the Coyote Library, here is a example you can try:

   Window, Title='Direct Graphics Alpha Blending', XSIZE=400, YSIZE=400
   LoadCT, 5
   cgImage, cgDemoData(7)
   background = cgSnapshot()
   LoadCT, 0
   cgImage, (cgDemoData(5) GT 40) * 255B
   foreground = cgSnapshot()
   alpha = 0.5
   rgb = BYTE( alpha * FLOAT(background) + (1.0 - alpha) * FLOAT(foreground) )
   cgImage, rgb

You can see the result in the figure below.

Example of gray-scale
image with transparent color overlay on top in direct graphics.
Example of gray-scale image with transparent color overlay on top in direct graphics.
 

You will need the Coyote Library programs cgImage, cgSnapshot, and cgDemoData to run the code above.

I have written a small program named cgBlendImage that can be used to blend or fuse any two 24-bit images in this way. You can use the ALPHA keyword to select the amount of alpha blending. The figure below shows two MODIS images of a region of the Arctic. The pink color indicates clouds, the slate gray color indicates ice, and land is indicated by the brown color. The foreground and background images, both 8-bit images, were prepared by displaying them in a IDL graphics window with the appropriate color tables, and then using cgSnapshot() to read them out of the graphics windows into 24-bit color image variables that I named "radiance" and "features", respectively. With these two images, I created the figure below with this code.

   Window, XSIZE=500, YSIZE=1500
   !P.Multi = [0,1,3]
   cgImage, radiance
   cgImage, features
   cgBlendImage, radiance, features, ALPHA=0.75
   !P.Multi = 0
An example of a blended image.
Top image is the blended image background, middle image is the blended image foreground,
and the bottom image is the blended image, displayed with the cgBlendImage command.
 

cgBlendImage is a wrapper for the cgImage command and accepts any cgImage keyword.

Written: 8 January 2006
Last Updated: 18 October 2012