20
$\begingroup$

An affine transformation is composed of rotations, translations, scaling and shearing.

In 2D, such a transformation can be represented using an augmented matrix by $$ \begin{bmatrix} \vec{y} \\ 1 \end{bmatrix} =\begin{bmatrix} A & \vec{b} \ \\ 0, \ldots, 0 & 1 \end{bmatrix} \begin{bmatrix} \vec{x} \\ 1 \end{bmatrix} $$

vector b represents the translation. Bu how can I decompose A into rotation, scaling and shearing? I am trying to "interpolate" an affine transform so that, if I have points in a frame 1 and in a frame 3 such that a transform $T$ takes points from frame 1 to points in frame 3, then the interpolated transform would take my points from frame 1 to frame 2. If, for example, an 60° rotation is needed to go from frame 1 to frame 3, then a 30° one would be used to go from frame 1 to frame 2.

Thank you for your help.

$\endgroup$
3
  • 1
    $\begingroup$ I find this question of interpolation an interesting one, but also difficult. Essentially you'd be asking about “nice” paths in the parameter space of all transformations. But characterizing what is a nice path might be more difficult than it looks at first glance. While your example with rotation only looks easy enough, it might be difficult to distinguish rotational and other effects. Take e.g. a 180° rotation. For interpolation, you can choose either direction, but you could also treat this as a scaling by $-1$, and interpolate that. Can you define which interpretation is correct, and why? $\endgroup$
    – MvG
    Dec 21, 2013 at 9:27
  • 1
    $\begingroup$ What also has me worried is the fact that most shearing definitions have a close relation to the chosen coordinate system. As do anisotropic scalings. I'd concentrate on aspects that do not depend on coordinate system. Diagonalization of the matrix might proove useful here: you could then do a linear interpolation of the diagonal matrix between identity and the final matrix, and with a bit of luck that would result in reasonable geometric behavior. You'd have to be prepared to deal with complex eigenvalues, though. And this is quite a different approach to what your question here asks. $\endgroup$
    – MvG
    Dec 21, 2013 at 9:36
  • $\begingroup$ @PeterSheldrick but quaternions would work only for rotations wouldn't they? $\endgroup$
    – bigTree
    Dec 21, 2013 at 17:49

3 Answers 3

19
$\begingroup$

I am trying to "interpolate" an affine transform […]

I'm suggesting a different approach, which should be well suited to interpolation, although it does not depend on splitting the transformation into separate elementary operations the way your question suggests. Instead, I'd use fractional powers of a matrix, as I'll describe now.

Suppose $A$ is diagonalizable (which will be the case for most transformations), then there exists an orthogonal matrix $P$ and a diagonal matrix $D$ such that $A=P\,D\,P^{-1}$. The entries of $D$ are the eigenvalues of $A$, which I'll call $\lambda_1$ and $\lambda_2$. Now you can define $A$ raised to the $t$-th power like this:

$$ A^t = P\,D^t\,P^{-1} = P\,\begin{pmatrix}\lambda_1^t&0\\0&\lambda_2^t\end{pmatrix}\,P^{-1} $$

Now if you change $t$ continuously from $0$ to $1$, the matrix $A^t$ will change from identity to the matrix $A$. So this is your interpolation.

One thing you have to be careful about is the fact that the $\lambda_k$ will very likely be a conjugate pair of complex number. You can express them as $\lambda_k=e^{z_k}$, where $z_k=\log\lambda_k$, but the logarithm of a number is only defined up to multiples of $2\pi i$. So in this case, you should make sure that the imaginary part doesn't become too big, namely you want $-\pi\le\operatorname{Im}(z_k)\le\pi$. This ensures that the interpolation will not take more turns for a rotation than actually required. Furthermore, you should maintain $z_2=\bar{z_1}$ to make sure that the interpolating matrices will be real as well. With this choice, $\lambda_k^t=e^{t\cdot z_k}$ is well defined and behaves as you described for the case of rotation.

I've created a proof of concept implementation which you can use to experiment, in order to decide whether this is what you want. Here is a snapshot of the kind of interpolation this will create:

Interpolation snapshot

With that experiment, I realized that one should include the translative part into the matrix as well, in just the way you composed a matrix including $A$ and $b$ in your question. Otherwise, the positions of the interpolated frames will depend on the location of the defining triples in the plane, which I consider undesirable.

$\endgroup$
6
  • $\begingroup$ Therefore if we need to include b, the matrices P and D would be 3 by 3 matrices right? (I am currently trying to include your method in the code I have and will let you know when I have results) $\endgroup$
    – bigTree
    Dec 21, 2013 at 19:57
  • $\begingroup$ @bigTree: Yes, they are $3\times3$ matrices. I'll probably edit my post one day to use that version from the start, but not just now. Your code is matlab, right? Reading the docs, it seems mpower should help there, and do most of the work already. If you end up with a working matlab code snippet, it would be nice if you could share it. $\endgroup$
    – MvG
    Dec 21, 2013 at 21:08
  • $\begingroup$ I see the matrix no worries. My code is in matlab. I'll write the code and run some tests to see whether I encounter non diagonalizable matrices and share it. Thanks $\endgroup$
    – bigTree
    Dec 21, 2013 at 21:37
  • $\begingroup$ So I just tried to implement the code. It turns out it doesn't work because many of the matrices I have are singular... $\endgroup$
    – bigTree
    Dec 22, 2013 at 2:59
  • $\begingroup$ @bigTree: I think that this should work even for singular or non-diagonalizable matrices. But I'm not sure, and asked this question for details of matrices raised to fractional powers. $\endgroup$
    – MvG
    Dec 22, 2013 at 14:42
12
$\begingroup$

If you scale, shear, and rotate (in that order) then you'll have:

$$\begin{bmatrix} A_{11} & A_{12} \ \\ A_{21} & A_{22} \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} 1 & m \\ 0 & 1 \end{bmatrix} \begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix}.$$

Then

$$\begin{align*} A_{11} &= s \cos \theta\\ A_{12} &= s m \cos \theta - s \sin \theta\\ A_{21} &= s \sin \theta\\ A_{22} &= s m \sin \theta + s \cos \theta\\ \end{align*} $$

From the first and third equations,

$$s = \sqrt{A_{11}^2 + A_{21}^2},$$

and

$$\theta = \tan^{-1}\left( \frac{A_{21}}{A_{11}} \right).$$

Finally, substituting the first and third into the second and fourth,

$$m = \frac{A_{12} + A_{21}}{A_{11}} = \frac{A_{22} - A_{11}}{A_{21}}.$$

$\endgroup$
5
  • 1
    $\begingroup$ I solved the equations and found the same results as you, but when I tried to test the equality for the matrix coefficients given by m (last substitution) on a real affine transformation matrix, it didn't work (the values are different...) $\endgroup$
    – bigTree
    Dec 19, 2013 at 20:40
  • $\begingroup$ It may depend on how you're doing the transformation. I guessed at the order of operations. I also assumed an $x$ shear rather than a $y$ shear. As well as uniform scaling, now that I look at it again. But if you know specifically how the transformation is made, then just order the matrices right to left and multiply them out. $\endgroup$
    – John
    Dec 19, 2013 at 21:05
  • 2
    $\begingroup$ Your equations are underspecified: you're solving for three variables $(s, m, \theta)$, but there's four parameters. One way to deal with this would be to add a $y$ shear as well. $\endgroup$
    – Steve
    Jun 6, 2015 at 4:16
  • 4
    $\begingroup$ I might miss something here, but affine transformation has scale x and scale y $\endgroup$
    – TripleS
    Dec 21, 2015 at 9:38
  • $\begingroup$ As already mentioned there should be four variables/parameters. One way is to perform a SVD on A this will give us 1. scale x, 2. scale y, 3. the angle under which both scaling have been performed and 4. rotation θ. Of course other decompositions including shear terms are also possible $\endgroup$ Jul 2, 2020 at 17:55
5
$\begingroup$

The answer provided by @John is not correct. It is not true that $\frac{A_{12} + A_{21}}{A_{11}} = \frac{A_{22} - A_{11}}{A_{21}}$ in general.

Start with $$ \begin{bmatrix} A_{11} & A_{12} \ \\ A_{21} & A_{22} \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} 1 & m \\ 0 & 1 \end{bmatrix} \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix}. $$ This gives $$ \begin{align*} A_{11} &= s_x \cos \theta\\ A_{12} &= s_y m \cos \theta - s_y \sin \theta\\ A_{21} &= s_x \sin \theta\\ A_{22} &= s_y m \sin \theta + s_y \cos \theta\\ \end{align*} $$ From the first and third equalities, one gets $$ s_x = \sqrt{A_{11}^2 + A_{21}^2} $$ and $$ \theta = \tan^{-1}\left( \frac{A_{21}}{A_{11}} \right). $$ From the second and fourth equalities, one gets $$ m s_y = A_{12}\cos\theta + A_{22}\sin\theta. $$ So we have $m s_y$.

If $\sin\theta \neq 0$, we get $s_y$ by $$ s_y = \frac{m s_y\cos\theta - A_{12}}{\sin\theta}, $$ otherwise we can get it by $$ s_y = \frac{A_{22} - m s_y\sin\theta}{\cos\theta}. $$ And we finally get $m$ by $m = \dfrac{m s_y}{s_y}$.

$\endgroup$
4
  • 1
    $\begingroup$ Excellent answer. Only mistake is that one must use the arctan2 function (or something that does the same) to find $\theta$ i.e. $\theta = \mathrm{arctan2}(A_{2,1}, A{1,1})$. Otherwise it does not work for $A=\begin{bmatrix}-1&1\\-1&1\end{bmatrix}$ $\endgroup$
    – Atnas
    Oct 3, 2020 at 11:16
  • $\begingroup$ Great answer, but the last few equations do not appear to solve for m and sy separately. It can be done from the equations for A12 and A22, eliminating sy, and solving for m. $\endgroup$ Nov 16, 2021 at 17:11
  • $\begingroup$ @Stéphane Laurent (or anyone else) I've attempted to verify this solution in sympy, but I'm having issues. I posted details in another SE thread: stackoverflow.com/questions/70357473/… Did I make a mistake? $\endgroup$
    – Erotemic
    Dec 15, 2021 at 1:29
  • $\begingroup$ I did make a mistake, this decomposition is correct. I'm editing my post to reflect that. $\endgroup$
    – Erotemic
    Dec 16, 2021 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.