-
Notifications
You must be signed in to change notification settings - Fork 8
/
forward_cpu.py
67 lines (50 loc) · 1.83 KB
/
forward_cpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from gsplat.gausplat import *
from gsplat.gau_io import *
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--gs", help="the input 3d gaussians path")
args = parser.parse_args()
if args.gs:
ply_fn = args.gs
print("Try to load %s ..." % ply_fn)
gs = load_gs(ply_fn)
else:
print("not gaussians file.")
gs = get_example_gs()
# Camera info
tcw = np.array([1.03796196, 0.42017467, 4.67804612])
Rcw = np.array([[0.89699204, 0.06525223, 0.43720409],
[-0.04508268, 0.99739184, -0.05636552],
[-0.43974177, 0.03084909, 0.89759429]]).T
width = int(979) # 1957 # 979
height = int(546) # 1091 # 546
fx = 581.6273640151177
fy = 578.140202494143
cx = width / 2
cy = height / 2
twc = np.linalg.inv(Rcw) @ (-tcw)
fig, ax = plt.subplots()
array = np.zeros(shape=(height, width, 3), dtype=np.uint8)
im = ax.imshow(array)
pws = gs['pw']
# step1. Transform pw to camera frame,
# and project it to iamge.
us, pcs = project(pws, Rcw, tcw, fx, fy, cx, cy)
depths = pcs[:, 2]
# step2. Calcuate the 3d Gaussian.
cov3ds = compute_cov_3d(gs['scale'], gs['rot'])
# step3. Project the 3D Gaussian to 2d image as a 2d Gaussian.
cov2ds = compute_cov_2d(pcs, fx, fy, width, height, cov3ds, Rcw)
# step4. get color info
colors = sh2color(gs['sh'], pws, twc)
# step5. Blend the 2d Gaussian to image
cinv2ds, areas = inverse_cov2d(cov2ds)
splat(height, width, us, cinv2ds, gs['alpha'],
depths, colors, areas, im)
# from PIL import Image
# pil_img = Image.fromarray((np.clip(image, 0, 1)*255).astype(np.uint8))
# print(pil_img.mode)
# pil_img.save('test.png')
# plt.imshow(image)
plt.show()