-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
58 lines (47 loc) · 1.54 KB
/
main.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
from PIL import Image
import matplotlib.pyplot as ppt
import numpy
# 仿真运动模糊
def motion_process(len, size):
sx, sy = size
PSF = numpy.zeros((sy, sx))
PSF[int(sy / 2):int(sy /2 + 1), int(sx / 2 - len / 2):int(sx / 2 + len / 2)] = 1
return PSF / PSF.sum() # 归一化亮度
def make_blurred(input, PSF, eps):
input_fft = numpy.fft.fft2(input)
PSF_fft = numpy.fft.fft2(PSF) + eps
blurred = numpy.fft.ifft2(input_fft * PSF_fft)
blurred = numpy.abs(numpy.fft.fftshift(blurred))
return blurred
def wiener(input, PSF, eps):
input_fft = numpy.fft.fft2(input)
PSF_fft = numpy.fft.fft2(PSF) + eps #噪声功率,这是已知的,考虑epsilon
result = numpy.fft.ifft2(input_fft / PSF_fft) #计算F(u,v)的傅里叶反变换
result = numpy.abs(numpy.fft.fftshift(result))
return result
image = Image.open('lena.jpg').convert('L')
ppt.figure(1)
ppt.xlabel("Original Image")
ppt.gray()
ppt.imshow(image)
ppt.figure(2)
ppt.gray()
data = numpy.asarray(image.getdata()).reshape(image.size)
PSF = motion_process(30, data.shape)
blurred = numpy.abs(make_blurred(data, PSF, 1e-3))
ppt.subplot(221)
ppt.xlabel("Motion blurred")
ppt.imshow(blurred)
result = wiener(blurred, PSF, 1e-3)
ppt.subplot(222)
ppt.xlabel("wiener deblurred")
ppt.imshow(result)
blurred += 0.1 * blurred.std() * numpy.random.standard_normal(blurred.shape)
ppt.subplot(223)
ppt.xlabel("motion & noisy blurred")
ppt.imshow(blurred)
result = wiener(blurred, PSF, 0.1 + 1e-3)
ppt.subplot(224)
ppt.xlabel("wiener deblurred")
ppt.imshow(result)
ppt.show()