Skip to content

Instantly share code, notes, and snippets.

@ollpu
Created October 11, 2019 12:32
Show Gist options
  • Save ollpu/228b0c873e1f749ba13b4c3b92ef247c to your computer and use it in GitHub Desktop.
Save ollpu/228b0c873e1f749ba13b4c3b92ef247c to your computer and use it in GitHub Desktop.
Perspective matrix solver
#!/usr/bin/python3
import numpy as np
# Input
w, h = map(float, input("Framebuffer dimensions (W H): ").split())
point_human = ["top left", "top right", "bottom left", "bottom right"]
P = [list(map(float, input("Enter coordinates of {} point (x y): ".format(s)).split())) for s in point_human]
T = [(0, 0), (w, 0), (0, h), (w, h)]
# Compute
a = []
b = []
for i in range(4):
Px, Py = P[i]
Tx, Ty = T[i]
a.append([Px, Py, 1, 0, 0, 0, -Tx*Px, -Tx*Py])
b.append(Tx)
a.append([0, 0, 0, Px, Py, 1, -Ty*Px, -Ty*Py])
b.append(Ty)
a, b = map(np.array, (a, b))
x = list(np.linalg.solve(a, b)) + [1]
print(x)
print("Result:")
print(",".join(["{:.10f}".format(v) for v in x]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment