DelinQu
???? upload all tar.gz data
bfbecd6
import torch
import re
import numpy as np
import cv2 as cv
def as_intrinsics_matrix(intrinsics):
"""
Get matrix representation of intrinsics.
"""
K = np.eye(3)
K[0, 0] = intrinsics[0]
K[1, 1] = intrinsics[1]
K[0, 2] = intrinsics[2]
K[1, 2] = intrinsics[3]
return K
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [int(x) if x.isdigit() else x for x in re.split('([0-9]+)', s)]
def radial_distortion(grid_x, grid_y, fx, fy, cx, cy, dists):
"""
radial distortion,
"""
k1, k2, k3 = dists[0], dists[1], dists[2]
x, y = (grid_x - cx) / fx, (grid_y - cy) / fy
r2 = x ** 2 + y ** 2
grid_x = fx * (x * (1 + k1 * r2 + k2 * r2 ** 2 + k3 * r2 ** 3)) + cx
grid_y = fy * (y * (1 + k1 * r2 + k2 * r2 ** 2 + k3 * r2 ** 3)) + cy
return grid_x, grid_y
# * TODO: add camera distorsion
def get_camera_rays(H, W, fx, fy=None, cx=None, cy=None, davis_distortion=None, type='OpenGL'):
"""Get ray origins, directions from a pinhole camera."""
# ----> i
# |
# |
# j
i, j = torch.meshgrid(torch.arange(W, dtype=torch.float32), torch.arange(H, dtype=torch.float32), indexing='xy')
# View direction (X, Y, Lambda) / lambda
# Move to the center of the screen
# -------------
# | y |
# | | |
# | .-- x |
# | |
# | |
# -------------
if cx is None:
cx, cy = 0.5 * W, 0.5 * H
if fy is None:
fy = fx
# * remove distorsion use undistortPoints
if davis_distortion is not None:
points = np.stack((i.numpy(), j.numpy()), axis=-1).reshape(-1, 1, 2) # (N, 1, 2)
points = cv.undistortPoints(points, as_intrinsics_matrix([fx, fy, cx, cy]), np.array(davis_distortion))
points = torch.from_numpy(points).reshape(H, W, 2) * torch.tensor([fx, fy]) + torch.tensor([cx, cy])
i, j = points[...,0], points[..., 1]
if type is 'OpenGL':
dirs = torch.stack([(i - cx)/fx, -(j - cy)/fy, -torch.ones_like(i)], -1)
elif type is 'OpenCV':
dirs = torch.stack([(i - cx)/fx, (j - cy)/fy, torch.ones_like(i)], -1)
else:
raise NotImplementedError()
rays_d = dirs
return rays_d