12

I have a $2\times 3$ affine matrix $$ M = \pmatrix{a &b &c\\ d &e &f} $$ which transforms a point $(x,y)$ into $x' = a x + by + c, y' = d x + e y + f$

Is there a way to decompose such matrix into shear, rotation, translation,and scale ? I know there's something for $4\times 4$ matrixes, but for a $2\times 3$ matrix ?

tagomago
  • 141
  • 1
    You can use the natural embedding of your 2x3 matrices in 4x4 matrices and apply the algorithms you know. – Listing Nov 02 '11 at 10:02
  • What's "natural embedding" ? Sorry if it's something trivial, but I'm not a mathematician. I was also thinking that ideally the affine transform should be something like M = A * p + T, where T is the translation vector simply given by c and f. So maybe I should "simply" decompose A (a 2x2 matrix) ? – tagomago Nov 02 '11 at 10:38
  • @tagomago the natorual embedding is given by using homogeneous coordinates as used in the answer. That is a nice trick to transform linear functions in n dimensions to affine functions in n+1 dimensions. – lumbric May 18 '15 at 14:07

2 Answers2

21

You've written this somewhat unorthodoxly. To use that matrix for that transformation, one would more usually write

$$\pmatrix{x'\\y'\\1}=\pmatrix{a&b&c\\d&e&f\\0&0&1}\pmatrix{x\\y\\1}\;.$$

So the difference between a $2\times3$ matrix and a $4\times4$ matrix was only from your way of writing it; this works the same way as an affine transform in three dimensions, just with one fewer dimension. You can immediately factor out the translation,

$$\pmatrix{x'\\y'\\1}=\pmatrix{1&0&c\\0&1&f\\0&0&1}\pmatrix{a&b&0\\d&e&0\\0&0&1}\pmatrix{x\\y\\1}\;.$$

Then you just have to decompose $\pmatrix{a&b\\d&e}$ into shear, rotation and scaling in two dimensions.

[Edit in response to the comment:]

This isn't a unique decomposition, since you can do the shear, rotation and scaling in any order. Here's the decomposition I use:

$$A=\pmatrix{a&b\\d&e}=\pmatrix{p\\&r}\pmatrix{1\\q&1}\pmatrix{\cos\phi&\sin\phi\\-\sin\phi&\cos\phi}$$

with

$$ \begin{eqnarray} p&=&\sqrt{a^2+b^2}\;,\\ r&=&\frac{\det A}p=\frac{ae-bd}{\sqrt{a^2+b^2}}\;,\\ q&=&\frac{ad+be}{\det A}=\frac{ad+be}{ae-bd}\;,\\ \phi&=&\operatorname{atan}(b,a)\;, \end{eqnarray} $$

where $\operatorname{atan}$ is the two-argument arctangent function with operand order as in Java. This of course assumes $p\ne0$.

joriki
  • 238,052
  • Thanks to both for the complete decomposition – tagomago Nov 02 '11 at 15:00
  • The matrix $A$ needs to be invertible. That means that $det A \neq 0$ and $p \neq 0$. If $p = 0$ we had $a = 0$ and $b=0$ and therefore A not invertible. – lumbric Apr 25 '15 at 19:52
  • Can you give/suggest a reference for this tecqnique? – acs Oct 07 '15 at 14:03
  • @acs: I can't, unfortunately. I seem to remember that there's something about this in either the PostScript or PDF reference manual, but I might be wrong. – joriki Oct 07 '15 at 16:18
  • @acs: There are a whole lot of equations in that article. Which equations are you interested in, and which equations should they be compatible with, in what sense? – joriki Feb 07 '16 at 14:59
  • @acs: I don't see a well-defined question there. If you tell me specifically which equation in the article you're comparing with which equation in my answer, and why you'd expect them to be compatible or not, and what it would mean for them to be compatible, I'll be happy to answer. – joriki Feb 07 '16 at 15:20
  • Ok. I withdraw my comments. Please can you specify the names of the variables. Which is translation, which is rotation etc. – acs Feb 07 '16 at 15:49
  • 1
    @acs: $e$ and $f$ are translation, $p$ and $r$ are scaling, $q$ is shear and $\phi$ is rotation. – joriki Feb 09 '16 at 09:47
  • @joriki that should be $c$ and $f$ for translation. – Drew Cummins Oct 21 '16 at 15:13
  • @DrewCummins: True, thanks. – joriki Jun 25 '18 at 11:43
  • Is there a 3x3 or in general nxn version of this shear-rotation-scaling decomposition? – Mike Battaglia Jun 09 '19 at 04:10
9

If $(x, y, 1)$ is a vector in homogeneous coordinates, we have, by decomposing $M$ into blocks, that

$$M \left[\begin{array}{c}x\\y\\1\end{array}\right] = \left[\begin{array}{cc} a& b\\ d&e\end{array}\right]\left[\begin{array}{c}x\\y\end{array}\right] + \left[\begin{array}{c}c\\f\end{array}\right].$$

Here $(c,f)$ is the translation component. We can decompose the 2x2 matrix into a composition of a rotation, shear, and scale by using the QR decomposition:

$$\begin{align*}\left[\begin{array}{cc}a & b\\d & e\end{array}\right] &= \left[\begin{array}{cc} \cos \theta &-\sin \theta \\ \sin\theta &\cos \theta\end{array}\right]\left[\begin{array}{cc} \sqrt{a^2+d^2} & b\cos \theta + e\sin \theta\\0 & e\cos \theta - b\sin \theta\end{array}\right]\\ &=\left[\begin{array}{cc} \cos \theta &-\sin \theta \\ \sin\theta &\cos \theta\end{array}\right]\left[\begin{array}{cc}1 & \frac{b\cos \theta + e\sin\theta}{e\cos \theta-b\sin\theta}\\0 & 1\end{array}\right]\left[\begin{array}{cc}\sqrt{a^2+d^2} & 0\\0 & e\cos\theta - b\sin\theta\end{array}\right],\end{align*}$$ where $\theta = \arctan\left(\frac{d}{a}\right).$

user7530
  • 49,280
  • 2
    Can you explain why and how the decompostion ends up in a rotation, shear and scale matrix? I recognise the rotation matrix but am not familiar with the others and also dont see why QR decomposition does this. And would this easily be extendable to more than 2 dimensions? – Leo Jan 26 '15 at 10:25
  • 1
    QR decomposition basically gives you a product of a rotation matrix and an upper triangle matrix. Since a rotation matrix is of the form $[cos\theta, -sin\theta; sin\theta, cos\theta]$ you know the other one must be the proposed one in order for their product to give you your initial $[a, b; c, d]$ matrix. Then you can simply decompose the upper triangle into a diagonal matrix (scale matrix) and upper triangle with one value on diagonal (shear matrix). Again you can make sure their product gives you the initial upper triangle matrix. – mocquin Feb 08 '21 at 19:43