= How to calculate "real" parameters from affine transformation = == Introduction == This page discusses how to compute conceptually meaningful parameters (i.e., rotation, scale in the x and y direction) when all you have is an affine transformation. The reverse process, calculating the affine parameters from the conceptually meaningful data, is articulated on [wiki:DevWikiAffineParameters another wiki page]. As described on the page which discusses calculation of affine transform parameters, the value of each affine coefficient is affected by which operations the transformation performs, as well as the order in which the transform performs the operations. In essense, this means that we have to make an assumption about which operations are performed by the transform and hope that we made the right guess. == The forward calculation == On the wiki page describing [wiki:DevWikiAffineParameters the reverse operation], a transform is developed which supports: 1. scaling 1. clockwise rotation around the origin 1. shearing parallel to the x axis 1. shearing parallel to the y axis For the sake of consistency, we will use this same model and attempt to calculate the conceptually meaningful parameters from the transform coefficients. The above model produced the following equations: * a,,11,, = o,,11,, = s,,x,, ( (1 + k,,x,, k,,y,,) cosθ + k,,y,, sinθ ) * a,,12,, = o,,12,, = s,,x,, ( k,,x,, cosθ + sinθ ) * a,,13,, = t,,x,, * a,,21,, = o,,21,, = s,,y,, ( -(1 + k,,x,, k,,y,,) sinθ + k,,y,, cosθ ) * a,,22,, = o,,22,, = s,,y,, ( - k,,x,, sinθ + cosθ ) * a,,23,, = t,,y,, where: * s,,x,, : scale factor in x direction * s,,y,, : scale factor in y direction * t,,x,, : offset in x direction * t,,y,, : offset in y direction * θ : angle of rotation clockwise around origin * k,,x,, : shearing parallel to x axis * k,,y,, : shearing parallel to y axis == Solving for the meaningful parameters == The objective of this page is to find a way to determine values for s,,x,,, s,,y,,, t,,x,,, t,,y,,, θ, k,,x,,, and k,,y,, if all we know is a,,11,,,a,,12,,, ... a,,23,,. Clearly, t,,x,, and t,,y,, are trivial cases, because we can just use the values for a,,13,, and a,,23,, respectively. This leaves us with these four equations: * a,,11,, = o,,11,, = s,,x,, ( (1 + k,,x,, k,,y,,) cosθ + k,,y,, sinθ ) * a,,12,, = o,,12,, = s,,x,, ( k,,x,, cosθ + sinθ ) * a,,21,, = o,,21,, = s,,y,, ( -(1 + k,,x,, k,,y,,) sinθ + k,,y,, cosθ ) * a,,22,, = o,,22,, = s,,y,, ( - k,,x,, sinθ + cosθ ) Unfortunately, these four equations contain five unknowns: s,,x,,, s,,y,,, θ, k,,x,,, and k,,y,,. This represents an ill-posed problem and forces us to simplify. Let's assume that there is no shearing. We are just going to declare upfront that k,,x,,=0 and k,,y,,=0. Remember that we did that because if we're wrong it will screw everything up. This makes the above into: * a,,11,, = o,,11,, = s,,x,, cosθ * a,,12,, = o,,12,, = s,,x,, sinθ * a,,21,, = o,,21,, = - s,,y,, sinθ * a,,22,, = o,,22,, = s,,y,, cosθ Now we're on a roll. We can knock out s,,x,, and s,,y,, very easily: * a,,11,,^2^ + a,,12,,^2^ * = s,,x,,^2^ cos^2^θ + s,,x,,^2^ sin^2^θ * = s,,x,,^2^ (cos^2^θ + sin^2^θ) * = s,,x,,^2^ (1) * a,,21,,^2^ + a,,22,,^2^ * = (- s,,y,,)^2^ sin^2^θ + s,,y,,^2^ cos^2^θ * = s,,y,,^2^ (sin^2^θ + cos^2^θ) * = s,,y,,^2^ (1) Rewriting as a "final result", this gives: * s,,x,,^2^ = a,,11,,^2^ + a,,12,,^2^ * s,,y,,^2^ = a,,21,,^2^ + a,,22,,^2^ == Checking against wikipedia == Checking this against the wikipedia page on the [http://en.wikipedia.org/wiki/World_file world file], which lists: * "pixel width" = s,,x,, = sqrt(A^2^ + D^2^) = sqrt(a,,11,,^2^ + a,,21,,^2^) * "pixel height" = s,,y,, = sqrt(B^2^ + E^2^) = sqrt(a,,12,,^2^ + a,,22,,^2^) Clearly, this does not match the answer on our page. The three possibilities are: we made a mistake; they made a mistake; or no one made a mistake, but we're using different models... The world file page does not declare it's assumptions about what operations are performed, or which order they are performed in. The world file page, however, also defines it's parameter A (for us: a,,11,,) as "pixel size in x direction". '''NOTE: If the order of the model operations changes, in particular if scaling and rotation are swapped, then these pages match wikipedia.''' However, the math on wikipedia is unsourced. Since it has become clear that order is vital, and it is by no means guaranteed that all software performs uses the same model, it is probably vital to go out and determine which software performs the calculations which way before we do anything else.