[Math] Calculating translation value and rotation angle of a rotated 2D image using MATLAB

image processingMATLAB

I have two images which one of them is the Original image and the second one is Transformed image.

I have to find out how many degrees Transformed image was rotated using 3×3 transformation matrix. Plus, I need to find how far translated from origin.

Both images are grayscaled and held in matrix variables. Their sizes are same [350 500].

I have found a few lecture notes like this.

Lecture notes say that I should use the following matrix formula for rotation:
Rotation Formula

For translation matrix the formula is given:

Translation Formula

Everything is good. But there are two problems:

  1. I could not imagine how to implement the formulas using MATLAB.
  2. The formulas are shaped to find x',y' values but I already have got x,x',y,y' values. I need to find rotation angle (theta) and tx and ty.

I have got the following code:

rotationMatrix = [   cos(theta) sin(theta) 0 ; ...
                    -sin(theta) cos(theta) 0 ; ...
                             0          0  1];


translationMatrix = [  1  0  tx; ...
                       0  1  ty; ...
                       0  0   1];

But as you can see, tx, ty, theta variables are not defined before used. How can I calculate theta, tx and ty?

PS: It is forbidden to use Image Processing Toolbox functions.

Best Answer

Your image is 2D, so we need only work with 2x2 matrices. Notice how all your other matrices have that 1 floating around? Unnecessary in the 2-D case.

A rotation matrix is a matrix of the form $\begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{pmatrix}$.

A pixel given by $(x,y)$ can be rotated simply by multiplying:

$$\begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{pmatrix}\begin{pmatrix} x \\ y \end{pmatrix}.$$

Multiplying this out, you get

$$\begin{align*} x' &= \cos \theta x - \sin \theta y, \\ y' &= \sin \theta x + \cos \theta y. \end{align*}$$

Even though we have 1 unknown ($\theta$), let's treat it as if we have two: $\alpha = \cos \theta, \beta = \sin \theta$.

This gives us a new linear system:

$$\begin{pmatrix} x & -y \\ y & x\end{pmatrix}\begin{pmatrix} \alpha \\ \beta \end{pmatrix} = \begin{pmatrix} x' \\ y'\end{pmatrix}.$$

Fill in your values $x,y, x', y'$ and solve:

A = [x -y; y x];
b = [x1; y1];
v = A\b;
alpha = v(1);
beta = v(2);

theta = acos(alpha); % or theta = asin(beta)
Related Question