Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
592c40d
1
Parent(s):
f836904
Create geometry.py
Browse files- geometry.py +72 -0
geometry.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
def get_intrinsics(H,W):
|
| 4 |
+
"""
|
| 5 |
+
Intrinsics for a pinhole camera model.
|
| 6 |
+
Assume fov of 55 degrees and central principal point.
|
| 7 |
+
"""
|
| 8 |
+
f = 0.5 * W / np.tan(0.5 * 55 * np.pi / 180.0)
|
| 9 |
+
cx = 0.5 * W
|
| 10 |
+
cy = 0.5 * H
|
| 11 |
+
return np.array([[f, 0, cx],
|
| 12 |
+
[0, f, cy],
|
| 13 |
+
[0, 0, 1]])
|
| 14 |
+
|
| 15 |
+
def depth_to_points(depth, R=None, t=None):
|
| 16 |
+
|
| 17 |
+
K = get_intrinsics(depth.shape[1], depth.shape[2])
|
| 18 |
+
Kinv = np.linalg.inv(K)
|
| 19 |
+
if R is None:
|
| 20 |
+
R = np.eye(3)
|
| 21 |
+
if t is None:
|
| 22 |
+
t = np.zeros(3)
|
| 23 |
+
|
| 24 |
+
# M converts from your coordinate to PyTorch3D's coordinate system
|
| 25 |
+
M = np.eye(3)
|
| 26 |
+
M[0, 0] = -1.0
|
| 27 |
+
M[1, 1] = -1.0
|
| 28 |
+
|
| 29 |
+
height, width = depth.shape[1:3]
|
| 30 |
+
|
| 31 |
+
x = np.arange(width)
|
| 32 |
+
y = np.arange(height)
|
| 33 |
+
coord = np.stack(np.meshgrid(x, y), -1)
|
| 34 |
+
coord = np.concatenate((coord, np.ones_like(coord)[:, :, [0]]), -1) # z=1
|
| 35 |
+
coord = coord.astype(np.float32)
|
| 36 |
+
# coord = torch.as_tensor(coord, dtype=torch.float32, device=device)
|
| 37 |
+
coord = coord[None] # bs, h, w, 3
|
| 38 |
+
|
| 39 |
+
D = depth[:, :, :, None, None]
|
| 40 |
+
# print(D.shape, Kinv[None, None, None, ...].shape, coord[:, :, :, :, None].shape )
|
| 41 |
+
pts3D_1 = D * Kinv[None, None, None, ...] @ coord[:, :, :, :, None]
|
| 42 |
+
# pts3D_1 live in your coordinate system. Convert them to Py3D's
|
| 43 |
+
pts3D_1 = M[None, None, None, ...] @ pts3D_1
|
| 44 |
+
# from reference to targe tviewpoint
|
| 45 |
+
pts3D_2 = R[None, None, None, ...] @ pts3D_1 + t[None, None, None, :, None]
|
| 46 |
+
# pts3D_2 = pts3D_1
|
| 47 |
+
# depth_2 = pts3D_2[:, :, :, 2, :] # b,1,h,w
|
| 48 |
+
return pts3D_2[:, :, :, :3, 0][0]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def create_triangles(h, w, mask=None):
|
| 52 |
+
"""Creates mesh triangle indices from a given pixel grid size.
|
| 53 |
+
This function is not and need not be differentiable as triangle indices are
|
| 54 |
+
fixed.
|
| 55 |
+
Args:
|
| 56 |
+
h: (int) denoting the height of the image.
|
| 57 |
+
w: (int) denoting the width of the image.
|
| 58 |
+
Returns:
|
| 59 |
+
triangles: 2D numpy array of indices (int) with shape (2(W-1)(H-1) x 3)
|
| 60 |
+
"""
|
| 61 |
+
x, y = np.meshgrid(range(w - 1), range(h - 1))
|
| 62 |
+
tl = y * w + x
|
| 63 |
+
tr = y * w + x + 1
|
| 64 |
+
bl = (y + 1) * w + x
|
| 65 |
+
br = (y + 1) * w + x + 1
|
| 66 |
+
triangles = np.array([tl, bl, tr, br, tr, bl])
|
| 67 |
+
triangles = np.transpose(triangles, (1, 2, 0)).reshape(
|
| 68 |
+
((w - 1) * (h - 1) * 2, 3))
|
| 69 |
+
if mask is not None:
|
| 70 |
+
mask = mask.reshape(-1)
|
| 71 |
+
triangles = triangles[mask[triangles].all(1)]
|
| 72 |
+
return triangles
|