Shifting Images
The SHIFT function moves elements of a vector or array along any dimension by any number of elements. All shifts are circular. Elements shifted off one end are wrapped around, appearing at the opposite end of the vector or array.
Occasionally, image files are saved with array elements offset. The SHIFT function allows you to easily correct such images assuming you know the amounts of the vertical and horizontal offsets. In the following example, the x-axis of original image is offset by a quarter of the image width, and the y-axis is offset by a third of the height.
Using the SHIFT syntax, Result = SHIFT(Array, S1, ..., Sn), we will enter negative values for the S (dimension) amounts in order to correct the image offset.
Example Code
See shiftimageoffset.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering shiftimageoffset at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT shiftimageoffset.pro.
- Select the image file and read it into memory:
- Prepare the display device and load the image's associated color table:
- Get the size of the image, prepare a window based upon the values returned by the SIZE function, and display the image to be corrected:
- Use SHIFT to correct the original image. Move the elements along the x-axis to the left, using a quarter of the array width as the x-dimension values. Move the y-axis elements, using one third of the array height as the number of elements to be shifted. By entering negative values for the amount the image dimensions are to be shifted, the array elements move toward the x and y axes.
- Display the corrected image in a second window:
file = FILEPATH('shifted_endocell.png', $
SUBDIRECTORY = ['examples','data'])
image = READ_PNG(file, R, G, B)
imageSize = SIZE(image, /DIMENSIONS) WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $ TITLE = 'Original Image' TV, image
The following figure displays the corrected image.

