Defining Transparency in Image Objects

In Object Graphics, a transparent image can be created by adding an alpha channel to the image array or by setting the ALPHA_CHANNEL property. The alpha channel is used to define the level of transparency in an image object. If you have an image containing both alpha channel data and a value for the ALPHA_CHANNEL property, the alpha values are combined by multiplying each image pixel alpha value by the ALPHA_CHANNEL property value.

If your image data includes an alpha channel, or if you set the ALPHA_CHANNEL property, use the BLEND_FUNCTION property of the image object to control how the alpha channel values will be interpreted. (See BLEND_FUNCTION property of IDLgrImage for details on how the blending is calculated.) This is known as alpha blending. For example, setting BLEND_FUNCTION = [3, 4] creates an image in which you can see through the foreground image to the background to the extent defined by the alpha channel values of the foreground image.

Transparency and Image Warping

Creating a transparent image is useful in the warping process when you want to overlay a transparency of the warped image onto the reference image (the image in which Xo, Yo control points were selected). See Warping Image Objects for an example that uses transparent image objects.

For background information on warping images and selecting control points, see Overview of Warping Images (Image Processing in IDL).

Image Transparency Examples

See the following topics for examples of creating transparent image objects:

Example: Applying a Transparent Image Overlay

The following example reads in two medical images, a computed tomography (CT) file that contains structural information, and a PET (positron emission tomography) file that contains metabolic data. A color table is applied to the PET file, and the transparency is set using the ALPHA_CHANNEL property. The PET image object is then overlaid on top of the base CT image. This is done by adding the transparent PET image to the model after (and therefore displayed in front of) the base CT image.

Example Code
See alphaimage_obj_doc.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering alphaimage_obj_doc at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT alphaimage_obj_doc.pro.

To replicate this example, create a new .pro file and complete the following steps:

  1. Load CT and PET images and get the image dimensions.
  2. file_pt = FILEPATH('head_pt.dcm', $ 
       SUBDIRECTORY=['examples', 'data']) 
    file_ct = FILEPATH('head_ct.dcm', $ 
       SUBDIRECTORY=['examples', 'data']) 
    img_pt = READ_DICOM(file_pt) 
    img_ct = READ_DICOM(file_ct) 
    dims_ct = SIZE(img_ct, /DIMENSIONS) 
    dims_pt = SIZE(img_pt, /DIMENSIONS) 
    
  3. Check for dimension equality and resize if different.
  4. IF dims_pt[0] NE dims_ct[0] THEN BEGIN 
       x = dims_ct[0]/dims_pt[0] 
       img_pt = REBIN(img_pt, dims_pt[0]*x, dims_pt[1]*x) 
       dims_pt = x*dims_pt 
       If dims_pt[0] NE dims_ct[0] THEN BEGIN 
          status = DIALOG_MESSAGE ('Incompatible images', /ERROR) 
       ENDIF 
    ENDIF 
    
  5. Change the data to byte type before creating the base CT image.
  6. img_ct = BYTSCL(img_ct) 
    oImageCT = OBJ_NEW('IDLgrImage', img_ct) 
    
  7. Create display objects and display the CT image.
  8. oWindow = OBJ_NEW('IDLgrWindow', RETAIN=2, $ 
       DIMENSIONS=[dims_ct[0], dims_ct[1]], TITLE='CT Image') 
    oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT=[0., 0., $ 
       dims_ct[0], dims_ct[1]]) 
    oModel = OBJ_NEW('IDLgrModel') 
    oModel->Add, oImageCT 
    oView->Add, oModel 
    oWindow->Draw, oView 
    
  9. Create a palette object and load the red-temperature table.
  10. oPalette = OBJ_NEW('IDLgrPalette') 
    oPalette->Loadct, 3 
    
  11. Change the data type to byte and create the PET image object. Set the BLEND_FUNCTION and ALPHA_CHANNEL properties to support image transparency.
  12. img_pt = BYTSCL(img_pt) 
    oImagePT = OBJ_NEW('IDLgrImage', img_pt, $ 
       PALETTE=oPalette, BLEND_FUNCTION=[3,4], $ 
       ALPHA_CHANNEL=0.50) 
    
  13. Create a second window, add the semi-transparent image to the model containing the original image and display the overlay.
  14. oWindow2 = OBJ_NEW('IDLgrWindow', RETAIN=2, $ 
       DIMENSIONS=[dims_pt[0], dims_pt[1]],  $ 
       LOCATION=[dims_ct[0]+10, 0], TITLE='CT/PET Transparency') 
    oModel -> Add, oImagePT 
    oWindow2 -> Draw, oView 
    
  15. Clean-up object references.
  16. OBJ_DESTROY, [oView, oImageCT, oImagePT] 
    

The results of this example are shown in the following figure.

Figure 4-9: CT Image (Left) and CT with Semi-transparent PET Overlay (Right)

Defining_Transparency_in_Image_Objects-17.jpg

Example: Cumulative Alpha Blending

The following example shows the additive effects of displaying an image object with alpha channel data and an image with an ALPHA_CHANNEL property setting. In this example, the alpha channel is used to mask out values, and the ALPHA_CHANNEL property is used to control the object transparency. However, it is easy to modify the code and investigate the relationship between setting image transparency using the alpha channel data and ALPHA_CHANNEL property. For example, defining 50% transparency for each results in 25% opacity overall.

The two initial images are displayed in the following figure. The black portion of the land classification image (left) will be removed and this image will then be overlaid on top of the map image.

Figure 4-10: Original Land and Map Images

Defining_Transparency_in_Image_Objects-18.jpg

Example Code
See alphacomposite_image_doc.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering alphacomposite_image at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT .EDIT alphacomposite_image.pro.

To replicate this example, create a new .pro file complete the following steps:

  1. Open the political map, the base image.
  2. mapFile = FILEPATH('afrpolitsm.png', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    mapImg = READ_PNG(mapFile, mapR, mapG, mapB) 
    
  3. Assign the color table of the map image to a palette object.
  4. mapPalette = OBJ_NEW('IDLgrPalette', mapR, mapG, mapB) 
    
  5. Create an image object containing the map data.
  6. oMapImg = OBJ_NEW('IDLgrImage', mapImg, $ 
       DIMENSIONS=[600, 600], PALETTE=mapPalette) 
    
  7. Open the land cover characteristics image.
  8. landFile = FILEPATH('africavlc.png', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    landImg = READ_PNG(landFile, landR, landG, landB) 
    landImgDims = SIZE(landImg, /DIMENSIONS) 
    
  9. To mask out the black values of the land classification image, create a 4 channel array for the red, green, blue, and alpha data.
  10. alphaLand = BYTARR(4, landImgDims[0], landImgDims[1],$ 
       /NOZERO) 
    
  11. Get the red, green and blue values used by the image and assign them to the first three channels of the alpha image array.
  12. alphaLand[0, *, *] = landR[landImg] 
    alphaLand[1, *, *] = landG[landImg] 
    alphaLand[2, *, *] = landB[landImg] 
    
  13. Mask out the black pixels with a value of 0. Multiply the mask value by 255 for complete opacity. You could set this to a value between 0 (completely transparent) and 255 (opaque) to control the transparency. Any value set here will be combined with any value set for the ALPHA_CHANNEL property on the image object.
  14. mask = (landImg GT 0) 
    alphaLand [3, *, *] = mask*255B 
    
  15. Create the semi-transparent image object. ALPHA_CHANNEL values can range from 0.0 (transparent) to 1.0 (opaque). The image will appear semi-transparent when the BLEND_FUNCTION property is set to [3,4].
  16. oAlphaLand = OBJ_NEW('IDLgrImage', alphaLand, $ 
       DIMENSIONS=[600, 600], BLEND_FUNCTION=[3,4], $ 
       ALPHA_CHANNEL=0.35) 
    
  17. Create the display objects.
  18. oWindow = OBJ_NEW('IDLgrWindow', $ 
       DIMENSIONS=[600, 600], RETAIN=2, $ 
       TITLE='Overlay of Land Cover Transparency') 
    viewRect = [0, 0, 600, 600] 
    oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT=viewRect) 
    oModel = OBJ_NEW('IDLgrModel') 
    
  19. Add the semi-transparent image to the model after the base image.
  20. oModel->Add, oMapImg 
    oModel->Add, oAlphaLand 
    oView->Add, oModel 
    oWindow->Draw, oView 
    
  21. Clean up objects.
  22. OBJ_DESTROY, [oView, oMapImg, oAlphaLand, mapPalette] 
    

The results appear in the following figure.

Figure 4-11: Land Image (35% Opaque) Overlaid the Map Image

alphaImg35.gif

Note
You can use control points to warp the images and properly align the transparent image over the map image. See Warping Image Objects for details.