Cropping Images

Cropping an image extracts a rectangular region of interest from the original image. This focuses the viewer's attention on a specific portion of the image and discards areas of the image that contain less useful information. Using image cropping in conjunction with image magnification allows you to zoom in on a specific portion of the image. This section describes how to exactly define the portion of the image you wish to extract to create a cropped image. For information on how to magnify a cropped image, see Resizing Images.

Image cropping requires a pair of (x, y) coordinates that define the corners of the new, cropped image. The following example extracts the African continent from an image of the world. Complete the following steps for a detailed description of the process.

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

  1. Open the world image file, using the R,G,B arguments to obtain the image's color information:
  2. world = READ_PNG (FILEPATH ('avhrr.png', $ 
       SUBDIRECTORY = ['examples', 'data']), R, G, B) 
    
  3. Prepare the display device and load the color table with the red, green and blue values retrieved from the image file in the previous step:
  4. DEVICE, RETAIN = 2, DECOMPOSED = 0 
    TVLCT, R, G, B 
    
  5. Get the size of the image and prepare the window display using the dimensions returned by the SIZE command:
  6. worldSize = SIZE(world, /DIMENSIONS) 
    WINDOW, 0, XSIZE = worldSize[0], YSIZE = worldSize[1] 
    
  7. Display the image:
  8. TV, world 
     

    In this example, we will crop the image to display only the African continent as shown in the following figure. Two sets of coordinates, (LeftLowX, LeftLowY) and (RightTopX, RightTopY), will be used to create the new, cropped image array.

    Figure 2-1: Defining the Boundaries of the Cropped Image Array

    imggeom03.jpg

    In the following step, use the CURSOR function to define the boundaries of the cropped image. The values returned by the CURSOR function will be defined as the variables shown in the previous image.

    Note
    To crop an image without interactively defining the cursor position, you can use the actual coordinates of the cropped image array in place of the coordinate variables, (LeftLowX, LeftLowY) and (RightTopX, RightTopY). See CropWorld.pro in the examples/doc/image subdirectory of the IDL installation directory for an example.

  9. Use the cursor function to define the lower-left corner of the cropped image by entering the following line:
  10. CURSOR, LeftLowX, LeftLowY, /DEVICE 
     

    The cursor changes to a cross hair symbol when it is positioned over the graphics window. Click in the area to the left and below the African continent.

    Note
    The values for LeftLowX and LeftLowY appear in the IDL Workbench Variable Watch window. Alternately, use PRINT, LeftLowX, LeftLowY to display these values.

  11. Define the upper-right corner of the cropped image. Enter the following line and then click above and to the right of the African continent.
  12. CURSOR, RightTopX, RightTopY, /DEVICE 
    
  13. Name the cropped image and define its array using the lower-left and upper-right x and y variables:
  14. africa = world[LeftLowX:RightTopX, LeftLowY:RightTopY] 
    
  15. Prepare a window based on the size of the new array:
  16. WINDOW, 2, XSIZE = (RightTopX - LeftLowX + 1), $ 
       YSIZE = (RightTopY - LeftLowY + 1) 
    
  17. Display the cropped image:
  18. TV, africa 
    

Your image should appear similar to the following figure.

Figure 2-2: Result of the Cropped Image Example

imggeom04.gif