Authors Top

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

A rotational matrix, a type of matrix used in image processing, can rotate an image by a particular angle.

In this tutorial, we’ll show how to determine the rotation angle from a rotation matrix. We’ll start with the rotation matrix for an angle, and we’ll learn how to calculate that angle.

2. Image Transformations

Transformation is a function that, after executing various operations, maps one set to another set. In computer graphics, transformations are crucial for resizing, rotating, and repositioning the graphics on the screen.

Transforming involves using rules to turn certain graphics into something else. We refer to the transformation as a 2D transformation occurring on a 2D plane. Transformations come in various forms, including translation, rotation, scaling up or down, and shearing.

2.1. Translation

A translation changes the location of an object on the screen. We can translate a point by including a translation vector with the original coordinates:

\\ X' = X + tx\\ Y' = Y + ty

2.2. Scaling

We use the scaling transformation to alter an object’s size. We either increase or decrease the object’s dimensions throughout the scaling procedure. We can scale an object by multiplying its original coordinates by the scaling factor:

\\ X' = X . S_x\\ Y' = Y . S_y

2.3. Rotation

When rotating an object, we rotate it around the coordinate system’s origin to a specific degree:

X'= X \cos {\theta} - Y \sin {\theta}
Y'= X \sin {\theta} + Y \cos {\theta}

2.4. Shearing

The shear transformation is a transformation that tilts an object’s shape. Skewing is another name for shearing transformation. X-Shear and Y-Shear are the two kinds of shear that can be applied to an image. X-Shear is shear applied only along the X axis:

\\ X' = X + {Sh}_{x} . Y\\ Y' = Y\\
Y-Shear is shear applied only along the Y axis:

\\ X' = X\\ Y' = Y + {Sh}_{y} . X

One may apply shear to the X or Y coordinates independently or simultaneously to both X and Y coordinates:
\\ X' = X + {Sh}_{x} . Y\\ Y' = Y + {Sh}_{y} . X

3. Image Transformation Matrices

An N \times N square matrix serves as a common type of N-dimensional transformation matrix. We can represent any transformation performed on a pixel by a two-dimensional transformation matrix, since the coordinates in a picture are only two-dimensional.

3.1. Matrix Representation for Image Coordinates

It is common to represent image coordinates in a 2\times1 matrix. For the current tutorial, we will represent a coordinate as follows:
\\\\ {Coordinate} = \begin{bmatrix} X\\ Y \end{bmatrix} \\
After applying the transformation, we move the same coordinate to the following coordinate:
\\\\ {New Coordinate} = {Coordinate'} = \begin{bmatrix} X'\\ Y' \end{bmatrix} \\
Note that in this context, movement is the act of moving a particular pixel data (color and intensity) from one to another position in the image.

3.2. Matrix Representation for Image Transformation

If we subject a pixel to multiple transformations, the result of the previous transformation must serve as the input for the subsequent transformation:
\\Coordinate' = {Transformation} . Coordinate\\

Concatenating multiple transformation matrices results in a combined transformation matrix, which we can use to perform a composite transformation.

3.3. Matrix Representation for Homogeneous Image Coordinates

Instead of using a two-dimensional transformation matrix, we must utilize a three-dimensional transformation matrix to speed up the process of conducting a sequence of transformations. To the two-dimensional pixel coordinate for this, we add a dummy coordinate:

\\\\ {Coordinate} \begin{bmatrix} X\\ Y \end{bmatrix} \equiv {Homogeneous Coordinate} \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix} \\

By doing so, we can represent a pixel with three numbers rather than two, a technique known as homogeneous coordinates. In this system, we can use matrix multiplication to describe all transformation equations.

3.4. Matrix Representation for Image Transformation

We can use a single transformation T_{ab} that is the composition of T_{a} and T_{b} taken in that order to express the result itself, if a plane transformation T_{a} is followed by a second plane transformation T_{b}. We write it as:

\\\\ T_{ab} \equiv T_{a}.T_{b}\\ \{Coordinate' = {T_{a}} . {T_{b}} . Coordinate\} \equiv \{Coordinate' = {T_{ab}} . Coordinate\}\\

4. Affine Image Transformation Matrices

A linear mapping technique that preserves planes, straight lines, and points is known as an affine translation. An affine transformation, also known as an affinity, is a geometric transformation in Euclidean geometry that generally retains parallelism and lines but not always distances and angles.

A linear transform and a translation vector make up an affine transform. For the affine transformations mentioned above, we give transformation matrices in this section.

4.1. Translation Transformation

We can express the image transformation described in earlier sections in matrix form as follows:
\\\\ {Translation Matrix} \equiv T = \begin{bmatrix} tx\\ ty \end{bmatrix}\\

\\{Translation Transformation} \equiv \{Coordinate' = Coordinate + T\}

\equiv \{ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} X \\ Y \end{bmatrix} + \begin{bmatrix} tx\\ ty \end{bmatrix} \}\\
The usual translation matrix in a homogeneous coordinate system is shown below:

\\\\ {Homogeneous Translation Matrix} \equiv \begin{bmatrix} 1 & 0 & tx\\ 0 & 1 & ty\\ 0 & 0 & 1 \end{bmatrix}\\

4.2. Scaling Transformation

We can express the scaling transformation described in earlier sections in matrix form as follows:

\\\\ {Scaling Matrix} \equiv S = \begin{bmatrix} S_x & 0\\ 0 & S_y \end{bmatrix}\\

{Scaling Transformation} \equiv \{Coordinate' = Coordinate . S\}

\equiv \{ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} X \\ Y \end{bmatrix} % \begin{bmatrix} S_x & 0\\ 0 & S_y \end{bmatrix} \}\\
The usual scaling matrix in a homogeneous coordinate system is shown below:

\\\\ {Homogeneous Scaling Matrix} \equiv \begin{bmatrix} S_x & 0 & 0\\ 0 & S_y & 0\\ 0 & 0 & 1 \end{bmatrix}\\

4.3. Rotation Transformation

We can express the rotation transformation described in earlier sections in matrix form as follows:

\\\\ {Rotation Matrix} \equiv R = \begin{bmatrix} \cos {\theta} & \sin {\theta}\\ -\sin {\theta} & \cos {\theta} \end{bmatrix}\\

{Rotation Transformation} \equiv \{Coordinate' = Coordinate . R\}

\equiv \{ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} X \\ Y \end{bmatrix} % \begin{bmatrix} \cos {\theta} & \sin {\theta}\\ -\sin {\theta} & \cos {\theta} \end{bmatrix} \} \\
The rotation matrix in a homogeneous coordinate system is shown below:

\\\\ {Homogeneous Rotation Matrix} \equiv \begin{bmatrix} \cos {\theta} & \sin {\theta} & 0\\ -\sin {\theta} & \cos {\theta} & 0\\ 0 & 0 & 1 \end{bmatrix}\\

4.4. Shearing Transformation

We can express the shearing transformation described in earlier sections in matrix form as follows:

\\\\ {Shearing Matrix}_{x} \equiv {SH}_{x} = \begin{bmatrix} 1 & {Sh}_{x}\\ 0 & 1 \end{bmatrix}\\
\\ {Shearing Matrix}_{y} \equiv {SH}_{y} = \begin{bmatrix} 1 & 0\\ {Sh}_{y} & 1 \end{bmatrix}\\

{Shearing Transformation}_{x} \equiv \{Coordinate' = Coordinate . {SH}_{x}\}
\equiv \{ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} X \\ Y \end{bmatrix} % \begin{bmatrix} 1 & {Sh}_{x}\\ 0 & 1 \end{bmatrix} \}\\

{Shearing Transformation}_{y} \equiv \{Coordinate' = Coordinate . {SH}_{y}\}

\equiv \{ \begin{bmatrix} X'' \\ Y" \end{bmatrix} = \begin{bmatrix} X' \\ Y' \end{bmatrix} % \begin{bmatrix} 1 & 0\\ {Sh}_{y} & 1 \end{bmatrix} \}\\

\\ {Shearing Matrix} \equiv {SH} = \begin{bmatrix} 1 & {Sh}_{x}\\ {Sh}_{y} & 1 \end{bmatrix}\\

{Shearing Transformation} \equiv \{Coordinate' = {SH} . Coordinate\}

\equiv \{ \begin{bmatrix} X' \\ Y' \end{bmatrix} = \begin{bmatrix} 1 & {Sh}_{x}\\ {Sh}_{y} & 1 \end{bmatrix} % \begin{bmatrix} X \\ Y \end{bmatrix} \}\\

The shearing matrix in a homogeneous coordinate system is shown below:

\\\\ {Homogeneous Shearing Matrix} \equiv \begin{bmatrix} 1 & {Sh}_{x} & 0\\ {Sh}_{x} & 1 & 0\\ 0 & 0 & 1 \end{bmatrix}\\

5. Homogeneous Matrix Composition

Translations become linear when using transformation matrices with homogeneous coordinates, making it possible to combine them easily with all other kinds of transformations:

\\\\ {Homogeneous Translation} \equiv \begin{bmatrix} X'\\ Y'\\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & tx\\ 0 & 1 & ty\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix}\\

\\\\ {Homogeneous Scaling} \equiv \begin{bmatrix} X'\\ Y'\\ 1 \end{bmatrix} = \begin{bmatrix} S_x & 0 & 0\\ 0 & S_y & 0\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix}\\

\\\\ {Homogeneous Rotation} \equiv \begin{bmatrix} X'\\ Y'\\ 1 \end{bmatrix} = \begin{bmatrix} \cos {\theta} & \sin {\theta} & 0\\ -\sin {\theta} & \cos {\theta} & 0\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix}\\

\\\\ {Homogeneous Shearing} \equiv \begin{bmatrix} X'\\ Y'\\ 1 \end{bmatrix} = \begin{bmatrix} 1 & {Sh}_{x} & 0\\ {Sh}_{x} & 1 & 0\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix}\\

Since these are homogeneous transformations, The combined transformations can be written as follows:

\\\\ \begin{bmatrix} a & b & c\\ d & e & f\\ 0 & 0 & 1 \end{bmatrix} \equiv T_{Affine} = [T] . [S] . [R] . [{SH}]\\

\\\\ \begin{bmatrix} a & b & c\\ d & e & f\\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & tx\\ 0 & 1 & ty\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} S_x & 0 & 0\\ 0 & S_y & 0\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} \cos {\theta} & \sin {\theta} & 0\\ -\sin {\theta} & \cos {\theta} & 0\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} 1 & {Sh}_{x} & 0\\ {Sh}_{x} & 1 & 0\\ 0 & 0 & 1 \end{bmatrix}\\

\\\\ {Combined Homogeneous Transformation} \equiv \begin{bmatrix} X'\\ Y'\\ 1 \end{bmatrix} = \begin{bmatrix} a & b & c\\ d & e & f\\ 0 & 0 & 1 \end{bmatrix} % \begin{bmatrix} X\\ Y\\ 1 \end{bmatrix}\\

6. Euler Angles From a Rotation Matrix

We begin by defining rotations about the three principal axes, following a customary practice. Three distinct rotations along the three spatial dimensions can be thought of as a three-dimensional rotation.

6.1. Multi-Axis Rotation Matrices

As an example, consider a rotation of p radians about the x-axis. Euler’s homogeneous rotation matrix for p radians about x axis:
\\\\ \begin{bmatrix} 1 & 0 & 0\\ 0 & \cos {p} & -\sin {p}\\ 0 & \sin {p} & \cos {p}\\ \end{bmatrix}\\
Similar to this, we can define rotation of q radians about the y-axis. Euler’s homogeneous rotation matrix for q radians about y axis:
\\\\ \begin{bmatrix} \cos {q} & 0 & \sin {q}\\ 0 & 1 & 0\\ -\sin {q} & 0 & \cos {q}\\ \end{bmatrix}\\
Similarly, the definition of a rotation of r radians about the z-axis is defined. Euler’s homogeneous rotation matrix for r radians about z axis:
\\\\ \begin{bmatrix} \cos {r} & \sin {r} & 0\\ -\sin {r} & \cos {r} & 0\\ 0 & 0 & 1 \end{bmatrix}\\

6.2. Generalized Rotation Matrices

A general rotation matrix (Euler’s Generalized Homogeneous Rotation Matrix) can have the following form:
\\\\ {Euler's\; Generalized\; Homogeneous\; Rotation\; Matrix} \equiv R = \begin{bmatrix} R_{11} & R_{12} & R_{13}\\ R_{21} & R_{22} & R_{23}\\ R_{31} & R_{32} & R_{33} \end{bmatrix}\\
We can think of this matrix as a series of three rotations, one around each main axis. The output will vary depending on the order of the rotation axes, as matrix multiplication does not commute. We’ll rotate the x-axis first, followed by the y-axis, and then the z-axis.

We can use the matrix product to depict such a series of rotations:

\\\\R = R_z(r)R_y(q)R_x(p)\\ = \begin{bmatrix} \cos {q} \cos {r} & \sin {p} \sin {q} \cos {r} - \cos {p} \sin {r} & \cos {p} \sin {q} \cos {r} + \sin {p} \sin q\\ \cos {q} \sin {r} & \sin {p} \sin {q} \sin {r} + \cos {p} \cos {r} & \cos {p} \sin {q} \sin {r} - \sin {p} \cos q\\ - \sin {q} & \sin {p} \cos {q} & \cos {p} \cos r \end{bmatrix}\\

6.3. Euler Angles From a Rotation Matrix

By equating each member of a rotation matrix R with its corresponding element in the matrix product R_z(r)R_y(q)R_x(p), we may get the Euler angles, p, q, and r. We generate nine equations as a result, which we can use to calculate the Euler angles.

The following is the pseudocode for calculating Euler angles from a rotation matrix (2aT: 2-Argument-Arctangent):

Rendered by QuickLaTeX.com

7. Euler Angle in Image Rotation

Now that we have the pseudocode and the previously discussed ideas, we can determine the rotational angle of a picture from its rotation matrix.

7.1. The Special Case of Image Rotation

Considering that the image’s location is an xy plane, Any image rotation involves a rotation along the z-axis. The definition of a rotation of r radians about the z-axis is as previously stated. Euler’s homogeneous rotation matrix for r radians about z axis:
\\\\ \begin{bmatrix} \cos {r} & \sin {r} & 0\\ -\sin {r} & \cos {r} & 0\\ 0 & 0 & 1 \end{bmatrix}\\
We’ll now use \theta (in radians) in place of r. The rotation matrix is as follows for an image rotated by an angle \theta:
\\\\ \begin{bmatrix} \cos {\theta} & \sin {\theta} & 0\\ -\sin {\theta} & \cos {\theta} & 0\\ 0 & 0 & 1 \end{bmatrix}\\

For the sake of simplicity, we’ve assumed that the two-dimensional image rotates inside its plane. i.e., the rotation of the image occurs in the image plane rather than in three dimensions. For a more general instance, the picture rotation can be thought of as three distinct rotations using the Euler angles mentioned above.

7.2. Euler Angles From an Image Rotation Matrix

The idea is to use the pseudocode for calculating Euler angles from a rotation matrix given above.
We’ll replace r with \theta, and will use 0 radians for p and q in our calculations.

For a given rotation matrix, we’ll solve for \theta using the pseudocode for calculating Euler angles.

7.3. Direct Method for Angle of Image Rotation

The idea here is to first bring the given rotation matrix in the standard format:
\\\\ \begin{bmatrix} A & B & 0\\ -B & A & 0\\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} \cos {\theta} & \sin {\theta} & 0\\ -\sin {\theta} & \cos {\theta} & 0\\ 0 & 0 & 1 \end{bmatrix}\\

Now the angle \theta can be calculated using the following relations:
\\\\ \theta = \arccos {A}\\ \theta = \arcsine {B}\\ \theta = \arctan {\frac{B}{A}}

7.4. More Than One Solution

The fact that more than one rotational sequence around the three main axes might produce the same orientation of an object is an intriguing observation. Thus, we can achieve the desired rotation in a variety of ways.

8. Conclusion

In this article, we learned how to determine the rotational angle of a given rotation matrix. First, we discussed what image transformations are and how transformation matrices are used to transform the pixels in an image. We were able to estimate the rotation’s angle by examining the rotation matrix’s parts after figuring out its template formula.

 

Authors Bottom

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!