Skip to content

Instantly share code, notes, and snippets.

@qzane
Created August 3, 2020 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qzane/a19be2b54e2ac20d2d72a9d15f90dfb0 to your computer and use it in GitHub Desktop.
Save qzane/a19be2b54e2ac20d2d72a9d15f90dfb0 to your computer and use it in GitHub Desktop.
def solve_RT1_RT2(A, B):
''' solve for B = x1.dot(A).dot(x2)
A = [[[R1,T1],[0,1]]...] # shape: (n,4,4)
B = [[[R2,T2],[0,1]]...] # shape: (n,4,4)
minimize{np.einsum('jk,ikp,pq->ijq', x1, A, x2)-B}
return: x1,x2: shape(4,4)
'''
def func(x,a,b):
x1 = x[:16].reshape(4,4)
x2 = x[16:].reshape(4,4)
tmp = np.einsum('jk,ikp,pq->ijq', x1, a, x2)
return (tmp - b).reshape(-1)
#init = np.ones((32,), np.float)
init = np.concatenate((np.identity(4,np.float).reshape(-1), np.identity(4, np.float).reshape(-1)))
xx,_ = scipy.optimize.leastsq(func, init, args=(A,B))
return xx[:16].reshape(4,4),xx[16:].reshape(4,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment