-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
167 lines (126 loc) · 5.21 KB
/
utils.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
import torch
import numpy as np
HUGE_NUMBER = 1e10
TINY_NUMBER = 1e-10
def img2mse(x, y, mask=None):
if mask is None:
return torch.mean((x - y) * (x - y))
else:
return torch.sum((x - y) * (x - y) * mask.unsqueeze(-1)) / (torch.sum(mask) * x.shape[-1] + TINY_NUMBER)
img_HWC2CHW = lambda x: x.permute(2, 0, 1)
gray2rgb = lambda x: x.unsqueeze(2).repeat(1, 1, 3)
normalize = lambda x: (x - x.min()) / (x.max() - x.min() + TINY_NUMBER)
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
# gray2rgb = lambda x: np.tile(x[:,:,np.newaxis], (1, 1, 3))
mse2psnr = lambda x: -10. * np.log(x+TINY_NUMBER) / np.log(10.)
########################################################################################################################
# depth to normal
########################################################################################################################
def depth_to_normal(depth, intrinsics, c2w_mat):
invalid_mask = depth < 1e-10
H, W = depth.shape[:2]
u, v = np.meshgrid(np.arange(W), np.arange(H))
u = u.reshape(-1).astype(dtype=np.float32) + 0.5 # add half pixel
v = v.reshape(-1).astype(dtype=np.float32) + 0.5
pixels = np.stack((u, v, np.ones_like(u)), axis=0) # (3, H*W)
rays_d = np.dot(np.linalg.inv(intrinsics[:3, :3]), pixels) # (3, H*W)
depth = depth.reshape((1, -1))
xyz_map = rays_d * depth # (3, H*W)
xyz_map = xyz_map.reshape((3, H, W)).transpose((1, 2, 0)) # (H, W, 3)
# estimate the normal
delta_x = np.gradient(xyz_map, axis=1)
delta_y = np.gradient(xyz_map, axis=0)
normal = np.cross(delta_x, delta_y, axis=2)
normal = normal / (np.linalg.norm(normal, axis=2, keepdims=True))
normal = -normal # flip normal so that it points towards camera
invalid_mask2 = np.logical_not(np.isfinite(normal.sum(axis=2)))
invalid_mask = np.logical_or(invalid_mask, invalid_mask2)
normal[invalid_mask, :] = 0.
assert(np.allclose(np.linalg.norm(normal[np.logical_not(invalid_mask), :], axis=1), 1.))
# rotate to scene space
H, W = normal.shape[:2]
normal = np.dot(normal.reshape((-1, 3)), c2w_mat[:3, :3].T).reshape((H, W, 3))
normal = normal.astype(np.float32)
return normal
########################################################################################################################
# visualize
########################################################################################################################
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import matplotlib as mpl
from matplotlib import cm
import cv2
def get_vertical_colorbar(h, vmin, vmax, cmap_name='jet', label=None):
'''
:param w: pixels
:param h: pixels
:param vmin: min value
:param vmax: max value
:param cmap_name:
:param label
:return:
'''
fig = Figure(figsize=(1.2, 8), dpi=100)
fig.subplots_adjust(right=1.5)
canvas = FigureCanvasAgg(fig)
# Do some plotting.
ax = fig.add_subplot(111)
cmap = cm.get_cmap(cmap_name)
norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
tick_cnt = 6
tick_loc = np.linspace(vmin, vmax, tick_cnt)
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
norm=norm,
ticks=tick_loc,
orientation='vertical')
tick_label = ['{:3.2f}'.format(x) for x in tick_loc]
cb1.set_ticklabels(tick_label)
cb1.ax.tick_params(labelsize=18, rotation=0)
if label is not None:
cb1.set_label(label)
fig.tight_layout()
canvas.draw()
s, (width, height) = canvas.print_to_buffer()
im = np.frombuffer(s, np.uint8).reshape((height, width, 4))
im = im[:, :, :3].astype(np.float32) / 255.
if h != im.shape[0]:
w = int(im.shape[1] / im.shape[0] * h)
im = cv2.resize(im, (w, h), interpolation=cv2.INTER_AREA)
return im
def colorize_np(x, cmap_name='jet', mask=None, append_cbar=False):
if mask is not None:
# vmin, vmax = np.percentile(x[mask], (1, 99))
vmin = np.min(x[mask])
vmax = np.max(x[mask])
vmin = vmin - np.abs(vmin) * 0.01
x[np.logical_not(mask)] = vmin
x = np.clip(x, vmin, vmax)
# print(vmin, vmax)
else:
vmin = x.min()
vmax = x.max() + TINY_NUMBER
x = (x - vmin) / (vmax - vmin)
# x = np.clip(x, 0., 1.)
cmap = cm.get_cmap(cmap_name)
x_new = cmap(x)[:, :, :3]
if mask is not None:
mask = np.float32(mask[:, :, np.newaxis])
x_new = x_new * mask + np.zeros_like(x_new) * (1. - mask)
cbar = get_vertical_colorbar(h=x.shape[0], vmin=vmin, vmax=vmax, cmap_name=cmap_name)
if append_cbar:
x_new = np.concatenate((x_new, np.zeros_like(x_new[:, :5, :]), cbar), axis=1)
return x_new
else:
return x_new, cbar
# tensor
def colorize(x, cmap_name='jet', append_cbar=False, mask=None):
x = x.numpy()
if mask is not None:
mask = mask.numpy().astype(dtype=np.bool)
x, cbar = colorize_np(x, cmap_name, mask)
if append_cbar:
x = np.concatenate((x, np.zeros_like(x[:, :5, :]), cbar), axis=1)
x = torch.from_numpy(x)
return x
if __name__ == '__main__':
pass