-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalTransmission.py
194 lines (158 loc) · 7 KB
/
CalTransmission.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
import numpy as np
import cv2
def CalTransmission(HazeImg, Transmission, regularize_lambda, sigma):
rows, cols = Transmission.shape
KirschFilters = LoadFilterBank()
# Normalize the filters
for idx, currentFilter in enumerate(KirschFilters):
KirschFilters[idx] = KirschFilters[idx] / np.linalg.norm(currentFilter)
# Calculate Weighting function --> [rows, cols. numFilters] --> One Weighting function for every filter
WFun = []
for idx, currentFilter in enumerate(KirschFilters):
WFun.append(CalculateWeightingFunction(HazeImg, currentFilter, sigma))
# Precompute the constants that are later needed in the optimization step
tF = np.fft.fft2(Transmission)
DS = 0
for i in range(len(KirschFilters)):
D = psf2otf(KirschFilters[i], (rows, cols))
DS = DS + (abs(D) ** 2)
# Cyclic loop for refining t and u --> Section III in the paper
beta = 1 # Start Beta value --> selected from the paper
beta_max = 2**8 # Selected from the paper --> Section III --> "Scene Transmission Estimation"
beta_rate = 2*np.sqrt(2) # Selected from the paper
while(beta < beta_max):
gamma = regularize_lambda / beta
# Fixing t first and solving for u
DU = 0
for i in range(len(KirschFilters)):
dt = circularConvFilt(Transmission, KirschFilters[i])
u = np.maximum((abs(dt) - (WFun[i] / (len(KirschFilters)*beta))), 0) * np.sign(dt)
DU = DU + np.fft.fft2(circularConvFilt(u, cv2.flip(KirschFilters[i], -1)))
# Fixing u and solving t --> Equation 26 in the paper
# Note: In equation 26, the Numerator is the "DU" calculated in the above part of the code
# In the equation 26, the Denominator is the DS which was computed as a constant in the above code
Transmission = np.abs(np.fft.ifft2((gamma * tF + DU) / (gamma + DS)))
beta = beta * beta_rate
return(Transmission)
def LoadFilterBank():
KirschFilters = []
KirschFilters.append(np.array([[-3, -3, -3], [-3, 0, 5], [-3, 5, 5]]))
KirschFilters.append(np.array([[-3, -3, -3], [-3, 0, -3], [5, 5, 5]]))
KirschFilters.append(np.array([[-3, -3, -3], [5, 0, -3], [5, 5, -3]]))
KirschFilters.append(np.array([[5, -3, -3], [5, 0, -3], [5, -3, -3]]))
KirschFilters.append(np.array([[5, 5, -3], [5, 0, -3], [-3, -3, -3]]))
KirschFilters.append(np.array([[5, 5, 5], [-3, 0, -3], [-3, -3, -3]]))
KirschFilters.append(np.array([[-3, 5, 5], [-3, 0, 5], [-3, -3, -3]]))
KirschFilters.append(np.array([[-3, -3, 5], [-3, 0, 5], [-3, -3, 5]]))
KirschFilters.append(np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]))
return(KirschFilters)
def CalculateWeightingFunction(HazeImg, Filter, sigma):
# Computing the weight function... Eq (17) in the paper
HazeImageDouble = HazeImg.astype(float) / 255.0
if(len(HazeImg.shape) == 3):
Red = HazeImageDouble[:, :, 2]
d_r = circularConvFilt(Red, Filter)
Green = HazeImageDouble[:, :, 1]
d_g = circularConvFilt(Green, Filter)
Blue = HazeImageDouble[:, :, 0]
d_b = circularConvFilt(Blue, Filter)
WFun = np.exp(-((d_r**2) + (d_g**2) + (d_b**2)) / (2 * sigma * sigma))
else:
d = circularConvFilt(HazeImageDouble, Filter)
WFun = np.exp(-((d ** 2) + (d ** 2) + (d ** 2)) / (2 * sigma * sigma))
return(WFun)
def circularConvFilt(Img, Filter):
FilterHeight, FilterWidth = Filter.shape
assert (FilterHeight == FilterWidth), 'Filter must be square in shape --> Height must be same as width'
assert (FilterHeight % 2 == 1), 'Filter dimension must be a odd number.'
filterHalsSize = int((FilterHeight - 1)/2)
rows, cols = Img.shape
PaddedImg = cv2.copyMakeBorder(Img, filterHalsSize, filterHalsSize, filterHalsSize, filterHalsSize, borderType=cv2.BORDER_WRAP)
FilteredImg = cv2.filter2D(PaddedImg, -1, Filter)
Result = FilteredImg[filterHalsSize:rows+filterHalsSize, filterHalsSize:cols+filterHalsSize]
return(Result)
##################
def psf2otf(psf, shape):
"""
Convert point-spread function to optical transfer function.
Compute the Fast Fourier Transform (FFT) of the point-spread
function (PSF) array and creates the optical transfer function (OTF)
array that is not influenced by the PSF off-centering.
By default, the OTF array is the same size as the PSF array.
To ensure that the OTF is not altered due to PSF off-centering, PSF2OTF
post-pads the PSF array (down or to the right) with zeros to match
dimensions specified in OUTSIZE, then circularly shifts the values of
the PSF array up (or to the left) until the central pixel reaches (1,1)
position.
Parameters
----------
psf : `numpy.ndarray`
PSF array
shape : int
Output shape of the OTF array
Returns
-------
otf : `numpy.ndarray`
OTF array
Notes
-----
Adapted from MATLAB psf2otf function
"""
if np.all(psf == 0):
return np.zeros_like(psf)
inshape = psf.shape
# Pad the PSF to outsize
psf = zero_pad(psf, shape, position='corner')
# Circularly shift OTF so that the 'center' of the PSF is
# [0,0] element of the array
for axis, axis_size in enumerate(inshape):
psf = np.roll(psf, -int(axis_size / 2), axis=axis)
# Compute the OTF
otf = np.fft.fft2(psf)
# Estimate the rough number of operations involved in the FFT
# and discard the PSF imaginary part if within roundoff error
# roundoff error = machine epsilon = sys.float_info.epsilon
# or np.finfo().eps
n_ops = np.sum(psf.size * np.log2(psf.shape))
otf = np.real_if_close(otf, tol=n_ops)
return otf
def zero_pad(image, shape, position='corner'):
"""
Extends image to a certain size with zeros
Parameters
----------
image: real 2d `numpy.ndarray`
Input image
shape: tuple of int
Desired output shape of the image
position : str, optional
The position of the input image in the output one:
* 'corner'
top-left corner (default)
* 'center'
centered
Returns
-------
padded_img: real `numpy.ndarray`
The zero-padded image
"""
shape = np.asarray(shape, dtype=int)
imshape = np.asarray(image.shape, dtype=int)
if np.alltrue(imshape == shape):
return image
if np.any(shape <= 0):
raise ValueError("ZERO_PAD: null or negative shape given")
dshape = shape - imshape
if np.any(dshape < 0):
raise ValueError("ZERO_PAD: target size smaller than source one")
pad_img = np.zeros(shape, dtype=image.dtype)
idx, idy = np.indices(imshape)
if position == 'center':
if np.any(dshape % 2 != 0):
raise ValueError("ZERO_PAD: source and target shapes "
"have different parity.")
offx, offy = dshape // 2
else:
offx, offy = (0, 0)
pad_img[idx + offx, idy + offy] = image
return pad_img