-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
165 lines (134 loc) · 4.13 KB
/
main.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
## Final Project
# Brannen Sorem
import sys
import os
import numpy as np
import cv2
# [0]main.py [1]filename [2]depth [3]num_layers
if len(sys.argv) < 3:
print "You need to include a source image and depth-map image. See README.md"
exit()
file_name = sys.argv[1]
depth_map = sys.argv[2]
NUM_LAYERS = int(sys.argv[3]) if len(sys.argv) > 3 else 15
img = cv2.imread(file_name)
row,col,a = img.shape
roi = img[0:row, 0:col]
depth = cv2.imread(depth_map)
out = np.zeros(img.shape)
min_depth = np.min(np.unique(depth))
max_depth = np.max(np.unique(depth))
s = (max_depth - min_depth) // NUM_LAYERS
layers = np.array(range(min_depth, max_depth, s))
###############################################
### Get layer mask (slice)
###############################################
# Create mask layer between values (s)tart <-> (e)nd
def layer_mask(img, s, e):
# create mask in single, grayscale channel
dm = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# copy image dimensions, but fill with zeros
m = np.zeros(dm.shape)
# set values above start threshold to white
m[dm > s] = 255
# set values above end threshold to black
m[dm > e] = 0
# set mask to 3 channels
cp = np.zeros(img.shape)
cp[:,:,0] = m[:,:]
cp[:,:,1] = m[:,:]
cp[:,:,2] = m[:,:]
# messy way to get correct format for OpenCV
cv2.imwrite('tmp.jpg', cp)
o = cv2.imread('tmp.jpg')
os.remove('tmp.jpg')
return o
###############################################
### Filters
###############################################
# Change blur by epsilon value (a)
def blur_filter(img, a):
# increase kernel effect slowly, must be odd
k = (a / 10) + 1 if (a / 10) % 2 == 0 else (a / 10) + 2
# can't exceed 255
k = k if k < 255 else 255
kernel = (k, k)
# blur filter
o = cv2.GaussianBlur(img, kernel, 5)
return o
# Change hue by epsilon value (a)
def hue_filter(img, a):
tmp = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
o = tmp
o[:,:,0] = tmp[:,:,0] + a
o = cv2.cvtColor(o, cv2.COLOR_HSV2BGR)
return o
# Change saturation by epsilon value (a)
def saturation_filter(img, a):
tmp = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
o = tmp
# normalize 180->0 by layer sizes
v = int(180 - (float(a.item()) / max_depth) * (180+min_depth))
o[:,:,1] = float(tmp[500,500,1]) * (float(v) / 180)
o = cv2.cvtColor(o, cv2.COLOR_HSV2BGR)
return o
# Change edge amount by epsilon value (a)
def edge_filter(img, a):
lap = cv2.Laplacian(img, 8)
b = float(a.item())
k = (255-b+20) / 255
j = 1 - k
o = cv2.addWeighted(img, k, lap, j, 0, dtype=0)
return o
# Change darkness by epsilon value (a)
def night_filter(img, a):
o = np.zeros(img.shape)
b = float(a.item())
k = (255-b+20) / 255
j = 1 - k
o = cv2.addWeighted(img, k, o, j, 0, dtype=0)
return o
# Change lightness by epsilon value (a)
def fog_filter(img, a):
o = np.zeros(img.shape)
o[o == 0] = 255
b = float(a.item())
k = (255-b+20) / 255
j = 1 - k
o = cv2.addWeighted(img, k, o, j, 0, dtype=0)
return o
###############################################
### Filters
###############################################
# Get layer slice in correct format
def get_layer(slice, l_mask):
ret, mask = cv2.threshold(l_mask, 100, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
layer = cv2.bitwise_and(slice, slice, mask = mask[:,:,0])
return layer, mask, mask_inv
###############################################
### For each slice of the image
###############################################
# Run mask on all layers and combine into output (out)
for a in layers:
l_mask = layer_mask(depth, a, a+s)
# res = blur_filter(img, a - min_depth)
# res = hue_filter(img, a - min_depth)
# res = saturation_filter(img, a - min_depth)
# res = edge_filter(img, a)
res = fog_filter(img, a)
# res = night_filter(img, a)
layer, mask, mask_inv = get_layer(res, l_mask)
out = cv2.add(out, layer, dtype=0)
###############################################
### Display
###############################################
# rescale image for display
h,w,c = out.shape
ha = h*2 // 3
wa = w*2 // 3
# show results in window
out = cv2.resize(out, (wa,ha))
cv2.imshow('Depth Filters', out)
cv2.waitKey(0)
# cv2.imwrite("output.jpg", out)