[Math] Decompose affine transformation (including shear in x and y)

affine-geometrymatrix decomposition

How can I fully decompose an affine transformation matrix that includes tx, ty, rotation (theta), scale-x (sx), scale-y (sy), shear-x and shear-y?

Using this matrix as example:

$$A = \begin{pmatrix}a & b & t_x\\c & d & t_y\\0 & 0 & 1\end{pmatrix}$$

I have seen similar questions in here, but they all limit the decomposition to a one value of skew/shear only (eg. Decomposition of a nonsquare affine matrix).

In this case, my parameters a-b-c-d contain rotation, scaling and shear, so I assume that no even rotation is as easy as an arctangent, given that:

a = sx * cos(theta)
b = sx * (-sin(theta)) * shear-x
c = sy * sin(theta) * shear-y
d = sy * cos(theta)

Therefore, typical equations that assume that shear-x and shear-y are equal, or that assume that one of them is zero are not used. Am I rigt?

In this general case with all four transformations, how can one find the parameters of each transformation?

Best Answer

The difficulty here is non-uniqueness.

Consider the two shear matrices (I'm going to use $2 \times 2$ to make typing easier; the translation part's easy to deal with in general, and then we just have the upper-left $2 \times 2$ anyhow): $$ A = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, B = \begin{bmatrix} 1 & 0 \\ -0.5 & 1 \end{bmatrix} $$ Their product is $$ AB = \begin{bmatrix} 0.5 & 1 \\ -0.5 & 1 \end{bmatrix} $$ That's exactly the same thing as scaling by $\sqrt{2}$ in $x$ and by $\frac{\sqrt{2}}{2}$ in $y$, and then rotating by 45 degrees.

So if I gave you the matrix $$ \begin{bmatrix} 0.5 & 1 \\ -0.5 & 1 \end{bmatrix}, $$ which answer would you want? The two shears, or the scale and rotation?

Why does this happen? Because you have five free parameters (rotation, 2 scales, 2 shears) and a four-dimensional set of matrices (all possible $2 \times 2$ matrices in the upper-left corner of your transformation). A continuous map from the first onto the second will necessarily be many-to-one.

In short: I think you need to ask a different question.

One possibility is to say "scale has to be the same in $x$ and $y$", or, perhaps worded better, "we only allow uniform scaling". Then there's generally a unique solution, although there are some bad cases: rotation-by-180 degrees and scaling-by-negative-one yield the same matrix, for instance.