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.
- Open the world image file, using the
R,G,Barguments to obtain the image's color information: - 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:
- Get the size of the image and prepare the window display using the dimensions returned by the SIZE command:
- Display the image:
- Use the cursor function to define the lower-left corner of the cropped image by entering the following line:
- 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.
- Name the cropped image and define its array using the lower-left and upper-right x and y variables:
- Prepare a window based on the size of the new array:
- Display the cropped image:
TV, worldIn 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.
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). SeeCropWorld.proin theexamples/doc/imagesubdirectory of the IDL installation directory for an example.
CURSOR, LeftLowX, LeftLowY, /DEVICEThe 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 LeftLowXand LeftLowYappear in the IDL Workbench Variable Watch window. Alternately, usePRINT,LeftLowX, LeftLowYto display these values.
Your image should appear similar to the following figure.

