Blog Archive

Saturday, September 1, 2012

2-D Transformations


2-d Transformations

Changing the shape or size of the image of any object in computer graphics is called Transformation. In other words transforming any image in any way is called transformation. Following are some basic kinds of transformations in 2 dimensions. These are:
  • Translation
  • Scaling
  • Rotation
  • Reflection
  • Shearing

More complex transformations can be obtained by combining two or more of the above transformations.




Any image is a set of pixels, if we apply any transformation to all the pixels of an image, the whole image will be transformed.





Types of 2d Transformations
Various transformation can be categorised as
·         Geometrical (or Object) Transformation : This transformation changes the coordinates of the pixels forming the image. This does not alter the underlying coordinate system.
·         Coordinate system transformation : This kind creates a new coordinate system and then represents all the pixels of the image in the new system.

Matrix / Vector representation of a point
A point p which is represented as (x,y) in the 2-d coordinate system can also be represented in the matrix form as follows. This is also called the Vector form.


Translation
Moving an image or a pixel from current position to another is called translation.
In the above picture point A has been translated from position (1,3) to position (3,5), ie. It has been moved +2 points in the x direction and +2 points in the y direction, so the point A (1,3) is translated to (1+2, 3+2)=(3,5)=A’. If we apply this translation to all the points of the image the whole image will be translated. Similarly other points of the triangle (1,1) & (3,1) are translated to (3,3) and (5,3) respectively.

If a point (x,y) is moved in the right direction then it’s x coordinate is added with a positive number (x+n,y), whereas if towards left then the point’s x coordinate is decremented (x-n,y).
If a point (x,y) is moved in the upward direction then it’s y coordinate is added with a positive number (x,y+n), whereas if down then the point’s y coordinate is decremented (x,y-n).

In translation the shape of the image remains the same, it is only moved from one point to another without distorting the image in any way.

Matrix / Vector representation of translation
A translation can also be represented in the matrix form as follows where T=(tx,ty) is the translation matrix and here tx is the change in the x-coordinate and ty is the change in y coordinate. To translate the point P(x,y) by T, we simply add to obtain the new (translated) point p' = P+T. This can be represented as follows.

So we can find out the new coordinates of a point (x,y) after translation as follows.
            X' =  tx + X
            Y' =  ty + Y


Scaling
Scaling is a transformation which increases or decreases the size of an image. It is similar to Zoom-in or Zoom-out. Scaling of a 2-d image means we must specify scaling in both the x and y direction.
 Fig1.


In the picture above the A” triangle is double (2 times) the triangle A. This means A” is 2 times the triangle A both in width and height. Here scaling in x direction is same as scaling in y direction, which is 2 times in both the directions. The triangle A” is similar to triangle A, this is because scaling in x and y direction is same.

The scaling in x and y directions can be different, this will distort the image, as seen in the following image.

 Fig2.

In this image the triangle A” is not similar to A. Here the triangle A has been scaled by a factor of 2 in x direction and it has been scaled by a factor of 1/2 or (0.5) in the y direction. Since scaling factor in x and y direction is not same the image has been distorted.
  • The scaling of a point (x,y) is done by multiplying the x co-ordinate with the scaling factor in x direction and multiplying the y coordinate of a point with the respective scaling factor.

Scaling Up & Scaling Down
When a point is scaled by a scaling factor which is more than 1, this is called Scale Up. The point in this case moves away from the origin. This happens because we are scaling with respect to the origin.
When a point is scaled by a factor less than 1, it is called scaling down. Here the point moves towards the origin, because the scaling is with respect to the origin.
  • In Fig2 tip of the triangle A assumed as point p (1.5,2) becomes q (3,1)
q = s * p  where s is the scaling factor. s*p = (1.5*2, 2*0.5) = (3,1).
The figure is scaled up in the x direction. So q is farther from origin as compared to p in x direction. Whereas the point q is near to the origin as compared to p in the y direction. So the triangle A is scaled up in the x direction, and scaled down in the y direction.

Matrix / Vector representation of Scaling
Scaling transformation can be represented in the vector or the matrix form with the help of following matrix.
In the Fig2 the point p (1.5,2) becomes point q (3,1). This (q = s * p) can be represented as follows.

So we can find out the new coordinates of a point (x,y) after scaling as follows.
            Xnew  =  Sx * X
            Ynew  =  SY * Y
Where Sx and SY represent the scaling factors in the X and Y directions respectively.

Scaling with respect to a point other than origin
We just saw that basic scaling is always with respect to the origin. What if we want to scale w.r.t. to a point that is not at origin, (a fixed point)? This situation calls for combining translation and scaling. The following steps need to be performed (geometric transformation has been used in this example).
  1. Translate the image so that the fixed point coincides with the origin .
  2. Perform scaling.
  3. Inverse the original translation, so that the fixed point moves back to its original position.
This is illustrated with the help of following Example.

In the above picture we have to scale up the triangle A by a factor of 2 in the X direction & by a factor of 1.5 in the Y direction, about the fixed point (1,0).
In order to do this we need to first translate the triangle A so that point (1,0) coincides with origin (0,0).  Then we do the scaling and then we do the inverse translation

In the above picture triangle A has been translated so that point (1,0) becomes (0,0).


The next step is to scale the image. This is shown in the above picture.

The next step is to inverse translate the image so that point at origin becomes the fixed point (1,0). This is shown in following figure.


This whole process can be represented in the vector form with the help of Matrices, as follows.

The triangle is represented with the following matrix.
Step 1: Translate the image so that the fixed point coincides with the origin.
Step 2 : Perform Scaling.
Step 3 : Inverse the original translation, so that the fixed point moves back to its original position.
So the new triangle has the coordinates (1,0), (2,3), (3,0). This is same as in the figures above.



Rotation
Rotation is a transformation which rotates a point or an image by an angle with respect to origin. The rotation is counter clockwise when the angle of rotation is positive, and the rotation is clockwise when angle of rotation is –ve. Rotation is shown in the following figure.


In the above picture point A (x,y) has been rotated by an angle θ and the new point is A’ (x’,y’).
Both the points A and A’ are at a distance r from the origin. A makes an angle ϕ with the X-axis at the origin. A’ makes an angle ϕ + θ with the X-axis at the origin.
At point A :
x = r * cos (ϕ)
y = r * sin (ϕ)               ....Eq1
At point A’ :
x’ = r * cos (ϕ + θ)  =  r * cos(ϕ) * cos(θ) – r * sin(ϕ) * sin(θ)
y’ = r * sin (ϕ + θ)  =  r * cos(ϕ) * cos(θ) + r * sin(ϕ) * sin(θ)              ....Eq2
Substituting for x and y from Eq1 in Eq2 :
            x’ =  x * cos(θ) – y * sin(θ)
            y’ =  x * sin(θ) – y * cos(θ)

At this point it is important to note the following trigonometric values.


Let’s look at the following pictorial example.
Fig Rot1

In this picture ABC is a triangle with coordinates as A=(1,0), B=(2,1) & C=(3,0).
If this triangle is rotated by an angle of +90̊. The coordinates of the triangle after rotation A’=(0,1), B’=(-1,2) & C’=(0,3).
If this triangle is rotated by an angle of -90̊. The coordinates of the triangle after rotation A’’=(0,-1), B’’=(1,-2) & C’’=(0,-3).

Matrix / Vector representation of Rotation
The rotation matrix can be represented as follows :
For Counter-clockwise rotation:
For Clockwise rotation:

The value of A’ after rotation by +θ̊ is as follows:


In the figure Rot1 the triangle ABC is rotated by 90̊ into triangles A’B’C’. This is represented in the vector form as follows:
A’= (0,1), B’= (-1,2) & C’= (0,3)

When ABC is rotated by -90̊  it becomes triangle A”B”C”. This is shown in following matrix form.
A’’= (0,-1), B’’= (1,-2) & C’’= (0,-3)


Scaling with respect to a point other than origin
As with Scaling the rotation transformation is also basically done with respect to Origin (0,0). What if we want to rotate w.r.t. to a point that is not at origin, (a fixed point)? As we saw with scaling we can perform this thing by combining translation and scaling. The following steps need to be performed.
  1. Translate the image so that the fixed point coincides with the origin. 
  2. Perform rotation.
  3. Inverse the original translation, so that the fixed point moves back to its original position.

Looking at the following picture


In the above picture let’s try to rotate the triangle by 90̊ while keeping the arbitrary point P(1,1) fixed. This is done with the help of above three steps and this is represented with the matrix form & also in the image form as follows:
Step 1: Translate the image so that the fixed point coincides with the origin.



Step 2 : Perform Rotation.




Step 3 : Inverse the original translation, so that the fixed point moves back to its original position.





So the triangle A(1,0), B(2,1), C(3,0) has been converted into A’ (2,1), B’ (1,2) & C’ (2,3). Looking closely we find that Rotation does not change the size or area of the image but scaling changes the size and area. 




Reflection
Reflection is a transformation which gives a mirror reflection of a point or a set of pixels. The reflection can be across X-axis or Y-axis or any other arbitrary line. The matrix form of reflection is as follows.
Across X-axis:
Across y-axis:
Reflecting a point P(x,y) across X-axis changes only the sign of the y coordinate of the point so the point becomes P’(x,-y). Similarly reflecting the point P across Y-axis changes the sign of the x coordinate of the point so this point P(x,y) becomes the point P’(-x,y). This is shown in the following picture.

Across X-axis:

Across Y-axis:

Reflection across arbitrary line
In case we need to reflect a point across a line which is not X or Y axis then following steps need to be performed. Assume the equation of the line is y = x+1.

Step 1: Translate, so the y intercept becomes origin.
Step 2: Rotate, so that the line y = x+1 becomes the X-axis.
Step 3: Reflect across X-axis.
Step 4: Inverse Rotate.
Step 5: Inverse Translate.


In all the examples so far we have used Geometric transformations where the object is transformed, now in the following examples we will use the Homogeneous Coordinate system and Coordinate Transformations, where the entire coordinate system is moved to do the required transformation.


Homogeneous Coordinate (HC) System
Homogeneous coordinate system is a method to represent all the transformations in the same form. Homogeneous coordinate system adds an extra virtual dimension, so every 2d point is represented as a 3d and a point in 3d is represented as 4d. HC is nothing more than this. This is just a mathematical trick. 

Advantages of using Homogeneous Coordinate system are:
  • All the transformations are represented as a 3X3 matrix.
  • All the transformations can be done only using multiplication.
  • By multiplying various individual transformation matrix, one single composite matrix can be obtained for a sequence of transformations.
Various 2d transformations can be represented in HC as follows.


In homogeneous coordinate system all the 2-d transformation matrix have been transformed into 3X3 matrix, and using these matrix we need to perform only multiplication in order to achieve any kind of transformation.
We will see this with the help of following examples. These examples use coordinate transformations.


Scaling using homogeneous coordinate transformations
Let’s take an example to see how we scale using the homogeneous coordinate system and the coordinate transformations.

A triangle with coordinates A(25,25), B(50,50) and C(75,25), has to be scaled to twice its size, while keeping the point A fixed.
Following steps need to be performed:

Step 1: Translate the Coordinate system so that the origin lies at point A(25,25).
Step 2: Scale the figure to the required size.
Step 3: Inverse translate.

These steps can be performed with the help of following matrix. If we multiply these three matrix we will get one single composite matrix.




Now if we multiply this composite matrix with the point matrix of the triangle, we will get the new coordinates of the triangle after transformation.

So the new coordinates of the triangle are A’=(25,25) B’=(75,75) and C’=(125,25).




Rotation using homogeneous coordinate transformations
Lets again use the same triangle with coordinates A(25,25), B(50,50) and C(75,25). It has to be rotated by 90̊, while keeping the point A fixed.
Following steps need to be performed:
Step 1: Translate the Coordinate system so that the origin lies at point A(25,25).
Step 2: Rotate the figure by the given angle.
Step 3: Inverse translate.

These steps can be performed with the help of following matrix. If we multiply these three matrices we will get one single composite matrix.




Now if we multiply this composite matrix with the point matrix of the triangle, we will get the new coordinates of the triangle after transformation.



So the new coordinates of the triangle are A’=(25,25) B’=(0,50) and C’=(25,75).








No comments:

Post a Comment