Previous Image Processing : Transforming Image Geometry Next

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.

Figure 2-6: Example of Misaligned Image Array Elements

Figure 2-6: Example of Misaligned Image Array Elements

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.

  1. Select the image file and read it into memory:
  2. file = FILEPATH('shifted_endocell.png', $  
       SUBDIRECTORY = ['examples','data'])  
    image = READ_PNG(file, R, G, B)  
    

     

  3. Prepare the display device and load the image's associated color table:
  4. DEVICE, DECOMPOSED = 0, RETAIN = 2  
    TVLCT, R, G, B  
    

     

  5. 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:
  6. imageSize = SIZE(image, /DIMENSIONS)  
    WINDOW, 0, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE = 'Original Image'  
    TV, image  
    

     

  7. 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.
  8. image = SHIFT(image, -(imageSize[0]/4), -(imageSize[1]/3))  
    

     

  9. Display the corrected image in a second window:
  10. WINDOW, 1, XSIZE = imageSize[0], YSIZE = imageSize[1], $  
       TITLE='Shifted Image'  
    TV, image  
    

The following figure displays the corrected image.

 

Figure 2-7: Resulting Shifted Array

Figure 2-7: Resulting Shifted Array

  IDL Online Help (March 06, 2007)