-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_map.py
64 lines (52 loc) · 1.77 KB
/
color_map.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
import numpy as np
def colormap(t):
'''
:param t: a number from [0, 1]
:return: a rgb color from blue (0, 0, 255) to red (255, 0, 0)
'''
l = 2 + np.sqrt(2)
t0 = 1 / l
t1 = (1 + np.sqrt(2)) / l
r = np.array([255, 0, 0])
y = np.array([255, 255, 0])
q = np.array([0, 255, 255])
b = np.array([0, 0, 255])
rt = np.zeros_like(r, dtype=np.uint8)
if (t <= t0):
s = 1 - t / t0
rt = s * r + (1 - s) * y
elif (t <= t1):
s = 1 - (t - t0) / (t1 - t0)
rt = s * y + (1 - s) * q
else:
s = 1 - (t - t1) / (1 - t1)
rt = s * q + (1 - s) * b
return rt
def colormap_depthmap(depmap, backgroud=255):
'''
:param depmap: shape [M, N]; value range [0, 255)
:return: colorful depth map
'''
depmap /= 255.
l = 2 + np.sqrt(2)
t0 = 1 / l
t1 = (1 + np.sqrt(2)) / l
r = np.array([255, 0, 0])
y = np.array([255, 255, 0])
q = np.array([0, 255, 255])
b = np.array([0, 0, 255])
img_h, img_w = depmap.shape
depmap_color = np.ones((img_h, img_w, 3), dtype=np.uint8) * 255
# for t <= t0
vid1, uid1 = np.where(depmap <= t0)
s = np.tile(np.array(1 - depmap[vid1, uid1] / t0).reshape((-1, 1)), (1, 3))
depmap_color[vid1, uid1] = s * r + (1 - s) * y
# for t > t0 and t <= t1
vid2, uid2 = np.where(np.logical_and(depmap > t0, depmap <= t1))
s = np.tile(np.array(1 - (depmap[vid2, uid2] - t0) / (t1 - t0)).reshape((-1, 1)), (1, 3))
depmap_color[vid2, uid2] = s * y + (1 - s) * q
# for t > t1 and t < 1
vid3, uid3 = np.where(np.logical_and(depmap > t1, depmap < 1. - 1e-8))
s = np.tile(np.array(1 - (depmap[vid3, uid3] - t1) / (1 - t1)).reshape((-1, 1)), (1, 3))
depmap_color[vid3, uid3] = s * q + (1 - s) * b
return depmap_color