-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
165 lines (108 loc) · 3.38 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
164
165
import cv2
import numpy as np
from tqdm import tqdm
def water_filling(img):
original_shape = img.shape
img = cv2.resize(img, (0, 0), fx=0.2, fy=0.2,
interpolation=cv2.INTER_LINEAR_EXACT)
h, w = img.shape
neta = 0.2
# Water
w_ = np.zeros((h, w), dtype=np.float)
# Overall height
G_ = np.zeros((h, w), dtype=np.float)
h_ = img.copy()
h_ = img.astype(np.float)
x = np.linspace(1, w-2, w-2)
y = np.linspace(1, h-2, h-2)
X, Y = np.meshgrid(x, y)
X = X.astype(np.uint)
Y = Y.astype(np.uint)
# Left (x-delta)
lx, ly = X-1, Y
# Right (x+delta)
rx, ry = X+1, Y
# Top (y-delta)
tx, ty = X, Y-1
# Btm (y+delta)
bx, by = X, Y+1
print("[MSG] Water filling in progress ...")
for t in tqdm(range(2500)):
G_ = w_ + h_
# Find peak
G_peak = np.amax(G_)
pouring = np.exp(-t) * (G_peak - G_)
left = -G_[Y, X] + G_[ly, lx]
left[left > 0] = 0
right = -G_[Y, X] + G_[ry, rx]
right[right > 0] = 0
top = -G_[Y, X] + G_[ty, tx]
top[top > 0] = 0
btm = -G_[Y, X] + G_[by, bx]
btm[btm > 0] = 0
del_w = neta * (left + right + top + btm)
# del_w : (w-2) * (h-2)
# pouring : w * h
# w_ : w * h
# To match the shape of del_w, padding is required
del_w = np.pad(del_w, ((1, 1), (1, 1)),
'constant', constant_values=0)
temp = del_w + pouring + w_
temp[temp < 0] = 0
w_[1: h - 2, 1: w - 2] = temp[1: h - 2, 1: w - 2]
h, w = original_shape
G_ = cv2.resize(G_, (w, h), interpolation=cv2.INTER_LINEAR)
G_ = G_.astype(np.uint8)
return G_
def incre_filling(h_, original):
h_ = h_.astype(np.float)
original = original.astype(np.float)
h, w = h_.shape
neta = 0.2
# Water
w_ = np.zeros((h, w), dtype=np.float)
# Overall height
G_ = np.zeros((h, w), dtype=np.float)
x = np.linspace(1, w-2, w-2)
y = np.linspace(1, h-2, h-2)
X, Y = np.meshgrid(x, y)
X = X.astype(np.uint)
Y = Y.astype(np.uint)
lx, ly = X-1, Y
rx, ry = X+1, Y
tx, ty = X, Y-1
bx, by = X, Y+1
print("[MSG] Incremental filling in progress ...")
for t in tqdm(range(100)):
G_ = w_ + h_
left = -G_[Y, X] + G_[ly, lx]
right = -G_[Y, X] + G_[ry, rx]
top = -G_[Y, X] + G_[ty, tx]
btm = -G_[Y, X] + G_[by, bx]
del_w = neta * (left + right + top + btm)
# del_w : (w-2) * (h-2)
# pouring : w * h
# w_ : w * h
# To match the shape of del_w, padding is required
del_w = np.pad(del_w, ((1, 1), (1, 1)),
'constant', constant_values=0)
temp = del_w + w_
temp[temp < 0] = 0
w_[1: h - 2, 1: w - 2] = temp[1: h - 2, 1: w - 2]
output = 0.85 * original / G_ * 255
output = output.astype(np.uint8)
return output
def main():
img = cv2.imread("./original_14_small.png")
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
y, cr, cb = cv2.split(ycrcb)
G_ = water_filling(y)
G_ = incre_filling(G_, y)
merged = cv2.merge([G_, cr, cb], 3)
merged = cv2.cvtColor(merged, cv2.COLOR_YCrCb2BGR)
cv2.imshow("after", merged)
cv2.imshow("before", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()