Unverified Commit 4c0f4414 authored by Johan Edstedt's avatar Johan Edstedt Committed by GitHub
Browse files

Make _get_perspective_coeffs more numerically stable (#8249)


Co-authored-by: default avatarNicolas Hug <nh.nicolas.hug@gmail.com>
parent ae147895
......@@ -676,14 +676,19 @@ def _get_perspective_coeffs(startpoints: List[List[int]], endpoints: List[List[i
Returns:
octuple (a, b, c, d, e, f, g, h) for transforming each pixel.
"""
a_matrix = torch.zeros(2 * len(startpoints), 8, dtype=torch.float)
if len(startpoints) != 4 or len(endpoints) != 4:
raise ValueError(
f"Please provide exactly four corners, got {len(startpoints)} startpoints and {len(endpoints)} endpoints."
)
a_matrix = torch.zeros(2 * len(startpoints), 8, dtype=torch.float64)
for i, (p1, p2) in enumerate(zip(endpoints, startpoints)):
a_matrix[2 * i, :] = torch.tensor([p1[0], p1[1], 1, 0, 0, 0, -p2[0] * p1[0], -p2[0] * p1[1]])
a_matrix[2 * i + 1, :] = torch.tensor([0, 0, 0, p1[0], p1[1], 1, -p2[1] * p1[0], -p2[1] * p1[1]])
b_matrix = torch.tensor(startpoints, dtype=torch.float).view(8)
res = torch.linalg.lstsq(a_matrix, b_matrix, driver="gels").solution
b_matrix = torch.tensor(startpoints, dtype=torch.float64).view(8)
# do least squares in double precision to prevent numerical issues
res = torch.linalg.lstsq(a_matrix, b_matrix, driver="gels").solution.to(torch.float32)
output: List[float] = res.tolist()
return output
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment