-
Notifications
You must be signed in to change notification settings - Fork 4
/
vis_utils.py
37 lines (29 loc) · 1.03 KB
/
vis_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
import cv2
import numpy as np
def visualize_depth_numpy(depth, minmax=None, cmap=cv2.COLORMAP_JET):
""" depth: (H, W) """
x = np.nan_to_num(depth) # change nan to 0
if minmax is None:
mi = np.min(x[x>0]) # get minimum positive depth (ignore background)
ma = np.max(x)
else:
mi, ma = minmax
x = (x-mi) / (ma-mi+1e-8) # normalize to 0~1
x = (255 * x).astype(np.uint8)
x_ = cv2.applyColorMap(x, cmap)
return x_, [mi, ma]
def visualize_depth(depth, minmax=None, cmap=cv2.COLORMAP_JET):
""" depth: (H, W) """
if type(depth) is not np.ndarray:
depth = depth.cpu().numpy()
x = np.nan_to_num(depth) # change nan to 0
if minmax is None:
mi = np.min(x[x>0]) # get minimum positive depth (ignore background)
ma = np.max(x)
else:
mi, ma = minmax
x = (x-mi) / (ma-mi+1e-8) # normalize to 0~1
x = (255 * x).astype(np.uint8)
x_ = Image.fromarray(cv2.applyColorMap(x, cmap))
x_ = T.ToTensor()(x_) # (3, H, W)
return x_, [mi, ma]