Skip to content

Instantly share code, notes, and snippets.

@adamwlev
Last active May 16, 2017 04:54
Show Gist options
  • Save adamwlev/895e19379d8fc31d723124d902dc6867 to your computer and use it in GitHub Desktop.
Save adamwlev/895e19379d8fc31d723124d902dc6867 to your computer and use it in GitHub Desktop.
Affine Transformation
import numpy as np
def produce_transformation(tuples,tuples_t):
"""
Given points in some 2d space, and points in a transformed
2d space, produces function to tranform one space to other.
tuples : points in original space (list of x,y tuples)
tuples_t : corresponding points in transformed space (")
"""
x = np.array(tuples)
X = np.kron(np.eye(2),np.hstack([x,np.ones(x.shape[0])[:,None]]))
X_inv = np.dot(np.linalg.inv(np.dot(X.T,X)),X.T)
x_prime = np.concatenate(zip(*tuples_t))[:,None]
a = np.dot(X_inv,x_prime)
def f(tuples):
x = np.array(tuples)
X = np.kron(np.eye(2),np.hstack([x,np.ones(x.shape[0])[:,None]]))
x_prime = np.squeeze(np.dot(X,a))
return zip(x_prime[0:len(tuples)],x_prime[len(tuples):])
return f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment