X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



6337 Rate this article:
No rating

Alpha Channel: Creating a Transparent Image Object with IDL

In IDL Object Graphics, image transparency is created by adding an alpha channel to an image array.

The alpha channel is used to control the level of transparency in an image object. The following Object Graphics example uses the IDLgrImage::Init method to create an image object and employs the BLEND_FUNCTION keyword to specify how the transparency of the alpha channel is applied. Other methods of applying a transparent image object include using the TEXTURE_MAP keyword in conjunction with either the IDLgrPolygon::Init or the IDLgrSurface::Init methods.

The following example creates two image objects of MRI slices of a human head. After adding an alpha channel to the second image object, it is layered over the first image object as a transparency.  

Code example:

PRO alpha_blend_obj

    ; Determine path to file.
    headFile = FILEPATH('head.dat', $
    SUBDIRECTORY = ['examples', 'data'])

    ; Initialize volume array and size parameter.
    headSize = [80, 100, 57]
    head = BYTARR(headSize[0], headSize[1], headSize[2])
    imageSize = [240, 300]

    ; Open file, read in volume, and close file.
    OPENR, unit, headFile, /GET_LUN
    READU, unit, head
    FREE_LUN, unit

    ; Initialize window and view objects to vertically
    ; display two images.
    oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $
     DIMENSIONS = [imageSize[0], 2*imageSize[1]], $
     TITLE='MRI Slices')
    oView = OBJ_NEW('IDLgrView', $
     VIEWPLANE_RECT = [0., 0., imageSize[0], 2*imageSize[1]])

    ; Initialize a model object for each image.
    oModel = [OBJ_NEW('IDLgrModel'), OBJ_NEW('IDLgrModel')]

    ; Extract the first slice of data.
    layer1 = CONGRID(head[*,*,30], imageSize[0],imageSize[1],$
     /INTERP)

    ; Initialize the first image layer.
    oLayer1 = OBJ_NEW('IDLgrImage', layer1)

    ; Extract the second slice of data.
    layer2 = CONGRID(head[*,*,43], imageSize[0],imageSize[1],$
     /INTERP)

    ; Initialize second image layer with a palette.
    oPalette = OBJ_NEW('IDLgrPalette')
    oPalette -> LoadCT, 12
    oLayer2 = OBJ_NEW('IDLgrImage', layer2, PALETTE = oPalette)

    ; Add the layers to the model.
    oModel[0] -> Add, oLayer1
    oModel[1] -> Add, oLayer2

    ; Translate the first layer to the top of the
    ; display. Initially, the lower left corner of both
    ; models are at the lower left corner of the display.
    ; The model of the first layer must be moved above the
    ; second layer model to allow both to be displayed.
    oModel[0] -> Translate, 0., imageSize[1], 0.

    ; Add the model to the view, and then display the view
    ; in the window.
    oView -> Add, oModel
    oWindow -> Draw, oView

    ; Cleanup object references.
    OBJ_DESTROY, [oView]

    ; Get the red, green and blue values of the palette.
    oPalette -> GetProperty, RED_VALUES = oRed, $
    GREEN_VALUES = oGreen, BLUE_VALUES = oBlue

    ; Create a four channel array for alpha blending.
    alpha = BYTARR(4, imageSize[0], imageSize[1])

    ; Add the palette values to the first three channels.
    alpha[0,*,*]= oRed[layer2]
    alpha[1,*,*]= oGreen[layer2]
    alpha[2,*,*]= oBlue[layer2]

    ; Create a mask to remove lower pixels values from array.
    mask = BYTARR(imageSize[0], imageSize[1])
    mask = (layer2 GT 25)

    ; Apply the mask to the alpha (fourth) channel of the
    ; array. Set transparency to 80. Range is 0 (completely
    ; transparent)to 255 (completely opaque).
    alpha [3,*,*] = mask * 80

    ; Initialize the alpha image object, setting blend function.
    oAlpha = OBJ_NEW('IDLgrImage', alpha, $
     DIMENSIONS =[imageSize[0], imageSize[1]], $
     BLEND_FUNCTION = [3,4])

    ; Initialize the window, model and view.
    oWindow =OBJ_NEW('IDLgrWindow', DIMENSIONS =[imageSize[0],$
     imageSize[1]], LOCATION = [300,0], RETAIN = 2,$
     TITLE ='Alpha Blending Example')
    oView = OBJ_NEW('IDLgrView', $
     VIEWPLANE_RECT=[0,0,imageSize[0], imageSize[1]])
    oModel = OBJ_NEW('IDLgrModel')

    ; Initialize a new image object for layer1.
    oBase = OBJ_NEW('IDLgrImage', layer1)
    ; Add the transparent image objects AFTER adding other
    ; image objects to the model.
    oModel -> Add, oBase
    oModel -> Add, oAlpha
    oView -> Add, oModel
    ; Display the transparent image object.
    oWindow -> Draw, oView

    ; Cleanup object references.
    OBJ_DESTROY, [oView, oPalette]

END
_____________________________________________________
Reviewed by BC on 09/05/2014
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »