-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmy_image_restore.py
59 lines (40 loc) · 1.32 KB
/
my_image_restore.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 29 22:17:59 2018
@author: prathmesh
"""
import numpy as np
import cv2
kernel_filename = 'estimate_4.png'
h = cv2.imread(kernel_filename,0)
image_filename = 'my_blur.png'
img_bgr = cv2.imread(image_filename,1)
restored = np.zeros(img_bgr.shape)
print(image_filename)
print(kernel_filename)
r=30
K=4000
Y = 90
p = np.array([(0, -1, 0),
(-1, 4, -1),
(0, -1, 0)])
for i in range (0,3):
g = img_bgr[:,:,i]
G = (np.fft.fft2(g))
h_padded = np.zeros(g.shape)
h_padded[:h.shape[0],:h.shape[1]] = np.copy(h)
H = (np.fft.fft2(h_padded))
p_padded = np.zeros(g.shape)
p_padded[:p.shape[0],:p.shape[1]] = np.copy(p)
P = (np.fft.fft2(p_padded))
H2 = (abs(H)**2 + Y*(abs(P)**2))/(np.conjugate(H))
H_norm = abs(H2/H2.max())
# Inverse Filter
F_hat = G / H_norm
#replace division by zero (NaN) with zeroes
#a = np.nan_to_num(F_hat)
f_hat = np.fft.ifft2( F_hat ) #- 50*np.ones(g.shape)
restored[:,:,i] = abs(f_hat)
out_filename = 'restored_out_cls.png'
cv2.imwrite(out_filename,restored)