24

Does anyone know of any standard algorithms to determine an affine transformation matrix based upon a set of known points in two co-ordinate systems?

Adam C.
  • 416
  • 1
  • 5
  • 11
  • If I recall from my college days, shouldn't you be able to do this by setting up a set of equations and solving for the transformation? By the way, is this your homework? – WhirlWind May 03 '10 at 01:55
  • Isn't this more a question for http://mathoverflow.net? – BalusC May 03 '10 at 01:55
  • 1
    @WhirlWind No this isn't my homework. I have come up with a solution myself, however it seems a bit "hacky" and doesn't seem to handle some cases very well. I have been searching for a standard algorithm, but it's been unsuccessful so far. I was wondering if anyone here knows of one. @BalusC Is it? Should I ask there instead? Sorry :) I'm a bit new here. – Adam C. May 03 '10 at 02:20
  • 2
    @BalusC: No, it is most definitely not a question for mathoverflow. Read their FAQ: http://mathoverflow.net/faq It's not for homework help, it's for questions about "research-level math...articles or graduate-level books". – Cascabel May 03 '10 at 04:35
  • 1
    @thehawk90: I'm sorry, I didn't mean to imply your question was homework; I know you said it wasn't already. I was just trying to very clearly tell BalusC what kind of questions mathoverflow is for - and anything that might ever be homework (short of math grad school) is definitely not the kind of question they want. Thanks for the explanation, though! – Cascabel May 03 '10 at 06:57

2 Answers2

37

Affine transformations are given by 2x3 matrices. We perform an affine transformation M by taking our 2D input (x y), bumping it up to a 3D vector (x y 1), and then multiplying (on the left) by M.

So if we have three points (x1 y1) (x2 y2) (x3 y3) mapping to (u1 v1) (u2 v2) (u3 v3) then we have

   [x1 x2 x3]   [u1 u2 u3]
M  [y1 y2 y3] = [v1 v2 v3].
   [ 1  1  1]

You can get M simply by multiplying on the right by the inverse of

[x1 x2 x3]
[y1 y2 y3]
[ 1  1  1].

A 2x3 matrix multiplied on the right by a 3x3 matrix gives us the 2x3 we want. (You don't actually need the full inverse, but if matrix inverse is available it's easy to use.)

Easily adapted to other dimensions. If you have more than 3 points you may want a least squares best fit. You'll have to ask again for that, but it's a little harder.

sigfpe
  • 7,996
  • 2
  • 27
  • 48
  • What about the translation ? – jeff Jun 06 '14 at 17:36
  • 1
    The last column of M gives the translation. You can read M as a linear map (given by the 2x2 matrix on the left) followed by translation by the last column. But you don't have to think of it as two separate transforms. To transform a 2D point you append a 1 to make it a 3D points, and then multiply by M. The result is a 2D vector with both linear and translation parts applied in one go. – sigfpe Jun 06 '14 at 20:55
  • If the measurements of (x1,y1), (x2,y2), and (x3,y3) are noisy, what's the best way to ensure that you don't introduce a skew/distortion into M? – BenB Dec 17 '15 at 02:15
1

I'm not sure how standard it is, but there is a nice formula especially for your case presented in "Beginner's guide to mapping simplexes affinely" and "Workbook on mapping simplexes affinely". Putting it into code should look something like this (sorry for bad codestyle -- I'm mathematician, not programmer)

import numpy as np
# input data
ins = [[1, 1, 2], [2, 3, 0], [3, 2, -2], [-2, 2, 3]]  # <- points
out = [[0, 2, 1], [1, 2, 2], [-2, -1, 6], [4, 1, -3]] # <- mapped to
# calculations
l = len(ins)
B = np.vstack([np.transpose(ins), np.ones(l)])
D = 1.0 / np.linalg.det(B)
entry = lambda r,d: np.linalg.det(np.delete(np.vstack([r, B]), (d+1), axis=0))
M = [[(-1)**i * D * entry(R, i) for i in range(l)] for R in np.transpose(out)]
A, t = np.hsplit(np.array(M), [l-1])
t = np.transpose(t)[0]
# output
print("Affine transformation matrix:\n", A)
print("Affine transformation translation vector:\n", t)
# unittests
print("TESTING:")
for p, P in zip(np.array(ins), np.array(out)):
  image_p = np.dot(A, p) + t
  result = "[OK]" if np.allclose(image_p, P) else "[ERROR]"
  print(p, " mapped to: ", image_p, " ; expected: ", P, result)

This code recovers affine transformation from given points ("ins" transformed to "outs") and tests that it works.

guest
  • 409
  • 4
  • 9