Warping 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. Run the example procedure by entering transparentwarping_object at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT transparentwarping_object.pro.
Note
For background information on warping images and selecting control points, see Overview of Warping Images (Image Processing in IDL).
- Select the political map image. This is the reference image to which the land cover image will be warped:
- Use READ_PNG routine to read in the file. Specify mapR, mapG, mapB to read in the image's associated color table:
- Using IDLgrPalette::Init, assign the image's color table to a palette object, which will be applied to an image object in a later step:
- Select and open the land cover input image, which will be warped to the map:
Selecting Control Points for Image Object Warping
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.
- Load the image and its associated color table into the XROI utility, specifying the REGIONS_OUT keyword to save the region defined by the control points in the landROIout object:
- Close the XROI window and assign the landROIout object data to the Xi and Yi control point vectors:
- Load the image of the political map and its associated color table into the XROI utility, specifying the REGIONS_OUT keyword to save the selected region in the mapROIout object:
- Close the XROI window and assign the mapROIout object data to the Xo and Yo control point vectors:
XROI, landImg, landR, landG, landB, $ REGIONS_OUT = landROIout, /BLOCKSelect 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,/BLOCKSelect 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.
Warping and Displaying a Transparent Image Object
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.
- Warp the input image, landImg, onto the reference image using WARP_TRI. This function uses the irregular grid of the reference image, defined by Xo, Yo, as a basis for triangulation, defining the surfaces associated with (Xo, Yo, Xi) and (Xo, Yo, Yi). Each pixel in the input image is then transferred to the appropriate position in the resulting output image as designated by interpolation. Using the WARP_TRI syntax,
- While not required, you can quickly check the precision of the warp in a Direct Graphics display before proceeding with creating a transparency by entering the following lines:
- A transparent image object must be a grayscale or an RGB (24-bit) image containing an alpha channel. The alpha channel controls the transparency of the pixels. See IDLgrImage::Init for more information.
- Load the red, green and blue channels of the warped land characteristics image into the first three channels of the alphaWarp array:
- Define the transparency of the alpha channel. First, create an array, masking out the black background of the warped image (where pixel values equal 0) by retaining only pixels with values greater than 0:
- Initialize the transparent image object using IDLgrImage::Init. Specify the BLEND_FUNCTION property of the image object to control how the alpha channel is interpreted. Setting the BLEND_FUNCTION to [3, 4] allows you to see through the foreground image to the background. The foreground opacity is defined by the alpha channel value, specified in the previous step:
- Initialize the reference image object, applying the palette created earlier:
- Using IDLgrWindow::Init, initialize a window object in which to display the images:
- Create a view object using IDLgrView::Init. The VIEWPLANE_RECT keyword controls the image display in the Object Graphics window. First create an array, viewRect, which specifies the x-placement, y-placement, width, and height of the view surface. The values
0,0place the (0, 0) coordinate of viewing surface in the lower-left corner of the Object Graphics window: - Using IDLgrModel::Init, initialize a model object to which the images will be applied. Add the base image and the transparent alpha image to the model:
- Add the model, containing the images, to the view and draw the view in the window:
- Use OBJ_DESTROY to clean up unneeded object references including the region objects:
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, warpImgPrecise 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*128BNote
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]) + 128alphaWarp[3, *, *] = mask
oWindow = OBJ_NEW('IDLgrWindow', DIMENSIONS = [600, 600], $
RETAIN = 2, TITLE = 'Overlay of Land Cover Transparency')
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, oViewThe following figure shows the warped image transparency overlaid onto the original reference image, the political map.





