Previous Image Processing in IDL: Working with Masks and Image Statistics Next

Masking Images

Masking (also known as thresholding) is used to isolate features within an image above, below, or equal to a specified pixel value. The value (known as the threshold level) determines how masking occurs. In IDL, masking is performed with the relational operators. IDL's relational operators are shown in the following table.

Table 4-2: IDL's Relational Operators

Table 4-2: IDL's Relational Operators
Operator
Description
EQ
Equal to
NE
Not equal to
GE
Greater than or equal to
GT
Greater than
LE
Less than or equal to
LT
Less than

For example, if you have an image variable and you want to mask it to include only the pixel values equaling 125, the resulting mask variable is created with the following IDL statement.

mask = image EQ 125  

The mask level is applied to every element in the image array, which results in a binary image.


Note
You can also provide both upper and lower bounds to masks by using the bitwise operators; AND, NOT, OR, and XOR. See Bitwise Operators in the Building IDL Applications for more information on these operators.

The following example uses masks derived from the image contained in the worldelv.dat file, which is in the examples/data directory. Masks are derived to extract the oceans and land. These masks are applied back to the image to show only on the oceans or the land. Masks are applied by multiplying them with the original image. Complete the following steps for a detailed description of the process.


Example Code
See maskingimages.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example.

  1. Determine the path to the file:
  2. file = FILEPATH('worldelv.dat', $  
       SUBDIRECTORY = ['examples', 'data'])  
    

     

  3. Initialize the image size parameter:
  4. imageSize = [360, 360]  
    

     

  5. Import the image from the file:
  6. image = READ_BINARY(file, DATA_DIMS = imageSize)  
    

     

  7. Initialize the display:
  8. DEVICE, DECOMPOSED = 0  
    LOADCT, 38  
    

     

  9. Create a window and display the image:
  10. WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'World Elevation'  
    TV, image  
    

     

    The following figure shows the original image, which represents the elevation levels of the world.

     

    Figure 4-1: World Elevation Image

    Figure 4-1: World Elevation Image

     

  11. Make a mask of the oceans:
  12. oceanMask = image LT 125  
    

     

  13. Multiply the ocean mask by the original image:
  14. maskedImage = image*oceanMask  
    

     

  15. Create another window and display the mask and the results of the multiplication:
  16. WINDOW, 1, XSIZE = 2*imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Oceans Mask (left) and Resulting Image (right)'  
    TVSCL, oceanMask, 0  
    TV, maskedImage, 1  
    

     

    The following figure shows the mask of the world's oceans and the results of applying it to the original image.

     

    Figure 4-2: Oceans Mask (left) and the Resulting Image (right)

    Figure 4-2: Oceans Mask (left) and the Resulting Image (right)

     

  17. Make a mask of the land:
  18. landMask = image GE 125  
    

     

  19. Multiply the land mask by the original image:
  20. maskedImage = image*landMask  
    

     

  21. Create another window and display the mask and the results of the multiplication:
  22. WINDOW, 2, XSIZE = 2*imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Land Mask (left) and Resulting Image (right)'  
    TVSCL, landMask, 0  
    TV, maskedImage, 1  
    

     

    The following figure shows the mask of the land masses of the world and the results of applying it to the original image.

     

    Figure 4-3: Land Mask (left) and the Resulting Image (right)

    Figure 4-3: Land Mask (left) and the Resulting Image (right)

  IDL Online Help (June 16, 2005)