-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdepth.py
100 lines (90 loc) · 3.2 KB
/
depth.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
import numpy as np
import cv2
from blurgenerator import motion_blur, lens_blur, gaussian_blur
def map_range(value, inMin, inMax, outMin, outMax):
return outMin + (((value - inMin) / (inMax - inMin)) * (outMax - outMin))
def get_depth_step_and_layer(depth_map, num_layers):
min_depth = np.min(np.unique(depth_map))
max_depth = np.max(np.unique(depth_map))
step = (max_depth - min_depth) // num_layers
layers = np.array(range(min_depth, max_depth, step))
return step, layers
def get_blur_amount_and_l_mask(depth_map, value, step, min_blur, max_blur):
dm = cv2.cvtColor(depth_map, cv2.COLOR_BGR2GRAY)
m = np.zeros(dm.shape)
m[dm > value] = 255
m[dm > (value + step)] = 0
l_mask = depth_map.copy()
l_mask[:,:,0] = m[:,:]
l_mask[:,:,1] = m[:,:]
l_mask[:,:,2] = m[:,:]
blur_amount = int(map_range(value, 0, 255, min_blur, max_blur))
return blur_amount, l_mask
def blur_with_depth(depth_map, num_layers=10, min_blur=1, max_blur=100):
step, layers = get_depth_step_and_layer(depth_map, num_layers)
blur_masks = []
for value in layers:
blur_amount, l_mask = get_blur_amount_and_l_mask(
depth_map,
value,
step,
min_blur,
max_blur
)
blur_masks.append((blur_amount, l_mask))
return blur_masks
def motion_blur_with_depth_map(img, depth_map, angle=30, num_layers=10, min_blur=1, max_blur=100):
out = np.zeros(img.shape)
blur_masks = blur_with_depth(
depth_map,
num_layers=num_layers,
min_blur=min_blur,
max_blur=max_blur
)
for blur_amount, l_mask in blur_masks:
slice = motion_blur(
img,
size=blur_amount,
angle=angle
)
_, mask = cv2.threshold(l_mask, 100, 255, cv2.THRESH_BINARY)
layer = cv2.bitwise_and(slice, slice, mask=mask[:,:,0])
out = cv2.add(out, layer, dtype=0)
return out
def lens_blur_with_depth_map(img, depth_map, components=5, exposure_gamma=5, num_layers=10, min_blur=1, max_blur=100):
out = np.zeros(img.shape)
blur_masks = blur_with_depth(
depth_map,
num_layers=num_layers,
min_blur=min_blur,
max_blur=max_blur
)
for blur_amount, l_mask in blur_masks:
slice = lens_blur(
img,
radius=blur_amount,
components=components,
exposure_gamma=exposure_gamma
)
_, mask = cv2.threshold(l_mask, 100, 255, cv2.THRESH_BINARY)
layer = cv2.bitwise_and(slice, slice, mask=mask[:,:,0])
out = cv2.add(out, layer, dtype=0)
return out
def gaussian_blur_with_depth_map(img, depth_map, sigma=5, num_layers=10, min_blur=1, max_blur=100):
out = np.zeros(img.shape)
blur_masks = blur_with_depth(
depth_map,
num_layers=num_layers,
min_blur=min_blur,
max_blur=max_blur
)
for blur_amount, l_mask in blur_masks:
slice = gaussian_blur(
img,
blur_amount,
sigma=sigma
)
_, mask = cv2.threshold(l_mask, 100, 255, cv2.THRESH_BINARY)
layer = cv2.bitwise_and(slice, slice, mask=mask[:,:,0])
out = cv2.add(out, layer, dtype=0)
return out