-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCP_ImageDehazing.py
246 lines (179 loc) · 7.07 KB
/
DCP_ImageDehazing.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#DCP AND GUIDED FILTERS
import math
import numpy as np
import pandas as pd
import cv2
import time
import matplotlib.pyplot as plt
#matplotlib inline
#from Ipython.display import display
class Node(object):
def __init__(self,x,y,value):
self.x = x
self.y = y
self.value = value
def printInfo(self):
print('%s:%s:%s' %(self.x,self.y,self.value))
def getMinChannel(img):
if len(img.shape)==3 and img.shape[2]==3:
pass
else:
print("bad image shape, input must be color image")
return None
return np.min(img, axis=2)
def getDarkChannel(img, blockSize = 3):
if len(img.shape)==2:
pass
else:
print("bad image shape, input image must be two demensions")
return None
A = int((blockSize-1)/2)
#New height and new width
H = img.shape[0] + blockSize - 1
W = img.shape[1] + blockSize - 1
imgMiddle = 255 * np.ones((H,W))
imgMiddle[A:H-A, A:W-A] = img
imgDark = np.zeros_like(img, np.uint8)
localMin = 255
for i in range(A, H-A):
for j in range(A, W-A):
x = range(i-A, i+A+1)
y = range(j-A, j+A+1)
imgDark[i-A,j-A] = np.min(imgMiddle[x,y])
return imgDark
def getAtomsphericLight(darkChannel,img,meanMode = False, percent = 0.001):
size = darkChannel.shape[0]*darkChannel.shape[1]
height = darkChannel.shape[0]
width = darkChannel.shape[1]
nodes = []
for i in range(0,height):
for j in range(0,width):
oneNode = Node(i,j,darkChannel[i,j])
nodes.append(oneNode)
nodes = sorted(nodes, key = lambda node: node.value,reverse = True)
atomsphericLight = 0
if int(percent*size) == 0:
sum = 0
for i in range(0,int(percent*size)):
for j in range(0,3):
sum = sum + img[nodes[i].x,nodes[i].y,j]
atomsphericLight = int(sum/(int(percent*size)*3))
return atomsphericLight
def getRecoverScene(img, omega=0.95, t0=0.1, blockSize=15, meanMode=False, percent=0.001, refine=True):
imgGray = getMinChannel(img)
imgDark = getDarkChannel(imgGray, blockSize = blockSize)
atomsphericLight = getAtomsphericLight(imgDark,img,meanMode = meanMode,percent= percent)
imgDark = np.float64(imgDark)
transmission = 1 - omega * imgDark / atomsphericLight
transmission[transmission<0.1] = 0.1
if refine:
normI = (img - img.min()) / (img.max() - img.min()) # normalize I
transmission = guided_filter(normI, transmission, r=40, eps=1e-3)
sceneRadiance = np.zeros(img.shape)
img = np.float64(img)
for i in range(3):
SR = (img[:,:,i] - atomsphericLight)/transmission + atomsphericLight
# 限制透射率 在0~255
SR[SR>255] = 255
SR[SR<0] = 0
sceneRadiance[:,:,i] = SR
sceneRadiance = np.uint8(sceneRadiance)
return sceneRadiance
"""Implementation for Guided Image Filtering
Reference:
http://research.microsoft.com/en-us/um/people/kahe/eccv10/
"""
from itertools import combinations_with_replacement
from collections import defaultdict
import numpy as np
from numpy.linalg import inv
R, G, B = 0, 1, 2 # index for convenience
def boxfilter(I, r):
"""Fast box filter implementation.
Parameters
----------
I: a single channel/gray image data normalized to [0.0, 1.0]
r: window radius
Return
-----------
The filtered image data.
"""
M, N = I.shape
dest = np.zeros((M, N))
# cumulative sum over Y axis
sumY = np.cumsum(I, axis=0)
# difference over Y axis
dest[:r + 1] = sumY[r: 2 * r + 1]
dest[r + 1:M - r] = sumY[2 * r + 1:] - sumY[:M - 2 * r - 1]
dest[-r:] = np.tile(sumY[-1], (r, 1)) - sumY[M - 2 * r - 1:M - r - 1]
# cumulative sum over X axis
sumX = np.cumsum(dest, axis=1)
# difference over Y axis
dest[:, :r + 1] = sumX[:, r:2 * r + 1]
dest[:, r + 1:N - r] = sumX[:, 2 * r + 1:] - sumX[:, :N - 2 * r - 1]
dest[:, -r:] = np.tile(sumX[:, -1][:, None], (1, r)) - \
sumX[:, N - 2 * r - 1:N - r - 1]
return dest
def guided_filter(I, p, r=40, eps=1e-3):
"""Refine a filter under the guidance of another (RGB) image.
Parameters
-----------
I: an M * N * 3 RGB image for guidance.
p: the M * N filter to be guided
r: the radius of the guidance
eps: epsilon for the guided filter
Return
-----------
The guided filter.
"""
M, N = p.shape
base = boxfilter(np.ones((M, N)), r)
# each channel of I filtered with the mean filter
means = [boxfilter(I[:, :, i], r) / base for i in range(3)]
# p filtered with the mean filter
mean_p = boxfilter(p, r) / base
# filter I with p then filter it with the mean filter
means_IP = [boxfilter(I[:, :, i] * p, r) / base for i in range(3)]
# covariance of (I, p) in each local patch
covIP = [means_IP[i] - means[i] * mean_p for i in range(3)]
# variance of I in each local patch: the matrix Sigma in ECCV10 eq.14
var = defaultdict(dict)
for i, j in combinations_with_replacement(range(3), 2):
var[i][j] = boxfilter(
I[:, :, i] * I[:, :, j], r) / base - means[i] * means[j]
a = np.zeros((M, N, 3))
for y, x in np.ndindex(M, N):
# rr, rg, rb
# Sigma = rg, gg, gb
# rb, gb, bb
Sigma = np.array([[var[R][R][y, x], var[R][G][y, x], var[R][B][y, x]],
[var[R][G][y, x], var[G][G][y, x], var[G][B][y, x]],
[var[R][B][y, x], var[G][B][y, x], var[B][B][y, x]]])
cov = np.array([c[y, x] for c in covIP])
a[y, x] = np.dot(cov, inv(Sigma + eps * np.eye(3))) # eq 14
# ECCV10 eq.15
b = mean_p - a[:, :, R] * means[R] - \
a[:, :, G] * means[G] - a[:, :, B] * means[B]
# ECCV10 eq.16
q = (boxfilter(a[:, :, R], r) * I[:, :, R] + boxfilter(a[:, :, G], r) *
I[:, :, G] + boxfilter(a[:, :, B], r) * I[:, :, B] + boxfilter(b, r)) / base
return q
#### IMPLEMENT ON IMAGE
Hazy_img_idx = [104, 3007, 3794, 23710, 38469]
for i in Hazy_img_idx:
#path = '../input/train-jpg/'
#filename = 'train_{}.jpg'.format(i)
#img = cv2.imread(path+filename) #0-255
img = cv2.imread('909_img_.png')
dehazed_img1 = getRecoverScene(img, refine=True)
dehazed_img2 = getRecoverScene(img, refine=False)
fig = plt.figure()
fig.set_size_inches(12, 4)
fig.suptitle(filename + ' Tags: ' + df_train['tags'][i], fontsize=12)
plt.subplot(131)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.subplot(132)
plt.imshow(cv2.cvtColor(dehazed_img1, cv2.COLOR_BGR2RGB))
plt.subplot(133)
plt.imshow(cv2.cvtColor(dehazed_img2, cv2.COLOR_BGR2RGB))
plt.show()