Affine transformation algorithm

后端 未结 2 1459
感动是毒
感动是毒 2020-12-05 07:14

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?

相关标签:
2条回答
  • 2020-12-05 07:52

    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.

    0 讨论(0)
  • 2020-12-05 08:12

    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.

    0 讨论(0)
提交回复
热议问题