-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolour_attenuation.py
126 lines (102 loc) · 3.45 KB
/
colour_attenuation.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
# Import all the necessary packages to your arsenal
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy import signal as sig
import math
# import guidedfilter
# from guidedfilter import guidedfilter as gF
def guide(I,P,r,e):
h,w=np.shape(I)
window = np.ones((r,r))/(r*r)
meanI = sig.convolve2d(I, window,mode='same')
meanP = sig.convolve2d(P, window,mode='same')
corrI = sig.convolve2d(I*I, window,mode='same')
corrIP = sig.convolve2d(I*P, window,mode='same')
varI = corrI - meanI*meanI
covIP = corrIP - meanI*meanP
a = covIP/(varI+e)
b = meanP - a*meanI
meana = sig.convolve2d(a, window,mode='same')
meanb = sig.convolve2d(b, window,mode='same')
q = meana*I+meanb
return q
def localmin(D, r=15):
R = int(r/2)
imax = D.shape[0]
jmax = D.shape[1]
LM = np.zeros([imax,jmax])
for i in np.arange(D.shape[0]):
for j in np.arange(D.shape[1]):
iL = np.max([i-R,0])
iR = np.min([i+R, imax])
jT = np.max([j-R,0])
jB = np.min([j+R, jmax])
# print(D[iL:iR+1,jT:jB+1].shape)
LM[i,j] = np.min(D[iL:iR+1,jT:jB+1])
return LM
def postprocessing(GD, I):
# this will give indices of the columnised image GD
flat_indices = np.argsort(GD, axis=None)
R,C = GD.shape
top_indices_flat = flat_indices[ int(np.round(0.999*R*C)):: ]
top_indices = np.unravel_index(top_indices_flat, GD.shape)
max_v_index = np.unravel_index( np.argmax(V[top_indices], axis=None), V.shape )
I = I/255.0
A = I[max_v_index[0], max_v_index[1], :]
print('Atmosphere A = (r, g, b)')
print(A)
beta = 1.0
transmission = np.minimum( np.maximum(np.exp(-1*beta*GD), 0.1) , 0.9)
# transmission = np.exp(-1*beta*GD)
transmission3 = np.zeros(I.shape)
transmission3[:,:,0] = transmission
transmission3[:,:,1] = transmission
transmission3[:,:,2] = transmission
J = A + (I - A)/transmission3
J = J - np.min(J)
J = J/np.max(J)
return J
if __name__ == '__main__':
import sys
try:
fn = sys.argv[1]
except:
fn = './images/foggy_building.png'
# Read the Image
_I = cv2.imread(fn)
# opencv reads any image in Blue-Green-Red(BGR) format,
# so change it to RGB format, which is popular.
I = cv2.cvtColor(_I, cv2.COLOR_BGR2RGB)
# Split Image to Hue-Saturation-Value(HSV) format.
H,S,V = cv2.split(cv2.cvtColor(_I, cv2.COLOR_BGR2HSV) )
V = V/255.0
S = S/255.0
# Calculating Depth Map using the linear model fit by ZHU et al.
# Refer Eq(8) in mentioned research paper
# Values given under EXPERIMENTS section
theta_0 = 0.121779
theta_1 = 0.959710
theta_2 = -0.780245
sigma = 0.041337
epsilon = np.random.normal(0, sigma, H.shape )
D = theta_0 + theta_1*V + theta_2*S + epsilon
# Local Minima of Depth map
LMD = localmin(D, 15)
# LMD = D
# Guided Filtering
r = 8; # try r=2, 4, 8 or 18
eps = 0.2 * 0.2; # try eps=0.1^2, 0.2^2, 0.4^2
# eps *= 255 * 255; # Because the intensity range of our images is [0, 255]
GD=guide(D,LMD,r,eps)
J = postprocessing(GD, I)
# Plot the generated raw depth map
# plt.subplot(121)
plt.imshow(J)
plt.title('Dehazed Image')
plt.xticks([]); plt.yticks([])
plt.show()
# save the depthmap.
# Note: It will be saved as gray image.
# cv2.imwrite('../data/dehazed/' + filename, J)
# plt.imsave('dehazed.jpg',J)