-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filters.py
223 lines (159 loc) · 8.11 KB
/
Filters.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
# coding: utf-8
# In[4]:
import numpy as np
from PIL import Image, ImageTk
from matplotlib import pyplot as plt
from scipy import fftpack
import cv2
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from __future__ import print_function
from scipy import signal
import tensorflow as tf
from skimage.measure import compare_ssim as ssim_function
from skimage.measure import compare_psnr as psnr_function
# In[5]:
def inverse_filtering(img1, kernel, R, ground_truth):
img = img1/255.0 #Scaling image to [0,1]
kernel = kernel/255.0 #Scaling kernel to [0,1]
#Padding kernel to the size of the image
kernel = np.pad(kernel, ((0,img.shape[0] - kernel.shape[0]),(0,img.shape[1] - kernel.shape[1])),
'constant', constant_values=kernel.min())
#Taking FFT
kernel_fft = np.fft.fftshift(np.fft.fft2(kernel))
ffti0=np.fft.fft2(img[...,0])
ffti1=np.fft.fft2(img[...,1])
ffti2=np.fft.fft2(img[...,2])
#Defining the deblurring filter
inv_filter = 1.0/kernel_fft
#Creating a truncation radius
for i in range(ffti0.shape[0]):
for j in range(ffti1.shape[1]):
if((i-ffti0.shape[0]/2)**2+(j-ffti0.shape[1]/2)**2>=R**2):
inv_filter[i,j]=0
output1 = np.zeros((img.shape[0],img.shape[1],3))
output2 = np.zeros((img.shape[0],img.shape[1],3))
#Multiplying the FFTs and then taking inverse
output1[...,0] = np.real(np.fft.ifft2(1.0*ffti0*inv_filter))
output1[...,1] = np.real(np.fft.ifft2(1.0*ffti1*inv_filter))
output1[...,2] = np.real(np.fft.ifft2(1.0*ffti2*inv_filter))
#Scaling the output back to the original pixel intensities
output2[:,:,0]=np.interp(output1[:,:,0], (output1[:,:,0].min(), output1[:,:,0].max()), (img1[:,:,0].min(), img1[:,:,0].max()))
output2[:,:,1]=np.interp(output1[:,:,1], (output1[:,:,1].min(), output1[:,:,1].max()), (img1[:,:,0].min(), img1[:,:,1].max()))
output2[:,:,2]=np.interp(output1[:,:,2], (output1[:,:,2].min(), output1[:,:,2].max()), (img1[:,:,0].min(), img1[:,:,2].max()))
output = Image.fromarray(output2.astype('uint8'))
output.save('output.png')
#Calculating the PSNR
psnr_1 = psnr_function(ground_truth, output2)
# print(R, ' PSNR:',psnr_1)
#Calculating the SSIM
ssim_1 = ssim_function(output2, ground_truth, multichannel=True)
# print(R, ' SSIM:', ssim_1)
return psnr_1, ssim_1
# In[6]:
def wiener_filtering(img1, kernel, K, ground_truth):
img = img1/255.0 #Scaling image to [0,1]
kernel = kernel/255.0 #Scaling kernel to [0,1]
#Padding kernel to the size of the image and taking FFT
kernel = np.pad(kernel, ((0,img.shape[0] - kernel.shape[0]),(0,img.shape[1] - kernel.shape[1])),'constant', constant_values=kernel.min())
kernel_fft = np.fft.fftshift(np.fft.fft2(kernel))
#Defining the deblurring filter
wiener_filter = np.conj(kernel_fft) / (np.abs((kernel_fft)**2) + K)
#Taking FFT of image channels
ffti0=np.fft.fft2(img[...,0])
ffti1=np.fft.fft2(img[...,1])
ffti2=np.fft.fft2(img[...,2])
output1 = np.zeros((img.shape[0],img.shape[1],3))
output2 = np.zeros((img.shape[0],img.shape[1],3))
#Multiplying the FFTs and filter and then taking inverse
output1[...,0] = np.real(np.fft.ifft2(1.0*ffti0*wiener_filter))
output1[...,1] = np.real(np.fft.ifft2(1.0*ffti1*wiener_filter))
output1[...,2] = np.real(np.fft.ifft2(1.0*ffti2*wiener_filter))
#Scaling the output back to the original pixel intensities
output2[:,:,0]=np.interp(output1[:,:,0], (output1[:,:,0].min(), output1[:,:,0].max()),(img1[:,:,0].min(), img1[:,:,0].max()))
output2[:,:,1]=np.interp(output1[:,:,1], (output1[:,:,1].min(), output1[:,:,1].max()),(img1[:,:,0].min(), img1[:,:,0].max()))
output2[:,:,2]=np.interp(output1[:,:,2], (output1[:,:,2].min(), output1[:,:,2].max()),(img1[:,:,0].min(), img1[:,:,0].max()))
output = Image.fromarray(output2.astype('uint8'))
output.save('wiener_output.png')
#Calculating the PSNR
psnr_1 = psnr_function(ground_truth, output2)
# print(K, ' PSNR:', psnr_1)
#Calculating the SSIM
ssim_1 = ssim_function(ground_truth, output2, multichannel=True)
# print(K, ' SSIM:', ssim_1)
return psnr_1, ssim_1
# In[7]:
def cls_filtering(img1, kernel, gamma, ground_truth):
img = img1/255.0 #Scaling image to [0,1]
kernel = kernel/255.0 #Scaling kernel to [0,1]
#Padding kernel to the size of the image and taking FFT
kernel = np.pad(kernel, ((0,img.shape[0] - kernel.shape[0]),(0,img.shape[1] - kernel.shape[1])),
'constant', constant_values=kernel.min())
kernel_fft = np.fft.fftshift(np.fft.fft2(kernel))
#Defining the deblurring filter
p1 = np.array(([0, -1, 0],[-1, 4, -1],[0, -1, 0]))
p1 = np.pad(p1, ((0,img.shape[0] - p1.shape[0]),(0,img.shape[1] - p1.shape[1])),
'constant', constant_values=0)
P = np.fft.fft2(p1)
cls_filter = np.conj(kernel_fft) / (np.abs(kernel_fft)**2 + (gamma*(np.abs(P)**2)))
#Taking FFT of image channels
ffti0=np.fft.fft2(img[...,0])
ffti1=np.fft.fft2(img[...,1])
ffti2=np.fft.fft2(img[...,2])
output1 = np.zeros((img.shape[0],img.shape[1],3))
output2 = np.zeros((img.shape[0],img.shape[1],3))
#Multiplying the FFTs and filter and then taking inverse
output1[...,0] = np.real(np.fft.ifft2(1.0*ffti0*cls_filter))
output1[...,1] = np.real(np.fft.ifft2(1.0*ffti1*cls_filter))
output1[...,2] = np.real(np.fft.ifft2(1.0*ffti2*cls_filter))
#Scaling the output back to the original pixel intensities
output2[:,:,0]=np.interp(output1[:,:,0], (output1[:,:,0].min(), output1[:,:,0].max()),
(img1[:,:,0].min(), img1[:,:,0].max()))
output2[:,:,1]=np.interp(output1[:,:,1], (output1[:,:,1].min(), output1[:,:,1].max()),
(img1[:,:,0].min(), img1[:,:,1].max()))
output2[:,:,2]=np.interp(output1[:,:,2], (output1[:,:,2].min(), output1[:,:,2].max()),
(img1[:,:,0].min(), img1[:,:,2].max()))
output = Image.fromarray(output2.astype('uint8'))
output.save('cls_output.png')
#Calculating the PSNR
psnr_1 = psnr_function(ground_truth, output2)
# print(K, ' PSNR:', psnr_1)
#Calculating the SSIM
ssim_1 = ssim_function(ground_truth, output2, multichannel=True)
# print(K, ' SSIM:', ssim_1)
return psnr_1, ssim_1
# In[11]:
## Performing an experiment by blurring the Ground Truth image to get a blurred image,
## then using the filtering methods to unblur it.
blur = Image.open('Kernel1.png')
kernel = np.array(blur)
ground_truth=Image.open('GroundTruth1_1_1.jpg')
img1=np.array(ground_truth)
img = img1/255.0
kernel = kernel/255.0
kernel = np.pad(kernel, ((0,img.shape[0] - kernel.shape[0]),(0,img.shape[1] - kernel.shape[1])),
'constant', constant_values=kernel.min())
kernel_fft = np.fft.fftshift(np.fft.fft2(kernel))
ffti0=np.fft.fft2(img[...,0])
ffti1=np.fft.fft2(img[...,1])
ffti2=np.fft.fft2(img[...,2])
output1 = np.zeros((img.shape[0],img.shape[1],3))
output2 = np.zeros((img.shape[0],img.shape[1],3))
output1[...,0] = np.real(np.fft.ifft2(1.0*ffti0*kernel_fft))
output1[...,1] = np.real(np.fft.ifft2(1.0*ffti1*kernel_fft))
output1[...,2] = np.real(np.fft.ifft2(1.0*ffti2*kernel_fft))
output2[:,:,0]=np.interp(output1[:,:,0], (output1[:,:,0].min(), output1[:,:,0].max()),
(img1[:,:,0].min(), img1[:,:,0].max()))
output2[:,:,1]=np.interp(output1[:,:,1], (output1[:,:,1].min(), output1[:,:,1].max()),
(img1[:,:,0].min(), img1[:,:,1].max()))
output2[:,:,2]=np.interp(output1[:,:,2], (output1[:,:,2].min(), output1[:,:,2].max()),
(img1[:,:,0].min(), img1[:,:,2].max()))
output = Image.fromarray(output2.astype('uint8'))
output.save('test.png')
ground_truth=Image.open('GroundTruth1_1_1.jpg')
gt=np.array(ground_truth)
blur = Image.open('Kernel1.png')
blr = np.array(blur)
x=Image.open('test.png')
x=np.array(x)
psnr,ssim=inverse_filtering(x, blr, 1000, gt)