πŸ“œ ⬆️ ⬇️

Decomposition of the affine transformation matrix

Not so long ago, in the process of developing a 2D graphics editor, a task arose to decompose the affine transformation matrix on a plane, into a product of simple transformation matrices in order to display them to the user and offer some more or less adequate interpretation of what happened to the object on the canvas. To be honest, this task caused me some difficulties. I finished university a long time ago, and it was not clear to me whether it was possible in principle, given that the original matrix could be the result of an arbitrary sequence of shifts, scales, turns, and transfers, with each transformation having its own arbitrary center. And, secondly, it was not clear how to find the seven parameters, having only six matrix coefficients. The key to solving this problem was the article "Decomposition of the centro-affine transformation matrix for image normalization" ΒΉ , in which the same problem is considered, but without taking into account the transfer transformation and for transformations relative to the coordinate center. Then I actually just adapt the results of this article to the transfer and for an arbitrary center of transformation.

So, let the matrix defining an arbitrary transformation on a plane be:

β”Œ ┐ β”‚ a11 a12 0 β”‚ M = β”‚ a21 a22 0 β”‚. β”‚ a31 a32 1 β”‚ β”” β”˜ 

Its determinant
')
 det = a11β‹…a22 - a12β‹…a21, det β‰  0. 

Let a point on a plane be given by a row vector of the form (x, y, 1), and its transformation by a multiplication to the right of the transformation matrix:

  β”Œ ┐ β”Œ ┐ β”‚ a11 a12 0 β”‚ p1 = p0 β€’ M = β”‚ x0 y0 1 β”‚ β€’ β”‚ a21 a22 0 β”‚ β”” β”˜ β”‚ a31 a32 1 β”‚ β”” β”˜ 

The article mentioned above states that an arbitrary matrix M of a centro-affine transformation can be represented as a product of rotation matrices R, a shift matrix Hx along the X axis, and a scaling matrix S:

 Mc = R β€’ Hx β€’ S 

Here

  β”Œ ┐ β”‚ cos(Ξ±) sin(Ξ±) 0 β”‚ R = β”‚ -sin(Ξ±) cos(Ξ±) 0 β”‚, β”‚ 0 0 1 β”‚ β”” β”˜ β”Œ ┐ β”‚ 1 hx 0 β”‚ Hx = β”‚ 0 1 0 β”‚, β”‚ 0 0 1 β”‚ β”” β”˜ β”Œ ┐ β”‚ sx 0 0 β”‚ S = β”‚ 0 sy 0 β”‚, β”‚ 0 0 1 β”‚ β”” β”˜ 

When decomposing an arbitrary affine transformation matrix, it is necessary to bring the transformation center to the center of coordinates, and also consider the transfer transformation:

 M = T0 β€’ S β€’ H β€’ R β€’ T0⁻¹ β€’ T, β”Œ ┐ β”‚ 1 0 0 β”‚ T0 = β”‚ 0 1 0 β”‚, β”‚ -tx0 -ty0 1 β”‚ β”” β”˜ β”Œ ┐ β”‚ 1 0 0 β”‚ T0⁻¹ = β”‚ 0 1 0 β”‚, β”‚ tx0 ty0 1 β”‚ β”” β”˜ β”Œ ┐ β”‚ 1 0 0 β”‚ T = β”‚ 0 1 0 β”‚. β”‚ tx ty 1 β”‚ β”” β”˜ 

Substituting the expression for the matrix of simple transformations, we obtain the following expressions for the coefficients M:

 a11 = sxβ‹…cos(Ξ±) - sxβ‹…hxβ‹…sin(Ξ±) a12 = sxβ‹…hxβ‹…cos(Ξ±) + sxβ‹…sin(Ξ±) a21 = -syβ‹…sin(Ξ±) a22 = syβ‹…cos(Ξ±) a31 = tx + tx0β‹…(1 - (sxβ‹…cos(Ξ±) - sxβ‹…hxβ‹…sin(Ξ±))) + ty0β‹…syβ‹…sin(Ξ±) = tx + tx0β‹…(1 - a11) - ty0β‹…a21 a32 = ty + ty0β‹…(1 - cos(Ξ±)β‹…sy) - tx0β‹…(sxβ‹…hxβ‹…cos(Ξ±) + sxβ‹…sin(Ξ±)) = ty + ty0β‹…(1 - a22) - tx0β‹…a12 

Solving these equations for sx, sy, Ξ±, hx, tx, ty, after some simplifications, we obtain expressions for the desired parameters:

 if a22=0 Ξ± = Ο€/2, sy = -a21 else Ξ± = atan(-a21/a22), sy = a22/cos(Ξ±), sx = det(M)/sy, hx = (a11β‹…a21 + a12β‹…a22)/det, tx = a31 + ty0β‹…a21 + tx0β‹…(a11 - 1), ty = a32 + tx0β‹…a12 + ty0β‹…(a22 - 1). 

The expressions for Ξ±, sx, sy, hx are similar in the article ΒΉ , although they differ somewhat from them in form. In addition, we have obtained formulas for calculating the parameters of the transfer transformation tx and ty. I would also like to note that even if there were shifts along both axes in the original sequence, only a shift along one of the axes is enough in the decomposition (here, along the X axis). In addition, since the angle of rotation is defined as the result of the function of the arctangent, it is fundamentally limited to values ​​from -90˚ to + 90˚. Considering also that the rotation angle of 180 corresponds to sx = -1 and sx = -1, we have here some ambiguity. For example, initially having a turn of 120˚ when decomposing according to this algorithm, we get -60˚ and sx = sy = -1.



ΒΉ) Putiatin EP, Yakovleva E.V., Lyubchenko V.A. "Decomposition of the centroaffine transformation matrix for image normalization"

Source: https://habr.com/ru/post/278597/


All Articles