Skip to content

Instantly share code, notes, and snippets.

@cheind
Created June 5, 2017 13:21
Show Gist options
  • Save cheind/90e79ef3f4256dd0bddd3791df1eb4b6 to your computer and use it in GitHub Desktop.
Save cheind/90e79ef3f4256dd0bddd3791df1eb4b6 to your computer and use it in GitHub Desktop.
1D homography between lines using DLT transform
import numpy as np
import matplotlib.pyplot as plt
o3d = np.array([-100, -100, 50])
d3d = np.array([1, 1, 5.])
d3d /= np.linalg.norm(d3d)
t3d = np.arange(0, 100, 1.)
p3d = o3d + d3d * t3d[:, None]
K = np.eye(3)
K[0,0] = 200
K[1,1] = 200
p2d = K.dot(p3d.T).T
p2d = p2d / p2d[:, 2][:, None]
p2d = p2d[:, :2]
o2d = p2d[0]
d2d = p2d[-1] - p2d[0]
d2d /= np.linalg.norm(d2d)
x = p2d - p2d[0][None, :]
t2d = np.dot(d2d, x.T)
s = t2d
k = t3d
A = []
for i in range(len(t2d)):
A.append([-s[i], -1., s[i]*k[i], k[i]])
A = np.asarray(A)
U, S, Vh = np.linalg.svd(A)
L = Vh[-1,:] / Vh[-1,-1]
H = L.reshape(2, 2)
print(H)
# reconstruct
hs = np.vstack((s, np.ones(s.shape[0])))
t = H.dot(hs).T
t = t / t[:,1][:, None]
t = t[:,0]
r3d = o3d + d3d * t[:, None]
plt.plot(s, p3d[:,2])
plt.plot(s, r3d[:,2])
plt.show()
print(s)
print(np.diff(s))
print(t[:,0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment