-
Notifications
You must be signed in to change notification settings - Fork 0
/
blender_ray.py
150 lines (120 loc) · 5.62 KB
/
blender_ray.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import torch
from torch.utils.data import Dataset
import json
import numpy as np
import os
from PIL import Image
from torchvision import transforms as T
from kornia import create_meshgrid
def get_ray_directions(H, W, focal):
"""
Get ray directions for all pixels in camera coordinate.
Reference: https://www.scratchapixel.com/lessons/3d-basic-rendering/
ray-tracing-generating-camera-rays/standard-coordinate-systems
Inputs:
H, W, focal: image height, width and focal length
Outputs:
directions: (H, W, 3), the direction of the rays in camera coordinate
"""
grid = create_meshgrid(H, W, normalized_coordinates=False)[0]
i, j = grid.unbind(-1)
# the direction here is without +0.5 pixel centering as calibration is not so accurate
# see https://github.com/bmild/nerf/issues/24
directions = \
torch.stack([(i-W/2)/focal, -(j-H/2)/focal, -torch.ones_like(i)], -1) # (H, W, 3)
return directions
def get_rays(directions, c2w):
"""
Get ray origin and normalized directions in world coordinate for all pixels in one image.
Reference: https://www.scratchapixel.com/lessons/3d-basic-rendering/
ray-tracing-generating-camera-rays/standard-coordinate-systems
Inputs:
directions: (H, W, 3) precomputed ray directions in camera coordinate
c2w: (3, 4) transformation matrix from camera coordinate to world coordinate
Outputs:
rays_o: (H*W, 3), the origin of the rays in world coordinate
rays_d: (H*W, 3), the normalized direction of the rays in world coordinate
"""
# Rotate ray directions from camera coordinate to the world coordinate
rays_d = directions @ c2w[:, :3].T # (H, W, 3)
rays_d = rays_d / torch.norm(rays_d, dim=-1, keepdim=True)
# The origin of all rays is the camera origin in world coordinate
rays_o = c2w[:, 3].expand(rays_d.shape) # (H, W, 3)
rays_d = rays_d.view(-1, 3)
rays_o = rays_o.view(-1, 3)
return rays_o, rays_d
class BlenderDataset(Dataset):
def __init__(self, root_dir, split='train', img_wh=(800, 800)):
self.x = 0
self.y = 0
self.z = 0
self.root_dir = root_dir
self.split = split
# assert img_wh[0] == img_wh[1], 'image width must equal image height!'
self.img_wh = img_wh
self.define_transforms()
self.read_meta()
self.white_back = True
def read_meta(self):
with open(os.path.join(self.root_dir,
f"transforms_{self.split}.json"), 'r') as f:
self.meta = json.load(f)
w, h = self.img_wh
self.focal = 0.5 * w / np.tan(0.5 * self.meta['camera_angle_x']) # original focal length
# bounds, common for all scenes
self.near = 2.0
self.far = 6.0
self.bounds = np.array([self.near, self.far])
# ray directions for all pixels, same for all images (same H, W, focal)
self.directions = \
get_ray_directions(h, w, self.focal) # (h, w, 3)
if self.split == 'train' or self.split == 'val': # create buffer of all rays and rgb data
self.image_paths = []
self.poses = []
self.all_rays = []
self.all_rgbs = []
for frame in self.meta['frames']:
pose = np.array(frame['rot_mat'])[:3, :4]
self.poses += [pose]
c2w = torch.FloatTensor(pose)
file = os.path.join(self.root_dir, "images")
file_list = os.listdir(file)
file_list.sort()# 按文件名排序
image_path = os.path.join(file, file_list[frame["frame_index"]-1])
# image_path = f"{frame['file_path']}"
self.image_paths += [image_path]
img = Image.open(image_path).convert("RGBA")
img = img.resize(self.img_wh, Image.LANCZOS)
img = self.transform(img) # (4, h, w)
img = img.view(4, -1).permute(1, 0) # (h*w, 4) RGBA
img = img[:, :3] * img[:, -1:] + (1 - img[:, -1:]) # blend A to RGB
# self.all_rgbs += [img]
rays_o, rays_d = get_rays(self.directions, c2w) # both (h*w, 3)
self.all_rays += [torch.cat([rays_o, rays_d, img], 1)] # (h*w, 9)
if self.x <= abs(rays_o[0, 0]):
self.x = abs(rays_o[0, 0])
if self.y <= abs(rays_o[0, 1]):
self.y = abs(rays_o[0, 1])
if self.z <= abs(rays_o[0, 2]):
self.z = abs(rays_o[0, 2])
self.all_rays = torch.cat(self.all_rays, 0) # (len(self.meta['frames'])*h*w, 3)
print(self.x)
print(self.y)
print(self.z)
def define_transforms(self):
self.transform = T.ToTensor()
def __len__(self):
if self.split == 'train':
return len(self.all_rays)
if self.split == 'val':
return 8 # only validate 8 images (to support <=8 gpus)
return len(self.meta['frames'])
def __getitem__(self, idx):
if self.split == 'train': # use data in the buffers
rays = {'rays': self.all_rays}
else: # create data for each image separately
frame = self.meta['frames'][idx]
c2w = torch.FloatTensor(frame['transform_matrix'])[:3, :4]
rays_o, rays_d = get_rays(self.directions, c2w)
rays = torch.cat([rays_o, rays_d, 1, 1, 1], 1) # rays
return rays