Someone help me understand matrix transformations

Rakehellion

Lifer
Jan 15, 2013
12,181
35
91
https://developer.apple.com/library...ce/CGAffineTransform/Reference/reference.html

KPFtEII.png


tx and ty are translation. I think a and d are scale and b and c are some function of rotation.

I have videos that are flipped and rotated using arbitrary matrices. How can I make them right-side-up again?

One example is [a: 1.0, b: 0.0, c: 0.0, d: -1.0, tx: 0.0, ty: 1080.0]

It has a Y scale of -1, which I believe moves it offscreen, then it's translated 1080 pixels. So the end result is that it's flipped along the Y axis, right?

Is there a formula I can use so I don't have to treat every matrix as a special case?

A piece of sample code I have uses atan2(m.b, m.a) to find the rotation. Won't this be incorrect if the image is flipped?
 

Exophase

Diamond Member
Apr 19, 2012
4,439
9
81
So you input a vector (x, y) and the output is:

x' = (x * a) + (y * c) + 1
y' = (x * b) + (y * d) + 1

In the example you gave, y is being negated. So yeah, that will flip it along the vertical axis if (0, 0) is in the middle of the image.

What you want to do to find the matrix to undo the transformations is compute its inverse. If you know what the matrix is you can use an online calculator like Wolfram Alpha. A simple example: http://www.wolframalpha.com/input/?i=inverse%28{{0%2C+1}%2C+{-1%2C+0}}%29

If you want a generalized formula for 3x3 matrices it's here: http://www.dr-lex.be/random/matrix_inv.html Since these are affine transformation matrices with the third column [0 0 1] you can simplify this formula a lot.