-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
clock_deblur.py
60 lines (43 loc) · 1.53 KB
/
clock_deblur.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
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color
from skimage.viewer import ImageViewer
from skimage.viewer.widgets import Slider
from skimage.viewer.plugins.base import Plugin
image = io.imread('data/clock_motion.png')
M, N = image.shape
## Should pad, but doesn't make much difference in this case
MM, NN = 2 * M + 1, 2 * N + 1
def hann(image):
wy = np.hanning(image.shape[0])[:, None]
wx = np.hanning(image.shape[1])
## Apply Hann window to prevent ringing
wy = np.hanning(M)[:, None]
wx = np.hanning(N)
f = np.zeros((MM, NN))
f[:M, :N] = wy * wx * image
F = np.fft.fft2(f)
v, u = np.ogrid[:MM, :NN]
v -= (MM - 1) // 2
u -= (NN - 1) // 2
def apply_inverse_filter(image, T, a, b, K=5, clip=500):
uavb = u * a + v * b
H = T * np.sinc(uavb) * np.exp(-1j * np.pi * uavb)
H = np.fft.fftshift(H)
HH = 1./H
HH[np.abs(HH) > K] = K
gg = np.abs(np.fft.ifft2(F * HH))
gg = gg[:M, :N]
gg = np.clip(gg, 0, clip)
gg -= gg.min()
gg /= gg.max()
return gg
viewer = ImageViewer(image)
plugin = Plugin(image_filter=apply_inverse_filter)
plugin += Slider('T', 0, 1, value=0.5, value_type='float', update_on='release')
plugin += Slider('a', -0.1, 0.1, value=0, value_type='float', update_on='release')
plugin += Slider('b', -0.1, 0.1, value=0, value_type='float', update_on='release')
plugin += Slider('K', 0, 100, value=15, value_type='float', update_on='release')
plugin += Slider('clip', 0, 1000, value=750, value_type='float', update_on='release')
viewer += plugin
viewer.show()