Date: Tue, 10 Dec 1996 17:44:38 GMT Server: NCSA/1.4.2 Content-type: text/html Last-modified: Tue, 05 Mar 1996 23:23:43 GMT Content-length: 8011 : Image Transformations

Image Transformations
Translation, Scaling, and Rotation

These transformations can be applied to an image using the XFORM programming language and the XLISP-STAT and METIP programming environment.

IMPORTANT NOTE: In this programming environment, the lower-lefthand corner of an image corresponds to pixel coordinates (0,0), and the upper-righthand corner of an image corresponds to pixel coordinates (n-1,m-1) where n is the width and m is the height of the image.


Translation

We translate an image by taking each pixels original coordinates and adding to them a translation amount. Translation can occur in each dimension. Since an image has only 2 dimensions, we can translate in the x and y dimensions. To translate an image by dx and dy, we compute the pixel value P(x,y) in the destination image by finding the pixel value P'(x', y') in the source image where x' and y' are defined as follows. (Note: These are mathematical formulas, not LISP expressions).

x' = x + dx
y' = y + dy

The line of code used in XLISP-STAT to perform this transformation on a source image source1 is

(xform '(source1 (+ x dx) (+ y dy)))

where dx and dy are the translation amounts in the x and y dimensions, respectively. Also, notice that dx and dy can be negative values. The following example translation moves the image 20 units to the right on the x axis and 30 units down the y axis.

(xform '(source1 (+ x -20) (+ y 30)))


Scaling

We scale an image by taking each pixels original coordinates and multiplying them by a scaling factor. Scaling can also occur in each dimension. To scale an image by sx and sy, we compute the pixel value P(x,y) in the destination image by finding the pixel value P'(x', y') in the source image where x' and y' are defined as follows. (Note: These are mathematical formulas, not LISP expressions).

x' = x * sx
y' = y * sy

The line of code used in XLISP-STAT to perform this transformation on a source image source1 is

(xform '(source1 (* x sx) (* y sy)))

where sx and sy are the scaling factors in the x and y dimensions, respectively. Note again that sx and sy can be negative values. The following illustrates a scaling transformation that stretches the image along the x axis (by a factor of 2) and shrinks the image along the y axis (by a factor of .5).

(xform '(source1 (* x .5) (* y 2)))


Rotation

To rotate an image by some angle theta (in radians), we compute the pixel value P(x,y) in the destination image by finding the pixel value P'(x', y') in the source image where x' and y' are defined as follows. (Note: These are mathematical formulas, not LISP expressions).

x' = (x * cos theta) - (y * sin theta)
y' = (x * sin theta) + (y * cos theta)

The line of code used in XLISP-STAT to perform this transformation on a source image source1 is

(xform '(source1 (- (* x costheta) (* y sintheta)) (+ (* x sintheta) (* y costheta))))

Before entering this line of code, it is necessary to define sintheta and costheta. This can be accomplished with the following 2 lines of code.

(setq costheta (cos theta))
(setq sintheta (sin theta))

If instead of radians, you prefer to use degrees, the following 2 lines of code can replace the above 2 lines.

(setq costheta (cos (/ (* (* theta 2) 3.14) 360)))
(setq sintheta (sin (/ (* (* theta 2) 3.14) 360)))

The following illustrates a rotation transformation that rotates the image 30 degrees from the y axis (clockwise).

(setq costheta (cos (/ (* (* 30 2) 3.14) 360)))
(setq sintheta (sin (/ (* (* 30 2) 3.14) 360)))
(xform '(source1 (- (* x costheta) (* y sintheta)) (+ (* x sintheta) (* y costheta))))


Activities

Multiple Transformations

Function trans_scale_rot allows you to perform multiple transformations on one source image. More specifically, it allows you to perform a translation, a scaling, and a rotation on a single image in the specified order. The function takes five parameters: the first two parameters, transX and transY, specify the translation amounts; the next two parameters, scaleX and scaleY, specify the scaling factors; and the last parameter, rotTheta specifies the rotation angle in degrees. The function template follows.

(trans_scale_rot transX transY scaleX scaleY rotTheta)

The following line of code illustrates the application of this function to an image. The image is first translated 50 units right on the x axis and 50 units up on the y axis. It is then scaled by a factor of .5 in the x and y direction (shrinking the image to 1/4 its original size). Finally, the image is rotated 30 degrees clockwise from the y axis.

(trans_scale_rot -50 -50 2 2 30)

In order to use this function, you must first open three windows by selecting "Child" from the Windows menu. Then tile the windows by selecting "Tile" from the Windows menu. The function trans_scale_rot will now put the translated image in window 1, the translated and scaled image in window 2, and the translated, scaled, and rotated image in window 3. Try using different parameters to the function, and see how they change the resulting images.

Guessing Game

Find a partner, and have one person be the coder and the other person be the guesser. While the guesser is looking away from the computer screen, the coder enters either a single transformation or multiple transformations (as defined above). After the transformed image is computed, the guesser must try to determine what transformation(s) were used to create the resulting image. You may wish to switch roles as the game progresses, and find some way of scoring how close the guesser gets to the actual transformations the coder entered.


Applications

Many electronic games require you to visualize a shape after it has undergone a given transformation. For example, the following might be a mental scenario of someone playing Tetris: "Well, if I move this to the left 3 units, and rotate it once, it will fit in that spot." The first operation performed was a translation along the x axis by 3 units, and the second operation was a rotation by 90 degrees. And you thought you were just playing a game!

Geometric transformations also have important real world applications. For example, an architect may visually apply transformations to some object, say a beam, to figure out its best position in some design. If the beam isn't quite long enough to fill a gap, it may need to be scaled to increase its length (Of course, this can only be done during the design process on a computer or paper, since we are all aware that you can't stretch a real beam.).

What other applications can you think of for geometric transformations?


This tutorial was created by Marla Baker (marla@cs.washington.edu)