-
Notifications
You must be signed in to change notification settings - Fork 45
/
cameras.py
226 lines (178 loc) · 9.08 KB
/
cameras.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact [email protected]
#
import torch
from torch import nn
import numpy as np
from utils.graphics_utils import getWorld2View2, getProjectionMatrix, getProjectionMatrixCV
from utils.graphics_utils import fov2focal
# def fov2focal(fov, pixels):
# return pixels / (2 * math.tan(fov / 2))
from kornia import create_meshgrid
from helper_model import pix2ndc
from helper_train import getgtisint8
import random
class Camera(nn.Module):
def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
image_name, uid,
trans=np.array([0.0, 0.0, 0.0]), scale=1.0, data_device = "cuda", near=0.01, far=100.0, timestamp=0.0, rayo=None, rayd=None, rays=None, cxr=0.0,cyr=0.0,
):
super(Camera, self).__init__()
self.uid = uid
self.colmap_id = colmap_id
self.R = R
self.T = T
self.FoVx = FoVx
self.FoVy = FoVy
self.image_name = image_name
self.timestamp = timestamp
try:
self.data_device = torch.device(data_device)
except Exception as e:
print(e)
print(f"[Warning] Custom device {data_device} failed, fallback to default cuda device" )
self.data_device = torch.device("cuda")
# image is real image
if not isinstance(image, tuple):
if getgtisint8():
self.original_image = (image*255).to(torch.uint8).to(self.data_device)
else:
if "camera_" not in image_name:
self.original_image = image.clamp(0.0, 1.0).to(self.data_device)
else:
self.original_image = image.clamp(0.0, 1.0).half().to(self.data_device)
self.image_width = self.original_image.shape[2]
self.image_height = self.original_image.shape[1]
# if gt_alpha_mask is not None:
# self.original_image *= gt_alpha_mask.to(self.data_device)
# else:
# self.original_image *= torch.ones((1, self.image_height, self.image_width), device=self.data_device)
else:
self.image_width = image[0]
self.image_height = image[1]
self.original_image = None
self.zfar = 100.0
self.znear = 0.01
self.trans = trans
self.scale = scale
self.world_view_transform = torch.tensor(getWorld2View2(R, T, trans, scale)).transpose(0, 1).cuda()
if cyr != 0.0 :
self.cxr = cxr
self.cyr = cyr
self.projection_matrix = getProjectionMatrixCV(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy, cx=cxr, cy=cyr).transpose(0,1).cuda()
else:
self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy).transpose(0,1).cuda()
self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0)
self.camera_center = self.world_view_transform.inverse()[3, :3]
if rayd is not None:
projectinverse = self.projection_matrix.T.inverse()
camera2wold = self.world_view_transform.T.inverse()
pixgrid = create_meshgrid(self.image_height, self.image_width, normalized_coordinates=False, device="cpu")[0]
pixgrid = pixgrid.cuda() # H,W,
xindx = pixgrid[:,:,0] # x
yindx = pixgrid[:,:,1] # y
ndcy, ndcx = pix2ndc(yindx, self.image_height), pix2ndc(xindx, self.image_width)
ndcx = ndcx.unsqueeze(-1)
ndcy = ndcy.unsqueeze(-1)# * (-1.0)
ndccamera = torch.cat((ndcx, ndcy, torch.ones_like(ndcy) * (1.0) , torch.ones_like(ndcy)), 2) # N,4
projected = ndccamera @ projectinverse.T
diretioninlocal = projected / projected[:,:,3:] #v
direction = diretioninlocal[:,:,:3] @ camera2wold[:3,:3].T
rays_d = torch.nn.functional.normalize(direction, p=2.0, dim=-1)
self.rayo = self.camera_center.expand(rays_d.shape).permute(2, 0, 1).unsqueeze(0) #rayo.permute(2, 0, 1).unsqueeze(0)
self.rayd = rays_d.permute(2, 0, 1).unsqueeze(0)
else :
self.rayo = None
self.rayd = None
class MiniCam:
def __init__(self, width, height, fovy, fovx, znear, zfar, world_view_transform, full_proj_transform):
self.image_width = width
self.image_height = height
self.FoVy = fovy
self.FoVx = fovx
self.znear = znear
self.zfar = zfar
self.world_view_transform = world_view_transform
self.full_proj_transform = full_proj_transform
view_inv = torch.inverse(self.world_view_transform)
self.camera_center = view_inv[3][:3]
class Camerass(nn.Module):
def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
image_name, uid,
trans=np.array([0.0, 0.0, 0.0]), scale=1.0, data_device = "cuda", near=0.01, far=100.0, timestamp=0.0, rayo=None, rayd=None, rays=None, cxr=0.0,cyr=0.0,
):
super(Camerass, self).__init__()
self.uid = uid
self.colmap_id = colmap_id
self.R = R
self.T = T
self.FoVx = FoVx
self.FoVy = FoVy
self.image_name = image_name
self.timestamp = timestamp
self.fisheyemapper = None
try:
self.data_device = torch.device(data_device)
except Exception as e:
print(e)
print(f"[Warning] Custom device {data_device} failed, fallback to default cuda device" )
self.data_device = torch.device("cuda")
# image is real image
if not isinstance(image, tuple):
if getgtisint8():
self.original_image = (image*255).to(torch.uint8).to(self.data_device)
else:
if "camera_" not in image_name:
self.original_image = image.clamp(0.0, 1.0).to(self.data_device)
else:
self.original_image = image.clamp(0.0, 1.0).half().to(self.data_device)
print("read one")# lazy loader already in it
self.image_width = self.original_image.shape[2]
self.image_height = self.original_image.shape[1]
else:
self.image_width = image[0]
self.image_height = image[1]
self.original_image = None
self.image_width = 2 * self.image_width
self.image_height = 2 * self.image_height #
self.zfar = 100.0
self.znear = 0.01
self.trans = trans
self.scale = scale
# w2c
self.world_view_transform = torch.tensor(getWorld2View2(R, T, trans, scale)).transpose(0, 1).cuda()
if cyr != 0.0 :
self.cxr = cxr
self.cyr = cyr
self.projection_matrix = getProjectionMatrixCV(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy, cx=cxr, cy=cyr).transpose(0,1).cuda()
else:
self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy).transpose(0,1).cuda()
self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0)
self.camera_center = self.world_view_transform.inverse()[3, :3]
if rayd is not None:
projectinverse = self.projection_matrix.T.inverse()
camera2wold = self.world_view_transform.T.inverse()
pixgrid = create_meshgrid(self.image_height, self.image_width, normalized_coordinates=False, device="cpu")[0]
pixgrid = pixgrid.cuda() # H,W,
xindx = pixgrid[:,:,0] # x
yindx = pixgrid[:,:,1] # y
ndcy, ndcx = pix2ndc(yindx, self.image_height), pix2ndc(xindx, self.image_width)
ndcx = ndcx.unsqueeze(-1)
ndcy = ndcy.unsqueeze(-1)# * (-1.0)
ndccamera = torch.cat((ndcx, ndcy, torch.ones_like(ndcy) * (1.0) , torch.ones_like(ndcy)), 2) # N,4
projected = ndccamera @ projectinverse.T
diretioninlocal = projected / projected[:,:,3:] #
direction = diretioninlocal[:,:,:3] @ camera2wold[:3,:3].T
rays_d = torch.nn.functional.normalize(direction, p=2.0, dim=-1)
self.rayo = self.camera_center.expand(rays_d.shape).permute(2, 0, 1).unsqueeze(0) #rayo.permute(2, 0, 1).unsqueeze(0)
self.rayd = rays_d.permute(2, 0, 1).unsqueeze(0) #rayd.permute(2, 0, 1).unsqueeze(0)
else :
self.rayo = None
self.rayd = None