Indexed and RGB Image Organization

IDL can display four types of images: binary, grayscale, indexed, and RGB. How an image is displayed depends upon its type. Binary images have only two values, zero and one. Grayscale images represent intensities and use a normal grayscale color table. Indexed images use an associated color table. RGB images contain their own color information in layers known as bands or channels. Any of these images can be displayed with iImage, Object Graphics, or Direct Graphics.

An image consists of a two-dimensional array of pixels. The value of each pixel represents the intensity and/or color of that position in the scene. Images of this form are known as sampled or raster images, because they consist of a discrete grid of samples. Such images come from many different sources and are a common form of representing scientific and medical data.

Numerous standards have been developed over the years to describe how an image can be stored within a file. However, once the image is loaded into memory, it typically takes one of two forms: indexed or RGB. An indexed image is a two-dimensional array, and is usually stored as byte data. A two-dimensional array of a different data type can be made into an indexed image by scaling it to the range from 0 to 255 using the BYTSCL function. See the BYTSCL description in the IDL Reference Guide for more information.

Image Orientation

The screen coordinate system for image displays puts the origin, (0, 0), at the lower-left corner of the device. The upper-right corner has the coordinate (xsize–1, ysize–1), where xsize and ysize are the dimensions of the visible area of the display. The descriptions of the image display routines that follow assume a display size of 512 x 512, although other sizes may be used.

The system variable !ORDER controls the order in which the image is written to the screen. Images are normally output with the first row at the bottom, i.e., in bottom-to-top order, unless !ORDER is 1, in which case images are written on the screen from top to bottom. The ORDER keyword also can be specified with TV and TVSCL. It works in the same manner as !ORDER except that its effect only lasts for the duration of the single call—the default reverts to that specified by !ORDER.

An image can be displayed with any of the eight possible combinations of axis reversal and transposition by combining the display procedures with the ROTATE function.

Indexed Images

An indexed image does not explicitly contain any color information. Its pixel values represent indices into a color Look-Up Table (LUT). Colors are applied by using these indices to look up the corresponding RGB triplet in the LUT. In some cases, the pixel values of an indexed image reflect the relative intensity of each pixel. In other cases, each pixel value is simply an index, in which case the image is usually intended to be associated with a specific LUT. In this case, the LUT is typically stored with the image when it is saved to a file. For information on the LUTs provided with IDL, see Loading a Default Color Table.

RGB Image Interleaving

An RGB (red, green, blue) image is a three-dimensional byte array that explicitly stores a color value for each pixel. RGB image arrays are made up of width, height, and three channels of color information. Scanned photographs are commonly stored as RGB images. The color information is stored in three sections of a third dimension of the image. These sections are known as color channels, color bands, or color layers. One channel represents the amount of red in the image (the red channel), one channel represents the amount of green in the image (the green channel), and one channel represents the amount of blue in the image (the blue channel).

Color interleaving is a term used to describe which of the dimensions of an RGB image contain the three color channel values. Three types of color interleaving are supported by IDL. In Object Graphics, an RGB image is contained within an image object where the INTERLEAVE property dictates the arrangement of the channels within the image file.

Determining RGB Image Interleaving

You can determine if an image file contains an RGB image by querying the file. The CHANNELS tag of the resulting query structure will equal 3 if the file's image is RGB. The query does not determine which interleaving is used in the image, but the array returned in DIMENSIONS tag of the query structure can be used to determine the type of interleaving.

The following example queries and imports a pixel-interleaved RGB image from the rose.jpg image file. This RGB image is a close-up photograph of a red rose. It is pixel interleaved. Complete the following steps for a detailed description of the process.

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

  1. Determine the path to the rose.jpg file:
  2. file = FILEPATH('rose.jpg', $ 
       SUBDIRECTORY = ['examples', 'data']) 
    
  3. Use QUERY_IMAGE to query the file to determine image parameters:
  4. queryStatus = QUERY_IMAGE(file, imageInfo) 
    
  5. Output the results of the file query:
  6. PRINT, 'Query Status = ', queryStatus 
    HELP, imageInfo, /STRUCTURE 
     

    The following text appears in the Output Log:

    Query Status = 1 ** Structure <14055f0>, 7 tags, length=36, refs=1: CHANNELS      LONG     3 DIMENSIONS    LONG     Array[2] HAS_PALETTE   INT      0 IMAGE_INDEX   LONG     0 NUM_IMAGES    LONG     1 PIXEL_TYPE    INT      1 TYPE          STRING   'JPEG'

    The CHANNELS tag has a value of 3. Thus, the image is an RGB image.

  7. Set the image size parameter from the query information:
  8. imageSize = imageInfo.dimensions 
     

    The type of interleaving can be determined from the image size parameter and actual size of each dimension of the image. To determine the size of each dimension, you must first import the image.

  9. Use READ_IMAGE to import the image from the file:
  10. image = READ_IMAGE(file) 
    
  11. Determine the size of each dimension within the image:
  12. imageDims = SIZE(image, /DIMENSIONS) 
    
  13. Determine the type of interleaving by comparing the dimension sizes to the image size parameter from the file query:
  14. interleaving = WHERE((imageDims NE imageSize[0]) AND $ 
       (imageDims NE imageSize[1])) 
    
  15. Output the results of the interleaving computation:
  16. PRINT, 'Type of Interleaving = ', interleaving 
     

    The following text appears in the Output Log:

    Type of Interleaving = 0

    The image is pixel interleaved. If the resulting value was 1, the image would have been line interleaved. If the resulting value was 2, the image would have been planar interleaved.

  17. Initialize the display objects:
  18. oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $ 
       DIMENSIONS = imageSize, TITLE = 'An RGB Image') 
    oView = OBJ_NEW('IDLgrView', $ 
       VIEWPLANE_RECT = [0., 0., imageSize]) 
    oModel = OBJ_NEW('IDLgrModel') 
    
  19. Initialize the image object:
  20. oImage = OBJ_NEW('IDLgrImage', image, $ 
       INTERLEAVE = interleaving[0]) 
    
  21. Add the image object to the model, which is added to the view, then display the view in the window:
  22. oModel -> Add, oImage 
    oView -> Add, oModel 
    oWindow -> Draw, oView 
     

    The following figure shows the resulting RGB image display.

    Figure 5-4: RGB Image in Object Graphics

    imgdisp10.gif

  23. Clean up the object references. When working with objects always remember to clean up any object references with the OBJ_DESTROY routine. Since the view contains all the other objects, except for the window (which is destroyed by the user), you only need to use OBJ_DESTROY on the view object.
  24. OBJ_DESTROY, oView 
    

Converting Between Image Types

Sometimes an image type must be converted from indexed to RGB, RGB to grayscale, or RGB to indexed. For example, an image may be imported into IDL as an indexed image (from a PNG file for example) but it may need to be exported as an RGB image (to a JPEG file for example). The opposite may also need to be done. See Foreground Color for more information on grayscale, indexed, and RGB images.

See the following routines s in the IDL Reference Guide for examples: