Previous Image Processing : Transforming Image Geometry Next

Rotating Images

To change the orientation of an image in IDL, use either the ROTATE or the ROT function. The ROTATE function changes the orientation of an image by 90 degree increments and/or transposes the array. The ROT function rotates an image by any amount and offers additional resizing options. For more information, see Using the ROT Function for Arbitrary Rotations.

Rotating an Image by 90 Degree Increments

The following example changes the orientation of an image by rotating it 270°.


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

  1. Select the file and read in the data, specifying known data dimensions:
  2. file = FILEPATH('galaxy.dat', $  
       SUBDIRECTORY=['examples', 'data'])  
    image = READ_BINARY(file, DATA_DIMS = [256, 256])  
    

     

  3. Prepare the display device, load a color table, create a window, and display the image:
  4. DEVICE, DECOMPOSED = 0, RETAIN = 2  
    LOADCT, 4  
    WINDOW, 0, XSIZE = 256, YSIZE = 256   
    TVSCL, image  
    

     

  5. Using the ROTATE syntax, Result = ROTATE (Array, Direction), rotate the galaxy image 270° counterclockwise by setting the Direction argument equal to 3. See ROTATE Direction Argument Options for more information.
  6. rotateImg = ROTATE(image, 3)  
    

     

  7. Display the rotated image.
  8. Window, 1, XSIZE = 256, YSIZE = 256,  
    TVSCL, rotateImg  
    

The following figure displays the original (left) and the rotated image (right).

 

Figure 2-10: Using ROTATE to Alter Image Orientation

Figure 2-10: Using ROTATE to Alter Image Orientation

ROTATE Direction Argument Options

The following table describes the Direction options available with the ROTATE function syntax, Result = ROTATE (Array, Direction).

Table 2-2: Direction Options Available with ROTATE 

Table 2-2: Direction Options Available with ROTATE 
Direction
Transpose?
Rotation Counterclockwise
Sample Image
0
No
None
1
No
90°
2
No
180°
3
No
270°
4
Yes
None
5
Yes
90°
6
Yes
180°
7
Yes
270°

Using the ROT Function for Arbitrary Rotations

The ROT function supports clockwise rotation of an image by any specified amount (not limited to 90 degree increments). Keywords also provide a means of optionally magnifying the image, selecting the pivot point around which the image rotates, and using either bilinear or cubic interpolation. If you wish to rotate an image only by 90 degree increments, ROTATE produces faster results.

The following example opens a image of a whirlpool galaxy, rotates it 33° clockwise and shrinks it to 50% of its original size.


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

  1. Select the file and read in the data, specifying known data dimensions:
  2. file = FILEPATH('m51.dat', $  
       SUBDIRECTORY = ['examples', 'data'])  
    image = READ_BINARY(file, DATA_DIMS = [340, 440])  
    

     

  3. Prepare the display device and load a black and white color table:
  4. DEVICE, DECOMPOSED = 0, RETAIN = 2  
    LOADCT, 0  
    

     

  5. Create a window and display the original image:
  6. WINDOW, 0, XSIZE = 340, YSIZE = 440  
    TVSCL, image  
    

     

  7. Using the ROT function syntax,
  8. Result=ROT(A, Angle, [Mag, X0, Y0] [,/INTERP] 
    [,CUBIC=value{-1 to 0}] [, MISSING=value] [,/PIVOT])  
    

     

    enter the following line to rotate the image 33°, shrink it to 50% of its original size, and fill the image display with a neutral gray color where there are no original pixel values:

     

    arbitraryImg = ROT(image, 33, .5, /INTERP, MISSING = 127)  
    

     

  9. Display the rotated image in a new window by entering the following two lines:
  10. WINDOW, 1, XSIZE = 340, YSIZE = 440  
    TVSCL, arbitraryImg  
    

Your output should appear similar to the following figure.

 

Figure 2-11: The Original Image (Left) and Modified Image (Right)

Figure 2-11: The Original Image (Left) and Modified Image (Right)

The MISSING keyword maintains the original image's boundaries, keeping the interpolation from extending beyond the original image size. Replacing MISSING = 127 with MISSING = 0 in the previous example creates a black background by using the default pixel color value of 0. Removing the MISSING keyword from the same statement allows the image interpolation to extend beyond the image's original boundaries.

  IDL Online Help (March 06, 2007)