|
Object Programming: Working with Image Objects |
|
Object Graphics allows precise control over the color palettes used to display image objects. By initializing a palette object, both the reference image object and the transparent, warped image object can be displayed using individual color palettes.
The following example warps an African land-cover characteristics image to a political map of the continent. After displaying the images and selecting control points in each image using the XROI utility, the resulting warped image is altered to include an alpha channel, enabling transparency. Image objects are then created and displayed in an IDL Object Graphics display. Complete the following steps for a detailed description of the process.
| Example Code See transparentwarping_object.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. |
| Note For background information on warping images and selecting control points, see Overview of Warping Images. |
mapFile= FILEPATH('afrpolitsm.png', $
Subdirectory = ['examples', 'data'])
mapImg = READ_PNG(mapFile, mapR, mapG, mapB)
mapPalette = OBJ_NEW('IDLgrPalette', mapR, mapG, mapB)
landFile = FILEPATH('africavlc.png', $
Subdirectory = ['examples', 'data'])
landImg = READ_PNG (landFile, landR, landG, landB)
This section describes using the XROI utility to select corresponding control points in the two images. The arrays of control points in the input image, (Xi, Yi), will be mapped to the array of points selected in the reference image, (Xo, Yo).
| Note The Xi and Yi vectors and the Xo and Yo vectors must be the same length, meaning that you must select the same number of control points in the reference image as you select in the input image. The control points must also be selected in the same order since the point Xi1, Yi1 will be warped to Xo1, Yo1. |
The following figure shows the points to be selected in the input image.
![]() |
Reasonably precise warping of the land classification image to the political map requires selecting numerous control points because of the irregularity of the continent's border. Select the control points in the land classification image as described in the following steps.
XROI, landImg, landR, landG, landB, $ REGIONS_OUT = landROIout, /BLOCK
Select the Draw Polygon button from the XROI utility toolbar shown in the following figure. Position the crosshairs symbol over CP1, shown in the previous figure, and click the left mouse button. Repeat this action for each successive control point. After selecting the sixteenth control point, position the crosshairs over the first point selected and click the right mouse button to close the region. Your display should appear similar to the following figure.
| Note It is of no concern that portions of the continent lie outside the polygonal boundary. The EXTRAPOLATE keyword to WARP_TRI enables warping of the image areas lying outside of the boundary of control points. However, images requiring more aggressive warp models may not have good results outside of the extent of the control points when WARP_TRI is used with the /EXTRAPOLATE keyword. |
landROIout -> GetProperty, DATA = landROIdata Xi = landROIdata[0,*] Yi = landROIdata[1,*]
The following figure displays the corresponding control points to be selected in the reference image of the political map. These control points will make up the Xo and Yo arrays required by the IDL warping routines.
![]() |
XROI, mapImg, mapR, mapG, mapB, $ REGIONS_OUT=mapROIout,/BLOCK
Select the Draw Polygon button from the XROI utility toolbar. Position the crosshairs symbol over CP1, shown in the previous figure, and click the left mouse button. Repeat this action for each successive control point. After selecting the sixteenth control point, position the crosshairs over the first point selected and click the right mouse button to close the region. Your display should appear similar to the following figure.
mapROIout -> GetProperty, DATA=mapROIdata Xo = mapROIdata[0,*] Yo = mapROIdata[1,*]
The following section describes warping the land cover image to the political map and creating image objects. The resulting warped image will then be made into a transparency by creating an alpha channel for the image. Finally, the transparent object will be displayed as an overlay to the original political map.
Result= WARP_TRI( Xo, Yo, Xi, Yi,Image[, OUTPUT_SIZE=vector][, /QUINTIC] [, /EXTRAPOLATE] )
set the OUTPUT_SIZE equal to the reference image dimensions since this image forms the basis of the warped, output image. Use the EXTRAPOLATE keyword to display the portions of the image which fall outside of the boundary of selected control points:
warpImg = WARP_TRI(Xo, Yo, Xi, Yi, landImg, $ OUTPUT_SIZE=[600, 600], /EXTRAPOLATE)
DEVICE, DECOMPOSED = 0 TVLCT, landR, landG, landB WINDOW, 3, XSIZE = 600, YSIZE = 600, $ TITLE = 'Image Warped with WARP_TRI' TV, warpImg
Precise control point selection results in accurate warping. If there is little distortion, as in the following figure, control points were successfully selected in nearly corresponding positions in both images.
The following lines convert the warped image and its associated color table into a RGB image containing four channels (red, green, blue, and alpha). First, get the dimensions of the warped image and then use BYTARR to create alphaWarp, a 4-channel by xdim by ydim array, where (xdim, ydim) are the dimensions of the warped image:
warpImgDims = SIZE(warpImg, /Dimensions) alphaWarp = BYTARR(4, warpImgDims[0], warpImgDims[1],$ /NOZERO)
alphaWarp[0, *, *] = landR[warpImg] alphaWarp[1, *, *] = landG[warpImg] alphaWarp[2, *, *] = landB[warpImg]
mask = (warpImg GT 0)
Apply the resulting mask to the alpha channel, the fourth channel of the array. This channel creates a 50% transparency of the pixels of the first three channels (red, green, blue) of the alphaWarp by multiplying the mask by 128B (byte). Alpha channel values range from 0 (completely transparent) to 255 (completely opaque):
alphaWarp [3, *, *] = mask*128B
| Note You can set the transparency of an entire image. To set the transparency of all pixels at 50% in this example, your could replace the two previous steps with the following two lines: mask = BYTARR(s[0], s[1]) + 128 alphaWarp [3, *, *] = mask |
oAlphaWarp = OBJ_NEW('IDLgrImage', alphaWarp, $
DIMENSIONS = [600, 600], BLEND_FUNCTION = [3, 4])
oMapImg = OBJ_NEW('IDLgrImage', mapImg, $
DIMENSIONS = [600,600], PALETTE = mapPalette)
oWindow = OBJ_NEW('IDLgrWindow', DIMENSIONS = [600, 600], $
RETAIN = 2, TITLE = 'Overlay of Land Cover Transparency')
0, 0 place the (0, 0) coordinate of viewing surface in the lower-left corner of the Object Graphics window:
viewRect = [0, 0, 600, 600]
oView = OBJ_NEW('IDLgrView', VIEWPLANE_RECT = viewRect)
oModel = OBJ_NEW('IDLgrModel')
oModel -> Add, oMapImg
oModel -> Add, oAlphaWarp
| Note Image objects appear in the Object Graphics window in the order in which they are added to the model. If a transparent object is added to the model before an opaque object, it will not be visible. |
oView -> Add, oModel oWindow -> Draw, oView
The following figure shows the warped image transparency overlaid onto the original reference image, the political map.
![]() |
OBJ_DESTROY, [oView, oMapImg, oAlphaWarp, $ mapPalette, landROIout, mapROIout]
IDL Online Help (March 06, 2007)