|
Object Programming: Working with 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.
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.
See the following topics for examples of creating transparent image objects:
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. |
To replicate this example, create a new .pro file and complete the following steps:
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)
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
img_ct = BYTSCL(img_ct)
oImageCT = OBJ_NEW('IDLgrImage', img_ct)
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
oPalette = OBJ_NEW('IDLgrPalette')
oPalette->Loadct, 3
img_pt = BYTSCL(img_pt)
oImagePT = OBJ_NEW('IDLgrImage', img_pt, $
PALETTE=oPalette, BLEND_FUNCTION=[3,4], $
ALPHA_CHANNEL=0.50)
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
OBJ_DESTROY, [oView, oImageCT, oImagePT]
The results of this example are shown in the following figure.
![]() |
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.
| Example Code See alphacomposite_image_doc.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. |
To replicate this example, create a new .pro file complete the following steps:
mapFile = FILEPATH('afrpolitsm.png', $
SUBDIRECTORY = ['examples', 'data'])
mapImg = READ_PNG(mapFile, mapR, mapG, mapB)
mapPalette = OBJ_NEW('IDLgrPalette', mapR, mapG, mapB)
oMapImg = OBJ_NEW('IDLgrImage', mapImg, $
DIMENSIONS=[600, 600], PALETTE=mapPalette)
landFile = FILEPATH('africavlc.png', $
SUBDIRECTORY = ['examples', 'data'])
landImg = READ_PNG(landFile, landR, landG, landB)
landImgDims = SIZE(landImg, /DIMENSIONS)
alphaLand = BYTARR(4, landImgDims[0], landImgDims[1],$ /NOZERO)
alphaLand[0, *, *] = landR[landImg] alphaLand[1, *, *] = landG[landImg] alphaLand[2, *, *] = landB[landImg]
mask = (landImg GT 0) alphaLand [3, *, *] = mask*255B
oAlphaLand = OBJ_NEW('IDLgrImage', alphaLand, $
DIMENSIONS=[600, 600], BLEND_FUNCTION=[3,4], $
ALPHA_CHANNEL=0.35)
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')
oModel->Add, oMapImg oModel->Add, oAlphaLand oView->Add, oModel oWindow->Draw, oView
OBJ_DESTROY, [oView, oMapImg, oAlphaLand, mapPalette]
The results appear in the following figure.
![]() |
| 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. |
IDL Online Help (March 06, 2007)